*This is part of a series of Leetcode solution explanations (index). If you liked this solution or found it useful,* *please like**this post and/or* *upvote**my solution post on Leetcode's forums.*

####
Leetcode Problem #1465 (*Medium*): Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts

####
*Description:*

*Description:*

(*Jump to*: *Solution Idea* || *Code*: *JavaScript* | *Python* | *Java* | *C++*)

Given a rectangular cake with height

`h`

and width`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,`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. Since the answer can be a huge number, return this modulo`horizontalCuts`

and`verticalCuts`

`10^9 + 7`

.

####
*Examples:*

*Examples:*

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. Visual:

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. Visual:

Example 3: Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3] Output: 9

####
*Constraints:*

*Constraints:*

`2 <= h, w <= 10^9`

`1 <= horizontalCuts.length < min(h, 10^5)`

`1 <= verticalCuts.length < min(w, 10^5)`

`1 <= horizontalCuts[i] < h`

`1 <= verticalCuts[i] < w`

- It is guaranteed that all elements in
`horizontalCuts`

are distinct.- It is guaranteed that all elements in
`verticalCuts`

are distinct.

####
*Idea:*

*Idea:*

(*Jump to*: *Problem Description* || *Code*: *JavaScript* | *Python* | *Java* | *C++*)

The trick to this problem is realizing that if the horizontal slices and vertical slices are perpendicular, then all vertical slices cross all horizontal slices. This means that we just need to find the largest of each, and the cross-section should be the largest slice.

To find the largest slice of each, we need to first **sort** the horizontal cuts (**hc**) and vertical cuts (**vc**), then iterate through both sets and keep track of the maximum difference found between two consecutive cuts (**maxh**, **maxv**). We need to not forget to include the two end cuts, which are found using **0** and **h**/**w**, as well.

Once we have the largest difference for both, we can just **return** the product of these two numbers, **modulo 1e9+7**.

**Time Complexity: O(N * log(N) + M * log(M))**where**N**is the length of**hc**and**M**is the length of**vc****Space Complexity: O(1)**

####
*Javascript Code:*

*Javascript Code:*

(*Jump to*: *Problem Description* || *Solution Idea*)

```
var maxArea = function(h, w, hc, vc) {
hc.sort((a,b) => a - b)
vc.sort((a,b) => a - b)
let maxh = Math.max(hc[0], h - hc[hc.length-1]),
maxv = Math.max(vc[0], w - vc[vc.length-1])
for (let i = 1; i < hc.length; i++)
maxh = Math.max(maxh, hc[i] - hc[i-1])
for (let i = 1; i < vc.length; i++)
maxv = Math.max(maxv, vc[i] - vc[i-1])
return BigInt(maxh) * BigInt(maxv) % 1000000007n
};
```

####
*Python Code:*

*Python Code:*

(*Jump to*: *Problem Description* || *Solution Idea*)

```
class Solution:
def maxArea(self, h: int, w: int, hc: List[int], vc: List[int]) -> int:
hc.sort()
vc.sort()
maxh, maxv = max(hc[0], h - hc[-1]), max(vc[0], w - vc[-1])
for i in range(1, len(hc)):
maxh = max(maxh, hc[i] - hc[i-1])
for i in range(1, len(vc)):
maxv = max(maxv, vc[i] - vc[i-1])
return (maxh * maxv) % 1000000007
```

####
*Java Code:*

*Java Code:*

(*Jump to*: *Problem Description* || *Solution Idea*)

```
class Solution {
public int maxArea(int h, int w, int[] hc, int[] vc) {
Arrays.sort(hc);
Arrays.sort(vc);
int maxh = Math.max(hc[0], h - hc[hc.length-1]),
maxv = Math.max(vc[0], w - vc[vc.length-1]);
for (int i = 1; i < hc.length; i++)
maxh = Math.max(maxh, hc[i] - hc[i-1]);
for (int i = 1; i < vc.length; i++)
maxv = Math.max(maxv, vc[i] - vc[i-1]);
return (int)((long)maxh * maxv % 1000000007);
}
}
```

####
*C++ Code:*

*C++ Code:*

(*Jump to*: *Problem Description* || *Solution Idea*)

```
class Solution {
public:
int maxArea(int h, int w, vector<int>& hc, vector<int>& vc) {
sort(hc.begin(), hc.end());
sort(vc.begin(), vc.end());
int maxh = max(hc[0], h - hc.back()),
maxv = max(vc[0], w - vc.back());
for (int i = 1; i < hc.size(); i++)
maxh = max(maxh, hc[i] - hc[i-1]);
for (int i = 1; i < vc.size(); i++)
maxv = max(maxv, vc[i] - vc[i-1]);
return (int)((long)maxh * maxv % 1000000007);
}
};
```

## Top comments (0)