矩形蛋糕的高度为 h 且宽度为 w,给你两个整数数组 horizontalCuts 和 verticalCuts,其中:

  • horizontalCuts[i] 是从矩形蛋糕顶部到第  i 个水平切口的距离
  • verticalCuts[j] 是从矩形蛋糕的左侧到第 j 个竖直切口的距离

请你按数组 horizontalCuts 和 verticalCuts 中提供的水平和竖直位置切割后,请你找出 面积最大 的那份蛋糕,并返回其 面积 。由于答案可能是一个很大的数字,因此需要将结果  109 + 7 取余 后返回。

示例 1:
LeetCode 1465. 切割后面积最大的蛋糕【贪心,数组,排序】1444-LMLPHP

输入:h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
输出:4 
解释:上图所示的矩阵蛋糕中,红色线表示水平和竖直方向上的切口。切割蛋糕后,绿色的那份蛋糕面积最大。

示例 2:
LeetCode 1465. 切割后面积最大的蛋糕【贪心,数组,排序】1444-LMLPHP

输入:h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
输出:6
解释:上图所示的矩阵蛋糕中,红色线表示水平和竖直方向上的切口。切割蛋糕后,绿色和黄色的两份蛋糕面积最大。

示例 3:

输入:h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
输出:9

提示:

  • 2 <= h, w <= 10^9
  • 1 <= horizontalCuts.length <= min(h - 1, 10^5)
  • 1 <= verticalCuts.length <= min(w - 1, 10^5)
  • 1 <= horizontalCuts[i] < h
  • 1 <= verticalCuts[i] < w
  • 题目数据保证 horizontalCuts 中的所有元素各不相同
  • 题目数据保证 verticalCuts 中的所有元素各不相同

解法 贪心+排序

为了算出两个相邻切口的距离,把 horizontalCuts \textit{horizontalCuts} horizontalCuts verticalCuts \textit{verticalCuts} verticalCuts 从小到大排序。

排序后,任意蛋糕都可以由两个相邻的水平切口和两个相邻的竖直切口决定(边界也算切口)。例如示例 1,水平切口 2 , 4 2,4 2,4 和竖直切口 1 , 3 1,3 1,3 就决定了面积最大的那份蛋糕。

所以蛋糕的面积可以表示为
( horizontalCuts [ i ] − horizontalCuts [ i − 1 ] ) ⋅ ( verticalCuts [ j ] − verticalCuts [ j − 1 ] ) (\textit{horizontalCuts}[i] - \textit{horizontalCuts}[i-1])\cdot (\textit{verticalCuts}[j] - \textit{verticalCuts}[j-1]) (horizontalCuts[i]horizontalCuts[i1])(verticalCuts[j]verticalCuts[j1])
由于相乘的两项是互相独立的,只需要分别算出 horizontalCuts [ i ] − horizontalCuts [ i − 1 ] \textit{horizontalCuts}[i] - \textit{horizontalCuts}[i-1] horizontalCuts[i]horizontalCuts[i1] 的最大值和 verticalCuts [ j ] − verticalCuts [ j − 1 ] \textit{verticalCuts}[j] - \textit{verticalCuts}[j-1] verticalCuts[j]verticalCuts[j1] 的最大值,就得到了蛋糕面积的最大值。

也可以这样理解,。接下来只需要知道竖着切时,两刀的最大间隔是多少,就知道了蛋糕的最大面积。

class Solution {
    int get_max_size(int size, vector<int> &cuts) {
        sort(cuts.begin(), cuts.end());
        int res = max(cuts[0], size - cuts.back());
        for (int i = 1; i < cuts.size(); i++) {
            res = max(res, cuts[i] - cuts[i - 1]);
        }
        return res;
    }
public:
    int maxArea(int h, int w, vector<int> &horizontalCuts, vector<int> &verticalCuts) {
        int max_h = get_max_size(h, horizontalCuts);
        int max_w = get_max_size(w, verticalCuts);
        return (long long) max_h * max_w % 1'000'000'007;
    }
};

复杂度分析:

  • 时间复杂度: O ( n log ⁡ n + m log ⁡ m ) \mathcal{O}(n\log n + m\log m) O(nlogn+mlogm) ,其中 n n n horizontalCuts \textit{horizontalCuts} horizontalCuts 的长度, m m m verticalCuts \textit{verticalCuts} verticalCuts 的长度。瓶颈在排序上。
  • 空间复杂度: O ( n ) \mathcal{O}(n) O(n) O ( 1 ) \mathcal{O}(1) O(1)
10-29 09:56