Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts - Problem
Imagine you're a pastry chef with a rectangular cake of dimensions h ร w. You need to make precise cuts to create individual pieces, and you want to find the largest possible piece after all cuts are made.
You have two arrays:
horizontalCuts[i]- distance from the top of the cake to the i-th horizontal cutverticalCuts[j]- distance from the left of the cake to the j-th vertical cut
Goal: Return the maximum area of any single piece after making all the specified cuts. Since the result can be very large, return it modulo 10^9 + 7.
Key insight: The largest piece will be formed by the largest gap between consecutive horizontal cuts multiplied by the largest gap between consecutive vertical cuts!
Input & Output
example_1.py โ Basic Case
$
Input:
h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
โบ
Output:
4
๐ก Note:
After sorting and adding boundaries: horizontal cuts at [0,1,2,4,5] and vertical cuts at [0,1,3,4]. The maximum horizontal gap is 2 (between positions 2 and 4), and maximum vertical gap is 2 (between positions 1 and 3). Maximum area = 2 ร 2 = 4.
example_2.py โ Larger Cake
$
Input:
h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
โบ
Output:
6
๐ก Note:
After sorting: horizontal cuts at [0,1,3,5] and vertical cuts at [0,1,4]. Maximum horizontal gap is 2 (between 3 and 5), maximum vertical gap is 3 (between 1 and 4). Maximum area = 2 ร 3 = 6.
example_3.py โ Edge Case
$
Input:
h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
โบ
Output:
9
๐ก Note:
Horizontal cuts at [0,3,5] give max gap of 3. Vertical cuts at [0,3,4] give max gap of 3. Maximum area = 3 ร 3 = 9.
Constraints
- 2 โค h, w โค 109
- 0 โค horizontalCuts.length โค 105
- 0 โค verticalCuts.length โค 105
- 1 โค horizontalCuts[i] < h
- 1 โค verticalCuts[i] < w
- All cuts are at distinct positions
- Return result modulo 109 + 7
Visualization
Tap to expand
Understanding the Visualization
1
Add Boundary Cuts
Imagine cuts at all four edges of the cake (positions 0, h, 0, w)
2
Sort All Cuts
Arrange all cuts in order from left to right and top to bottom
3
Find Largest Gaps
Look for the biggest space between consecutive cuts in each direction
4
Calculate Max Area
The intersection of the largest horizontal and vertical gaps gives the maximum area
Key Takeaway
๐ฏ Key Insight: The maximum area is always at the intersection of the largest horizontal gap and largest vertical gap - no need to check every possible piece!
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code