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

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)

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)
    }
}

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]);

ๅฏไปฅๅฏซๆˆไธ‹้ข้€™ๆจฃ, ๅˆไฝตๅ†ไธ€่ตทๅŽปๆฏ”่ผƒไบ† (ๅ…ˆ
        long maxH = Math.max(horizontalCuts[0], h - horizontalCuts[n-1]);

ๆœ€ๅพŒ้€™ๅ€‹ไน˜ๆณ•, ่ฆๅฐๅฟƒ, ๅพŒ้ข่ฆไธ€่ตทไน˜็Žฉ, ๅ†่ฝ‰ int, ไธ็„ถๆœƒๅฃžๆŽ‰

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

Last updated