Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts (LeetCode 1465): Solution and Explanation
A cake of height h and width w is cut at every position in horizontalCuts and verticalCuts. Return the area of the largest piece, modulo 10^9 + 7.
Intuition
Picture the cake after all the cuts. The horizontal cuts split it into bands stacked top to bottom, and the vertical cuts split it into strips side by side. Every piece is exactly one band crossed with one strip.
That makes the two directions independent. The piece's height depends only on which band it sits in, and its width depends only on which strip. To make the area as large as possible, pick the tallest band and the widest strip.
The height of a band is the distance between two neighbouring cuts, so sort the cuts and look at consecutive differences. The cake's edges act as extra cuts at 0 and at h, so the first and last bands must be included too.
The two maxima can each be close to 10^9, so their product overflows 32-bit integers. Multiply in 64-bit and then take the modulo.
Approach: Sort and take the largest gap per axis
- Sort
horizontalCutsandverticalCuts. - Set
maxHto the larger of the top band (horizontalCuts[0]) and the bottom band (h - horizontalCuts[-1]). - For each pair of neighbouring horizontal cuts, update
maxHwith their difference. - Compute
maxWthe same way with the vertical cuts andw. - Return
maxH * maxW % 1_000_000_007, using 64-bit arithmetic.
Step-by-step example
h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
- Both arrays are already sorted.
- Edge bands: top 1, bottom 5 - 4 = 1, so maxH = 1.
- Band 1 to 2 has height 1. Band 2 to 4 has height 2, so maxH = 2.
- Edge strips: left 1, right 4 - 3 = 1, so maxW = 1. Strip 1 to 3 has width 2, so maxW = 2.
- The largest piece is 2 x 2 = 4.
Code: Python, Java, C++
1class Solution:
2 def maxArea(self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int]) -> int:
3 horizontalCuts.sort()
4 verticalCuts.sort()
5 maxH = max(horizontalCuts[0], h - horizontalCuts[-1])
6 for i in range(1, len(horizontalCuts)):
7 maxH = max(maxH, horizontalCuts[i] - horizontalCuts[i - 1])
8 maxW = max(verticalCuts[0], w - verticalCuts[-1])
9 for j in range(1, len(verticalCuts)):
10 maxW = max(maxW, verticalCuts[j] - verticalCuts[j - 1])
11 return maxH * maxW % 1_000_000_0071class Solution {
2 public int maxArea(int h, int w, int[] horizontalCuts, int[] verticalCuts) {
3 Arrays.sort(horizontalCuts);
4 Arrays.sort(verticalCuts);
5 int n = horizontalCuts.length, m = verticalCuts.length;
6 long maxH = Math.max(horizontalCuts[0], h - horizontalCuts[n - 1]);
7 for (int i = 1; i < n; i++) {
8 maxH = Math.max(maxH, horizontalCuts[i] - horizontalCuts[i - 1]);
9 }
10 long maxW = Math.max(verticalCuts[0], w - verticalCuts[m - 1]);
11 for (int j = 1; j < m; j++) {
12 maxW = Math.max(maxW, verticalCuts[j] - verticalCuts[j - 1]);
13 }
14 return (int) (maxH * maxW % 1_000_000_007);
15 }
16}1class Solution {
2public:
3 int maxArea(int h, int w, vector<int>& horizontalCuts, vector<int>& verticalCuts) {
4 sort(horizontalCuts.begin(), horizontalCuts.end());
5 sort(verticalCuts.begin(), verticalCuts.end());
6 int n = horizontalCuts.size(), m = verticalCuts.size();
7 long long maxH = max(horizontalCuts[0], h - horizontalCuts[n - 1]);
8 for (int i = 1; i < n; i++) {
9 maxH = max(maxH, (long long) horizontalCuts[i] - horizontalCuts[i - 1]);
10 }
11 long long maxW = max(verticalCuts[0], w - verticalCuts[m - 1]);
12 for (int j = 1; j < m; j++) {
13 maxW = max(maxW, (long long) verticalCuts[j] - verticalCuts[j - 1]);
14 }
15 return (int) (maxH * maxW % 1000000007);
16 }
17};Time and space complexity
O(n log n + m log m)
Sorting the two cut arrays dominates. The gap scans are linear.
O(1)
Only the two running maxima are stored, apart from the space the sort uses.
Other approaches
Check every piece (time O(n log n + m log m + n m), space O(1))
Sort the cuts and compute the area of every band and strip combination. It gives the same answer but does (n + 1)(m + 1) multiplications instead of exploiting independence.
Bucket-based maximum gap (time O(n + m), space O(n + m))
The largest gap between sorted values can be found in linear time with the pigeonhole bucket trick from Maximum Gap. It removes the sort but is much more code for little practical gain.
Common mistakes
- Forgetting the edge bands. The piece between the top of the cake and the first cut, or between the last cut and the bottom, is often the largest.
- Multiplying two 32-bit ints before taking the modulo. The product can be near 10^18, so cast to 64-bit first.
- Taking the modulo of each maximum separately and then multiplying. Each maximum is below 10^9 + 7 anyway, so that does not prevent the overflow.
- Assuming the cuts arrive sorted. Example 2 lists them out of order.
Frequently asked questions
Why is the largest piece always the tallest band times the widest strip?
Every band crosses every strip, so the band with the greatest height and the strip with the greatest width do meet in a piece. No other piece can have a larger height or a larger width, so none can have a larger area.
Why do we take the answer modulo 10^9 + 7?
Both maxima can approach 10^9, so the area can approach 10^18. The modulo keeps the returned value inside a 32-bit int.
Do I need to handle duplicate cuts?
No. The cut positions on each axis are distinct, and if they were not, a duplicate would only create a zero-height band, which never becomes the maximum.