[LeetCode] Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts

1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts

Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cut and similarly, verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut.

Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. Since the answer can be a huge number, return this modulo 10^9 + 7.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public:
int maxArea(int h, int w, vector<int>& horizontalCuts, vector<int>& verticalCuts) {
sort(horizontalCuts.begin(), horizontalCuts.end());
sort(verticalCuts.begin(), verticalCuts.end());
int width = verticalCuts[0], height = horizontalCuts[0], mod = 1e9 + 7;
for(int i = 1; i < horizontalCuts.size(); i++)
height = max(height, horizontalCuts[i] - horizontalCuts[i - 1]);

height = max(height, h - horizontalCuts.back());

for(int i = 1; i < verticalCuts.size(); i++)
width = max(width, verticalCuts[i] - verticalCuts[i - 1]);

width = max(width, w - verticalCuts.back());

return ((long)width * (long)height)%mod;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2021/02/15/PS/LeetCode/maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.