Watch 10 video solutions for Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts, a medium level problem involving Array, Greedy, Sorting. This walkthrough by Coding Decoded has 6,759 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.Problem Overview: A rectangular cake of size h × w is cut using horizontal and vertical cut positions. Each cut divides the cake completely across that axis. After all cuts are applied, multiple smaller rectangles are formed. The task is to compute the maximum possible area of any resulting piece. Because the result can be large, return it modulo 1e9 + 7.
Approach 1: Sort and Find Maximum Gaps (Greedy) (Time: O(n log n), Space: O(1) or O(n) depending on sorting)
The key observation: the largest piece must come from the largest distance between two consecutive horizontal cuts and the largest distance between two consecutive vertical cuts. First sort both cut arrays using a standard sorting routine. Then iterate through each list to compute the maximum gap between adjacent cuts. Remember to also check the edges: 0 → first_cut and last_cut → h (or w). The maximum horizontal gap multiplied by the maximum vertical gap gives the largest rectangle area. This approach works because cuts partition the cake into independent intervals; maximizing each dimension independently yields the optimal rectangle. The final multiplication is taken modulo 1e9+7. This method relies on simple iteration over an array and a greedy selection of the largest gaps.
Approach 2: Dynamic Programming with Memoization (Time: O(n × m), Space: O(n × m))
A more exploratory method models the cake as segments between cuts and recursively computes the maximum area achievable from combinations of horizontal and vertical partitions. Memoization stores results for subproblems defined by index ranges between cuts. For each state, the algorithm considers potential partitions and calculates the maximum area from resulting segments. Although this demonstrates how subproblems overlap and how memoization avoids recomputation, it is less efficient than the greedy observation because the rectangle area is determined solely by the largest gaps. The DP approach is mostly educational for practicing recursion and caching patterns often seen in greedy-vs-DP comparisons.
Recommended for interviews: Interviewers expect the sorting + maximum gap insight. A brute or DP-style exploration shows you understand the partitioning idea, but recognizing that the largest piece is defined by the largest consecutive gaps demonstrates algorithmic clarity. The optimal solution runs in O(n log n) due to sorting and O(1) additional space, which is efficient even for large cut arrays.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Sort and Find Maximum Gaps (Greedy) | O(n log n) | O(1) extra | Best general solution; compute largest horizontal and vertical gaps after sorting cuts |
| Dynamic Programming (Memoization) | O(n × m) | O(n × m) | Educational exploration of partition states; rarely needed for the optimal solution |