You are given a rectangular cake of size h x 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, and
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 arrayshorizontalCutsandverticalCuts. Since the answer can be a large number, return this modulo10^9 + 7.
Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
Output: 4
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area.
Example 2:
1
2
3
Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
Output: 6
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area.
Example 3:
1
2
Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
Output: 9
For horizontal cuts, find the largest gap by comparing the first cut, the distance from the last cut to the edge, and all gaps between consecutive cuts.
Repeat this process for vertical cuts to get the maximum width.
Multiply the maximum height and width, convert to long to avoid overflow, and return the result modulo 10^9 + 7.
The largest piece of cake will be determined by the largest gap between consecutive horizontal cuts (height) and the largest gap between consecutive vertical cuts (width). By sorting the cuts and checking the maximum difference between adjacent cuts (including the edges), we can easily find these values.
For horizontal cuts, find the largest gap by comparing the first cut, the distance from the last cut to the edge, and all gaps between consecutive cuts.
Repeat this process for vertical cuts to get the maximum width.
Multiply the maximum height and width, convert to long to avoid overflow, and return the result modulo 10^9 + 7.
classSolution {
public:int maxArea(int h, int w, vector<int>& hc, vector<int>& vc) {
int M =1e9+7;
sort(hc.begin(), hc.end());
sort(vc.begin(), vc.end());
int max_h = max(hc[0], h - hc.back());
int max_w = max(vc[0], w - vc.back());
for (int i =1; i < hc.size(); ++i)
max_h = max(max_h, hc[i] - hc[i -1]);
for (int i =1; i < vc.size(); ++i)
max_w = max(max_w, vc[i] - vc[i -1]);
longlong ans =1LL* max_h * max_w;
return ans % M;
}
};