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, andverticalCuts[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 arrays horizontalCuts
and verticalCuts
. Since the answer can be a large number, return this modulo 109 + 7
.
Example 1:
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:
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:
Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3] Output: 9
Constraints:
2 <= h, w <= 109
1 <= horizontalCuts.length <= min(h - 1, 105)
1 <= verticalCuts.length <= min(w - 1, 105)
1 <= horizontalCuts[i] < h
1 <= verticalCuts[i] < w
horizontalCuts
are distinct.verticalCuts
are distinct.This problem asks to find the maximum area of a piece of cake after making horizontal and vertical cuts at specified positions. The solution leverages a greedy approach combined with sorting.
Core Idea: The largest piece of cake will always be formed by the maximum gap between consecutive cuts (including the implicit cuts at the edges of the cake). Therefore, we need to find the largest gaps in both horizontal and vertical cut positions.
Algorithm:
Handle Boundary Conditions: Add 0 (top/left edge) and the total height/width (h
, w
) to the horizontalCuts
and verticalCuts
arrays, respectively. This accounts for the gaps at the edges of the cake.
Sort Cuts: Sort both horizontalCuts
and verticalCuts
in ascending order. This makes it easy to find the largest gaps between consecutive cuts.
Find Maximum Gaps: Iterate through the sorted arrays to find the maximum difference between consecutive elements. This difference represents the largest piece of cake along the horizontal and vertical axes.
Calculate Maximum Area: The maximum area is simply the product of the largest horizontal gap and the largest vertical gap. Because the problem asks for the result modulo 109 + 7, we perform the multiplication using appropriate data types to prevent integer overflow.
Return Result: Return the maximum area modulo 109 + 7.
Time Complexity: The dominant operations are sorting the horizontalCuts
and verticalCuts
arrays. Sorting takes O(m log m) and O(n log n) time respectively, where 'm' and 'n' are the lengths of the input arrays. The remaining steps have linear time complexity, O(m + n). Therefore, the overall time complexity is O(m log m + n log n).
Space Complexity: The space complexity is determined by the space used for sorting. In-place sorting algorithms might use O(1) extra space, but generally, merge sort or Timsort (used in Python) require O(m + n) extra space in the worst case. However, if we consider the space used for storing the sorted arrays, it remains O(m+n). Therefore, the overall space complexity is O(m + n) (or O(log m + log n) if using in-place algorithms in certain implementations).
Code Examples (Python):
import itertools
def maxArea(h: int, w: int, horizontalCuts: list[int], verticalCuts: list[int]) -> int:
horizontalCuts = sorted([0] + horizontalCuts + [h])
verticalCuts = sorted([0] + verticalCuts + [w])
max_h_gap = 0
max_v_gap = 0
for i in range(1, len(horizontalCuts)):
max_h_gap = max(max_h_gap, horizontalCuts[i] - horizontalCuts[i-1])
for i in range(1, len(verticalCuts)):
max_v_gap = max(max_v_gap, verticalCuts[i] - verticalCuts[i-1])
return (max_h_gap * max_v_gap) % (10**9 + 7)
#More concise version using itertools pairwise
from itertools import pairwise
def maxArea_concise(h: int, w: int, horizontalCuts: list[int], verticalCuts: list[int]) -> int:
horizontalCuts = sorted([0] + horizontalCuts + [h])
verticalCuts = sorted([0] + verticalCuts + [w])
max_h_gap = max(b - a for a, b in pairwise(horizontalCuts))
max_v_gap = max(b - a for a, b in pairwise(verticalCuts))
return (max_h_gap * max_v_gap) % (10**9 + 7)
The concise version uses the pairwise
function from the itertools
library to make the code more readable. Both versions achieve the same time and space complexity. Other language implementations will follow similar logic and complexity patterns, though the specific sorting algorithms and details may vary.