加载中...
1465-切割后面积最大的蛋糕(Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts)
发表于:2021-12-03 | 分类: 中等
字数统计: 900 | 阅读时长: 4分钟 | 阅读量:

原文链接: https://leetcode-cn.com/problems/maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts

英文原文

You are given a rectangular cake of size h x w and two arrays of integers horizontalCuts and verticalCuts where:

  • horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cut and similarly, and
  • verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut.

Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. Since the answer can be a large number, return this modulo 109 + 7.

 

Example 1:

Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
Output: 4 
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area.

Example 2:

Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
Output: 6
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area.

Example 3:

Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
Output: 9

 

Constraints:

  • 2 <= h, w <= 109
  • 1 <= horizontalCuts.length <= min(h - 1, 105)
  • 1 <= verticalCuts.length <= min(w - 1, 105)
  • 1 <= horizontalCuts[i] < h
  • 1 <= verticalCuts[i] < w
  • All the elements in horizontalCuts are distinct.
  • All the elements in verticalCuts are distinct.

中文题目

矩形蛋糕的高度为 h 且宽度为 w,给你两个整数数组 horizontalCutsverticalCuts,其中 horizontalCuts[i] 是从矩形蛋糕顶部到第  i 个水平切口的距离,类似地, verticalCuts[j] 是从矩形蛋糕的左侧到第 j 个竖直切口的距离。

请你按数组 horizontalCuts verticalCuts 中提供的水平和竖直位置切割后,请你找出 面积最大 的那份蛋糕,并返回其 面积 。由于答案可能是一个很大的数字,因此需要将结果对 10^9 + 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 <= 10^9
  • 1 <= horizontalCuts.length < min(h, 10^5)
  • 1 <= verticalCuts.length < min(w, 10^5)
  • 1 <= horizontalCuts[i] < h
  • 1 <= verticalCuts[i] < w
  • 题目数据保证 horizontalCuts 中的所有元素各不相同
  • 题目数据保证 verticalCuts 中的所有元素各不相同

通过代码

高赞题解

解题思路:
分别找出长和宽的最大值(这里的长和宽指相邻两个切割位置之差),乘积即为面积最大值。
注意 需要补充验证边界切割位置。

Java代码:

public int maxArea(int h, int w, int[] horizontalCuts, int[] verticalCuts) {
    long horizonMax = 0, verticalMax = 0;
    int mod = 1000000007;
    Arrays.sort(horizontalCuts);
    Arrays.sort(verticalCuts);
        
    for (int i = 1; i < horizontalCuts.length; i++) {
        horizonMax = Math.max(horizonMax, horizontalCuts[i] - horizontalCuts[i - 1]);
    }
    // 补充验证边界切割位置
    horizonMax = Math.max(horizonMax, horizontalCuts[0] - 0);
    horizonMax = Math.max(horizonMax, h - horizontalCuts[horizontalCuts.length - 1]);
        
    for (int i = 1; i < verticalCuts.length; i++) {
        verticalMax = Math.max(verticalMax, verticalCuts[i] - verticalCuts[i - 1]);
    }
    // 补充验证边界切割位置
    verticalMax = Math.max(verticalMax, verticalCuts[0] - 0);
    verticalMax = Math.max(verticalMax, w - verticalCuts[verticalCuts.length - 1]);
        
    return (int) ((horizonMax * verticalMax) % mod);
}

统计信息

通过次数 提交次数 AC比率
6621 21185 31.3%

提交历史

提交时间 提交结果 执行时间 内存消耗 语言
上一篇:
1464-数组中两元素的最大乘积(Maximum Product of Two Elements in an Array)
下一篇:
1466-重新规划路线(Reorder Routes to Make All Paths Lead to the City Zero)
本文目录
本文目录