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 <= 1091 <= horizontalCuts.length <= min(h - 1, 105)1 <= verticalCuts.length <= min(w - 1, 105)1 <= horizontalCuts[i] < h1 <= verticalCuts[i] < whorizontalCuts are distinct.verticalCuts are distinct.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you have a large sheet of paper and you need to make several horizontal and vertical cuts to see which resulting piece has the biggest area. The brute force strategy involves actually making every possible combination of cuts, creating every single little piece of cake, and then measuring them all to find the biggest one.
Here's how the algorithm would work step-by-step:
def max_area(cake_height, cake_width, horizontal_cuts, vertical_cuts):
# We add the boundaries of the cake to properly evaluate all regions
full_horizontal = [0] + sorted(horizontal_cuts) + [cake_height]
full_vertical = [0] + sorted(vertical_cuts) + [cake_width]
maximum_area_found = 0
# Iterate through all cut combinations to test every possible rectangular piece
for horizontal_start_index in range(len(full_horizontal)):
for horizontal_end_index in range(horizontal_start_index + 1, len(full_horizontal)):
for vertical_start_index in range(len(full_vertical)):
for vertical_end_index in range(vertical_start_index + 1, len(full_vertical)):
piece_height = full_horizontal[horizontal_end_index] - full_horizontal[horizontal_start_index]
piece_width = full_vertical[vertical_end_index] - full_vertical[vertical_start_index]
current_piece_area = piece_height * piece_width
# Track the largest area found so far among all generated pieces
if current_piece_area > maximum_area_found:
maximum_area_found = current_piece_area
# Apply modulo constraint to prevent integer overflow for large outputs
return maximum_area_found % 1000000007To find the largest piece of cake, we need to determine the single biggest vertical gap and the single biggest horizontal gap created by the cuts. Multiplying these two maximum gaps together gives the area of the largest possible piece.
Here's how the algorithm would work step-by-step:
def max_area(cake_height, cake_width, horizontal_cuts, vertical_cuts):
# Adding boundaries is necessary to measure the gaps from edges to the first cuts.
horizontal_cuts.extend([0, cake_height])
vertical_cuts.extend([0, cake_width])
horizontal_cuts.sort()
vertical_cuts.sort()
max_horizontal_gap = 0
for index in range(1, len(horizontal_cuts)):
current_gap = horizontal_cuts[index] - horizontal_cuts[index - 1]
if current_gap > max_horizontal_gap:
max_horizontal_gap = current_gap
max_vertical_gap = 0
for index in range(1, len(vertical_cuts)):
current_gap = vertical_cuts[index] - vertical_cuts[index - 1]
if current_gap > max_vertical_gap:
max_vertical_gap = current_gap
# Multiplying max dimensions finds the area of the single largest resulting piece.
maximum_area = max_horizontal_gap * max_vertical_gap
# Modulo is required because the area can be excessively large for memory limits.
return maximum_area % 1000000007| Case | How to Handle |
|---|---|
| Cuts provided in unsorted order | We must explicitly sort both the horizontal and vertical cut arrays before calculating differences. |
| Cuts at the exact boundaries of the cake such as 0 or the max height and width | Prepend 0 and append the cake dimension (height or width) to the respective cut arrays to include boundary intervals. |
| Arrays containing duplicate cut values | Sorting and computing adjacent differences naturally yields a difference of zero for duplicates, ignoring redundant cuts safely. |
| Minimum possible cake dimensions with no intermediate cuts | Defaulting the boundary prepending and appending ensures we correctly calculate the area as the full height multiplied by the full width. |
| Maximum possible dimensions and cut array lengths up to 10^5 | An O(N log N) sorting approach combined with a linear pass scales efficiently and easily passes within time limits. |
| Integer overflow during the multiplication of the maximum horizontal and vertical gaps | We must cast the maximum differences to 64-bit integers and apply the modulo operation 10^9 + 7 to the product before returning. |