LC.P1465[切割后面积最大的蛋糕]

方法一:贪心+排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {

private static final int MOD = (int) 1e9 + 7;

public int maxArea(int h, int w, int[] horizontalCuts, int[] verticalCuts) {
Arrays.sort(horizontalCuts);
Arrays.sort(verticalCuts);
int m = horizontalCuts.length, n = verticalCuts.length;
int x = Math.max(horizontalCuts[0], h - horizontalCuts[m - 1]);
int y = Math.max(verticalCuts[0], w - verticalCuts[n - 1]);
for (int i = 1; i < m; ++i) {
x = Math.max(x, horizontalCuts[i] - horizontalCuts[i - 1]);
}
for (int i = 1; i < n; ++i) {
y = Math.max(y, verticalCuts[i] - verticalCuts[i - 1]);
}
return (int) (((long) x * y) % MOD);
}
}
  • 时间复杂度:$O(mlogm+nlogn)$
  • 空间复杂度:$O(max(logm,logn))$