class Solution:
def maxArea(self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int]) -> int:
MOD = 10**9 + 7
horizontalCuts.sort()
verticalCuts.sort()
# Include the edges
maxH = max(horizontalCuts[0], h - horizontalCuts[-1])
for i in range(1, len(horizontalCuts)):
maxH = max(maxH, horizontalCuts[i] - horizontalCuts[i-1])
maxV = max(verticalCuts[0], w - verticalCuts[-1])
for i in range(1, len(verticalCuts)):
maxV = max(maxV, verticalCuts[i] - verticalCuts[i-1])
return (maxH * maxV) % MOD
class Solution {
public:
int maxArea(int h, int w, vector<int>& horizontalCuts, vector<int>& verticalCuts) {
int MOD = 1e9 + 7;
sort(horizontalCuts.begin(), horizontalCuts.end());
sort(verticalCuts.begin(), verticalCuts.end());
long maxH = max(horizontalCuts[0], h - horizontalCuts.back());
for (int i = 1; i < horizontalCuts.size(); ++i) {
maxH = max(maxH, (long)horizontalCuts[i] - horizontalCuts[i-1]);
}
long maxV = max(verticalCuts[0], w - verticalCuts.back());
for (int i = 1; i < verticalCuts.size(); ++i) {
maxV = max(maxV, (long)verticalCuts[i] - verticalCuts[i-1]);
}
return (int)((maxH * maxV) % MOD);
}
};
class Solution {
public int maxArea(int h, int w, int[] horizontalCuts, int[] verticalCuts) {
int MOD = 1_000_000_007;
Arrays.sort(horizontalCuts);
Arrays.sort(verticalCuts);
long maxH = Math.max(horizontalCuts[0], h - horizontalCuts[horizontalCuts.length - 1]);
for (int i = 1; i < horizontalCuts.length; ++i) {
maxH = Math.max(maxH, horizontalCuts[i] - horizontalCuts[i-1]);
}
long maxV = Math.max(verticalCuts[0], w - verticalCuts[verticalCuts.length - 1]);
for (int i = 1; i < verticalCuts.length; ++i) {
maxV = Math.max(maxV, verticalCuts[i] - verticalCuts[i-1]);
}
return (int)((maxH * maxV) % MOD);
}
}
var maxArea = function(h, w, horizontalCuts, verticalCuts) {
const MOD = 1e9 + 7;
horizontalCuts.sort((a,b) => a-b);
verticalCuts.sort((a,b) => a-b);
let maxH = Math.max(horizontalCuts[0], h - horizontalCuts[horizontalCuts.length - 1]);
for (let i = 1; i < horizontalCuts.length; ++i) {
maxH = Math.max(maxH, horizontalCuts[i] - horizontalCuts[i-1]);
}
let maxV = Math.max(verticalCuts[0], w - verticalCuts[verticalCuts.length - 1]);
for (let i = 1; i < verticalCuts.length; ++i) {
maxV = Math.max(maxV, verticalCuts[i] - verticalCuts[i-1]);
}
return (BigInt(maxH) * BigInt(maxV) % BigInt(MOD));
};
You are given a rectangular cake of size h
x w
. There are arrays horizontalCuts
and verticalCuts
representing the distances from the top and left edges of the cake where horizontal and vertical cuts are made, respectively. Your task is to find the area of the largest piece of cake that can be obtained after performing all the cuts. The answer should be returned modulo 109 + 7.
Inputs:
h
: integer, height of the cakew
: integer, width of the cakehorizontalCuts
: list of integersverticalCuts
: list of integersAt first glance, the problem seems to require checking all possible cake pieces after every cut and calculating their areas. But with potentially many cuts, this brute-force approach would be inefficient. Instead, we can look for a pattern: after all cuts are made, the largest possible piece will always be a rectangle whose sides are determined by the largest gaps between consecutive horizontal and vertical cuts.
This insight shifts our focus from tracking all pieces to simply finding the largest "gap" in both dimensions. The largest piece's area will be the product of the largest horizontal gap and the largest vertical gap.
horizontalCuts
and verticalCuts
arrays in ascending order. This makes it easy to compute the distances between consecutive cuts.This approach is efficient, as it only requires sorting and a single pass through each cuts array.
Example Input:
h = 5
, w = 4
horizontalCuts = [1,2,4]
verticalCuts = [1,3]
horizontalCuts = [1,2,4]
verticalCuts = [1,3]
Output: 4
The problem reduces to finding the largest rectangle that can be formed after all cuts. By sorting the cuts and looking for the largest consecutive gaps in both directions, we can efficiently compute the answer. This solution is elegant because it avoids brute-force enumeration and leverages properties of sorted arrays and simple arithmetic, making it both fast and easy to implement.