Leetcode.1465 切割后面积最大的蛋糕

题目链接

Leetcode.1465 切割后面积最大的蛋糕 rating : 1445

题目描述

矩形蛋糕的高度为 h h h 且宽度为 w w w,给你两个整数数组 h o r i z o n t a l C u t s horizontalCuts horizontalCuts 和 v e r t i c a l C u t s verticalCuts verticalCuts,其中:

  • h o r i z o n t a l C u t s [ i ] horizontalCuts[i] horizontalCuts[i] 是从矩形蛋糕顶部到第 i i i 个水平切口的距离
  • v e r t i c a l C u t s [ j ] verticalCuts[j] verticalCuts[j] 是从矩形蛋糕的左侧到第 j j j 个竖直切口的距离

请你按数组 h o r i z o n t a l C u t s horizontalCuts horizontalCuts 和 v e r t i c a l C u t s verticalCuts verticalCuts 中提供的水平和竖直位置切割后,请你找出 面积最大 的那份蛋糕,并返回其 面积 。由于答案可能是一个很大的数字,因此需要将结果 对 1 0 9 + 7 10^9 + 7 109+7 取余 后返回。

示例 1:

输入:h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]

输出:4

解释:上图所示的矩阵蛋糕中,红色线表示水平和竖直方向上的切口。切割蛋糕后,绿色的那份蛋糕面积最大。

示例 2:

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

输出:6

解释:上图所示的矩阵蛋糕中,红色线表示水平和竖直方向上的切口。切割蛋糕后,绿色和黄色的两份蛋糕面积最大。

示例 3:

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

输出:9

提示:
  • 2 ≤ h , w ≤ 1 0 9 2 \leq h, w \leq 10^9 2≤h,w≤109
  • 1 ≤ h o r i z o n t a l C u t s . l e n g t h ≤ m i n ( h − 1 , 1 0 5 ) 1 \leq horizontalCuts.length \leq min(h - 1, 10^5) 1≤horizontalCuts.length≤min(h−1,105)
  • 1 ≤ v e r t i c a l C u t s . l e n g t h ≤ m i n ( w − 1 , 1 0 5 ) 1 \leq verticalCuts.length \leq min(w - 1, 10^5) 1≤verticalCuts.length≤min(w−1,105)
  • 1 ≤ h o r i z o n t a l C u t s [ i ] < h 1 \leq horizontalCuts[i] < h 1≤horizontalCuts[i]<h
  • 1 ≤ v e r t i c a l C u t s [ i ] < w 1 \leq verticalCuts[i] < w 1≤verticalCuts[i]<w
  • 题目数据保证 h o r i z o n t a l C u t s horizontalCuts horizontalCuts 中的所有元素各不相同
  • 题目数据保证 v e r t i c a l C u t s verticalCuts verticalCuts 中的所有元素各不相同

解法:排序 + 贪心

蛋糕的面积为
a r e a = ( h o r i z o n t a l C u t s [ i ] − h o r i z o n t a l C u t s [ i − 1 ] ) × ( v e r t i c a l C u t s [ i ] − v e r t i c a l C u t s [ i − 1 ] ) area = (horizontalCuts[i] - horizontalCuts[i - 1]) \times (verticalCuts[i] - verticalCuts[i-1]) area=(horizontalCuts[i]−horizontalCuts[i−1])×(verticalCuts[i]−verticalCuts[i−1])

用于相乘的这两项是独立的,所以我们可以先分别找到各自最大的一项,即 m a x ( h o r i z o n t a l C u t s [ i ] − h o r i z o n t a l C u t s [ i − 1 ] ) max(horizontalCuts[i] - horizontalCuts[i - 1]) max(horizontalCuts[i]−horizontalCuts[i−1]) 和 m a x ( v e r t i c a l C u t s [ i ] − v e r t i c a l C u t s [ i − 1 ] ) max(verticalCuts[i] - verticalCuts[i-1]) max(verticalCuts[i]−verticalCuts[i−1])。

再相乘就得到最终的答案了。

时间复杂度: O ( n × l o g n ) O(n \times logn) O(n×logn)

C++代码:

cpp 复制代码
const int MOD = 1e9 + 7;

class Solution {
public:
    int get_max_size(vector<int>& cuts , int size){
        sort(cuts.begin(),cuts.end());
        int ans = max(cuts[0] , size - cuts.back());
        int n = cuts.size();
        for(int i = 1;i < n;i++){
            ans = max(ans , cuts[i] - cuts[i - 1]);
        }

        return ans;
    }

    int maxArea(int h, int w, vector<int>& horizontalCuts, vector<int>& verticalCuts) {
        int max_h = get_max_size(horizontalCuts,h);
        int max_w = get_max_size(verticalCuts,w);
        return max_h * 1LL * max_w % MOD;
    }
};
相关推荐
CUC-MenG1 天前
2025牛客多校第十场 K.神奇集合 F.老师和Yuuka逛商场 E.老师与好感度 I.矩阵 个人题解
数学·线段树·贪心·dp·线性dp·构造·强联通分量·树上背包·线段树二分
Q741_14712 天前
优选算法 力扣 611. 有效三角形的个数 双指针降低时间复杂度 贪心策略 C++题解 每日一题
c++·算法·leetcode·贪心·双指针
hzhzh~17 天前
【C++】神秘-希尔排序
c++·希尔排序·排序·sort
Alfred king19 天前
Leetcode 四数之和
算法·leetcode·职场和发展·数组·排序·双指针
junjunyi19 天前
【LeetCode 148】算法进阶:排序链表 ( 归并排序、快速排序、计数排序 )
链表·排序·分治·归并
Alfred king23 天前
面试150 IPO
面试·职场和发展·贪心·数组··排序
christ_lrs23 天前
2025.7.25 测试 总结
贪心·模拟
christ_lrs25 天前
2025.7.22 测试 总结
贪心·dp
今天背单词了吗9801 个月前
算法学习笔记:29.拓扑排序——从原理到实战,涵盖 LeetCode 与考研 408 例题
java·笔记·算法·拓扑排序·排序
NuyoahC1 个月前
HOT100——排序篇Leetcode215. 数组中的第K个最大元素
c++·leetcode·排序算法·排序