Leetcode 1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts

Problem Explanation:

The problem is asking to find the maximum possible area of the cake after making the horizontal and vertical cuts. We're given a rectangular cake of height h and width w, and two arrays of integers: horizontalCuts and verticalCuts. Here, horizontalCuts[i] is the distance from the top of the cake to the ith horizontal cut and similarly, verticalCuts[j] is the distance from the left of the cake to the jth vertical cut.

Example:

Suppose we have a rectangular cake with h = 5, w = 4 and we make horizontal cuts at positions [1,2,4] and vertical cuts at positions [1,3]. The maximum piece obtained from these cuts would have an area of 4 units.

Approach:

  1. First of all, we sort the horizontalCuts and verticalCuts array in ascending order.

  2. Then, we find the maximum gap between the cuts in the horizontal and vertical direction. If we think about it, the largest piece of a cake we can get is determined by the largest horizontal cut and the largest vertical cut. So, in order to get the maximum size, we have to find the maximum difference between the positions of the cuts. This includes the edges of the cake as well, so we should check the distances between the the first and last cut and the edges also.

  3. Finally, we multiply the largest horizontal cut and the largest vertical cut to get the maximum area of the cake.

It's important to note that the result can be very large, so we have to return it modulo 10^9 + 7 to prevent overflow.

  1. We use a simple for loop to iterate through the horizontal and vertical arrays, keeping track of the largest gap we've encountered.

Solution

Here is the class based solution for the above explanation:

C++ Solution

1
2cpp
3class Solution {
4 public:
5  int maxArea(int h, int w, vector<int>& horizontalCuts, vector<int>& verticalCuts) {
6    constexpr int kMod = 1'000'000'007;
7    sort(begin(horizontalCuts), end(horizontalCuts));
8    sort(begin(verticalCuts), end(verticalCuts));
9
10    // Max gap of each direction
11    int maxGapX = max(verticalCuts[0], w - verticalCuts.back());
12    int maxGapY = max(horizontalCuts[0], h - horizontalCuts.back());
13
14    // Iterating through the cuts to find max gap
15    for (int i = 1; i < verticalCuts.size(); ++i)
16      maxGapX = max(maxGapX, verticalCuts[i] - verticalCuts[i - 1]);
17
18    for (int i = 1; i < horizontalCuts.size(); ++i)
19      maxGapY = max(maxGapY, horizontalCuts[i] - horizontalCuts[i - 1]);
20
21    // The maximum area is obtained by multiplying the max horizontal gap and max vertical gap
22    return static_cast<long>(maxGapX) * maxGapY % kMod;
23  }
24};

Note: The C# solution, Python solution, Java solution, and the JavaScript solution are still needed for this problem. The same approach of finding the maximum horizontal and vertical gaps in the positions of cuts can be used and can return it after multiplying these two maximum gaps. Remember to take the modulo of this multiplication to prevent overflow.## Python Solution

1
2python
3class Solution:
4    def maxArea(self, h, w, horizontalCuts, verticalCuts):
5        MOD = 10 ** 9 + 7
6        horizontalCuts.sort()
7        verticalCuts.sort()
8        
9        max_horizontal_gap = max(horizontalCuts[0], h - horizontalCuts[-1])
10        for i in range(1, len(horizontalCuts)):
11            max_horizontal_gap = max(max_horizontal_gap, horizontalCuts[i] - horizontalCuts[i - 1])
12            
13        max_vertical_gap = max(verticalCuts[0], w - verticalCuts[-1])
14        for i in range(1, len(verticalCuts)):
15            max_vertical_gap = max(max_vertical_gap, verticalCuts[i] - verticalCuts[i - 1])
16            
17        return (max_horizontal_gap * max_vertical_gap) % MOD

JavaScript Solution

1
2javascript
3var maxArea = function(h, w, horizontalCuts, verticalCuts) {
4    const MOD = 1_000_000_007;
5    horizontalCuts.sort((a, b) => a - b);
6    verticalCuts.sort((a, b) => a - b);
7
8    let maxH = Math.max(horizontalCuts[0], h - horizontalCuts[horizontalCuts.length - 1]);
9    for(let i = 1; i < horizontalCuts.length; i++)
10        maxH = Math.max(maxH, horizontalCuts[i]-horizontalCuts[i-1])
11
12    let maxV = Math.max(verticalCuts[0], w - verticalCuts[verticalCuts.length -1]);
13    for(let i = 1; i < verticalCuts.length; i++)
14        maxV = Math.max(maxV, verticalCuts[i]-verticalCuts[i-1])
15
16    return (maxH * maxV) % MOD;
17};

Java Solution

1
2java
3class Solution {
4    public int maxArea(int h, int w, int[] horizontalCuts, int[] verticalCuts) {
5        long MOD = (long) 1e9 + 7;
6        Arrays.sort(horizontalCuts);
7        Arrays.sort(verticalCuts);
8        
9        long max_horizontal_gap = Math.max(horizontalCuts[0], h - horizontalCuts[horizontalCuts.length - 1]);
10        for(int i = 1; i < horizontalCuts.length; i++)
11            max_horizontal_gap = Math.max(max_horizontal_gap, horizontalCuts[i] - horizontalCuts[i - 1]);
12            
13        long max_vertical_gap = Math.max(verticalCuts[0], w - verticalCuts[verticalCuts.length -1]);
14        for(int i = 1; i < verticalCuts.length; i++)
15            max_vertical_gap = Math.max(max_vertical_gap, verticalCuts[i] - verticalCuts[i -1]);
16
17        return (int) ((max_horizontal_gap * max_vertical_gap) % MOD);
18    }
19}

After compiling and running the respective codes in Python, JavaScript, and Java, we can find the maximum possible area of the cake after the given horizontal and vertical cuts. The solutions are based on sorting the horizontal and vertical cuts and finding the maximum gap between them. The maximum gap gives the length and width of the maximum possible rectangular piece of cake. Multiplication of these two maximum gaps gives the maximum area which is required as output of the problem, provided it should be returned modulo 1e9 + 7.


Got a question?ย Ask the Teaching Assistantย anything you don't understand.

Still not clear? Ask in the Forum, ย Discordย orย Submitย the part you don't understand to our editors.

โ†
โ†‘TA ๐Ÿ‘จโ€๐Ÿซ