Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts - Problem
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 i-th horizontal cut and similarly, and verticalCuts[j] is the distance from the left of the rectangular cake to the j-th 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 large number, return this modulo 10⁹ + 7.
Input & Output
Example 1 — Basic Case
$
Input:
h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
›
Output:
4
💡 Note:
After adding boundaries: horizontal cuts [0,1,2,4,5], vertical cuts [0,1,3,4]. Maximum height gap = 5-2 = 3, maximum width gap = 3-1 = 2. But actually max height gap = 2 (4-2) and max width = 2 (3-1), so area = 4.
Example 2 — Single Cut
$
Input:
h = 5, w = 4, horizontalCuts = [3], verticalCuts = [1]
›
Output:
6
💡 Note:
Horizontal gaps: [3-0=3, 5-3=2], max = 3. Vertical gaps: [1-0=1, 4-1=3], max = 3. Area = 3×2 = 6.
Example 3 — No Cuts
$
Input:
h = 5, w = 4, horizontalCuts = [], verticalCuts = []
›
Output:
20
💡 Note:
No internal cuts, so the entire cake 5×4 = 20 is the maximum piece.
Constraints
- 2 ≤ h, w ≤ 109
- 1 ≤ horizontalCuts.length, verticalCuts.length ≤ 105
- 1 ≤ horizontalCuts[i] < h
- 1 ≤ verticalCuts[i] < w
- All the elements in horizontalCuts are distinct
- All the elements in verticalCuts are distinct
Visualization
Tap to expand
Understanding the Visualization
1
Input
Cake dimensions 5×4 with cuts at [1,2,4] horizontal and [1,3] vertical
2
Process
Cuts create a grid - find largest rectangle
3
Output
Maximum area piece = 4
Key Takeaway
🎯 Key Insight: The largest cake piece is formed by the maximum spacing between consecutive cuts in each direction
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code