Skip to content

Instantly share code, notes, and snippets.

@vrat28
Last active June 4, 2021 04:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vrat28/bba9f6d15221552e86b55576c9cda637 to your computer and use it in GitHub Desktop.
Save vrat28/bba9f6d15221552e86b55576c9cda637 to your computer and use it in GitHub Desktop.
Max Area Cake after cuts(Leetcode 1465)
//Link: https://leetcode.com/problems/maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts/
class Solution {
func maxArea(_ h: Int, _ w: Int, _ horizontalCuts: [Int], _ verticalCuts: [Int]) -> Int {
// first sort the cuts
let hcuts = horizontalCuts.sorted()
let vcuts = verticalCuts.sorted()
var maxWidth:Int = 0, maxHeight:Int = 0
// Find max gap b/w the vertical lines = Width
maxWidth = max(vcuts[0], w - vcuts.last!)
for i in 1..<vcuts.count{
maxWidth = max(maxWidth, vcuts[i] - vcuts[i-1])
}
// Max gap b/w horizontal line = Height
maxHeight = max(hcuts[0], h - hcuts.last!)
for i in 1..<hcuts.count{
maxHeight = max(maxHeight, hcuts[i] - hcuts[i-1])
}
// calculate the area
// since area would be huge return this modulo 10^9 + 7.
return (maxWidth * maxHeight) % 1000000007
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment