/**
* Runtime: 13 ms, faster than 91.31%
* Memory Usage: 48.6 MB, less than 93.62%
*/
class Solution {
public int maxArea(int h, int w, int[] horizontalCuts, int[] verticalCuts) {
int preH = 0, preV = 0, maxH = 1, maxV = 1;
Arrays.sort(horizontalCuts);
Arrays.sort(verticalCuts);
for (int hh : horizontalCuts) {
maxH = Math.max(maxH, hh - preH);
preH = hh;
}
maxH = Math.max(maxH, h - preH);
for (int vv : verticalCuts) {
maxV = Math.max(maxV, vv - preV);
preV = vv;
}
maxV = Math.max(maxV, w - preV);
return (int)((long)maxH * maxV % 1000000007);
}
}
本来还想试下面这种方法,给每一个方向创建一个数组,遍历cut数组并在被cut的一行标记,再遍历标记后的数组,得到最长的连续cut长度,这样不需要排序
但是发现当h和w设置的很大的时候,会超出内存limit
确实大部分情况下cut的数量会比h、w要小很多,排序应该也不会消耗太多资源,反而是用这种方法占用了太多内存
class Solution {
public int maxArea(int h, int w, int[] horizontalCuts, int[] verticalCuts) {
boolean[] hCuts = new boolean[h], vCuts = new boolean[w];
int maxH = 0, maxV = 0, pre = 0;
for (int cut : horizontalCuts) {
hCuts[cut] = true;
}
for (int i = 1; i < hCuts.length; i++) {
if (hCuts[i]) {
maxH = Math.max(maxH, i - pre);
pre = i;
}
}
maxH = Math.max(maxH, h - pre);
pre = 0;
for (int cut : verticalCuts) {
vCuts[cut] = true;
}
for (int i = 0; i < vCuts.length; i++) {
if (vCuts[i]) {
maxV = Math.max(maxV, i - pre);
pre = i;
}
}
maxV = Math.max(maxV, w - pre);
return (int)((long)maxH * maxV % 1000000007);
}
}