Last active
June 4, 2021 04:44
-
-
Save vrat28/bba9f6d15221552e86b55576c9cda637 to your computer and use it in GitHub Desktop.
Max Area Cake after cuts(Leetcode 1465)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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