Skip to content

Latest commit

 

History

History
39 lines (26 loc) · 1.27 KB

File metadata and controls

39 lines (26 loc) · 1.27 KB

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

Leetcode link

解题思路

本题给出了一个 h * w 的蛋糕以及他要切的地方,要求我们求出蛋糕经过切割后剩下的最大面积


本题的思路很简单,就是分别找出横着切与竖着切中的最大距离,然后相乘起来就好了

最后一个要注意的点是,相乘之后的数字可能会超过精度,所以可以选用 JS 中的 bigInt 来保存

Javascript

var maxArea = function(h, w, horizontalCuts, verticalCuts) {
    horizontalCuts.push(h, 0);
    horizontalCuts.sort((a, b) => a-b);
    verticalCuts.push(w, 0);
    verticalCuts.sort((a, b) => a-b);
    let maxWidth = 0, maxHeight = 0;
    for(let height = 1;height < horizontalCuts.length;height++) {
        let diff = horizontalCuts[height] - horizontalCuts[height-1];
        maxHeight = Math.max(maxHeight, diff);
    }
    for(let width = 1;width < verticalCuts.length;width++) {
        let diff = verticalCuts[width] - verticalCuts[width-1];
        maxWidth = Math.max(maxWidth, diff);
    }
    return (BigInt(maxHeight) * BigInt(maxWidth)) % BigInt(Math.pow(10, 9) + 7);
};