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

![](https://4272748102-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LekNH5IywF8mjBxFcnu%2F-MifowAXUZvEIipWu9mF%2F-Mig0YWXyhATvJql5sKg%2Fimage.png?alt=media\&token=de67c290-4941-424a-b7d9-45cb558d83f8)

![](https://4272748102-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LekNH5IywF8mjBxFcnu%2F-MifowAXUZvEIipWu9mF%2F-Mig0aOK6BXetgGfCcTQ%2Fimage.png?alt=media\&token=1ced18c1-5ded-44e3-a657-0920fde38f61)

![](https://4272748102-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LekNH5IywF8mjBxFcnu%2F-MifowAXUZvEIipWu9mF%2F-Mig0d8K6dGtYBxcrNZK%2Fimage.png?alt=media\&token=e6fe16eb-5154-4d82-846e-ab1aa1f1047d)

![](https://4272748102-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LekNH5IywF8mjBxFcnu%2F-MifowAXUZvEIipWu9mF%2F-Mig0gtePqi3jwUFBjaB%2Fimage.png?alt=media\&token=0fff7ef3-4943-4cea-bfad-edbdc9bc5529)

just find **max height (height cut between height cut)** after each cut

and find **max width (width cut between width cut)** after each cut

ans = max height \* max width

time: O(nlogn)

space: O(1)

```java
class Solution {
    public int maxArea(int h, int w, int[] horizontalCuts, int[] verticalCuts) {
        Arrays.sort(horizontalCuts);
        Arrays.sort(verticalCuts);
        int n = horizontalCuts.length;
        int m = verticalCuts.length;
        
        long maxH = Math.max(horizontalCuts[0], h - horizontalCuts[n-1]);
        for (int i = 1; i < n; i++) {
            maxH = Math.max(maxH, horizontalCuts[i] - horizontalCuts[i-1]);
        }
        
        long maxW = Math.max(verticalCuts[0], w - verticalCuts[m-1]);
        for (int i = 1; i < m; i++) {
            maxW = Math.max(maxW, verticalCuts[i] - verticalCuts[i-1]);
        }
        
        return (int)(maxH*maxW%1000000007); // notice here, (int) (product)
    }
}
```





```java
long maxH = horizontalCuts[0];
for (int i = 1; i < n; i++) {
    maxH = Math.max(maxH, horizontalCuts[i] - horizontalCuts[i-1]);
}
maxH = Math.max(maxH, h - horizontalCuts[n-1]);

可以寫成下面這樣, 合併再一起去比較了 (先
```

```java
        long maxH = Math.max(horizontalCuts[0], h - horizontalCuts[n-1]);

```



最後這個乘法, 要小心, 後面要一起乘玩, 再轉 int, 不然會壞掉

```java
return (int)(maxH*maxW%1000000007); // notice here, (int) (product)
```
