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

Given a rectangular cake with height h and width 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, 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 verticalCutsSince the answer can be a huge number, return this modulo 10^9 + 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 <= 10^9
  • 1 <= horizontalCuts.length < min(h, 10^5)
  • 1 <= verticalCuts.length < min(w, 10^5)
  • 1 <= horizontalCuts[i] < h
  • 1 <= verticalCuts[i] < w
  • It is guaranteed that all elements in horizontalCuts are distinct.
  • It is guaranteed that all elements in verticalCuts are distinct.

切割后面积最大的蛋糕。

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

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

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

这道题不涉及算法,唯一值得注意的是最后计算面积的时候数据类型的转换。一开始需要先对input数组排序,因为给的input数组是乱序的。另外,如果长和宽都只给了一条边,也是可以组成矩形的,就是看给的这一条边离蛋糕的哪条边缘更远即可。最后长和宽的乘积有可能会超过Integer的上限,所以需要先转换成long型再转换回来才能输出。

时间O(nlogn)

空间O(1)

Java实现

 1 class Solution {
 2     public int maxArea(int h, int w, int[] horizontalCuts, int[] verticalCuts) {
 3         Arrays.sort(horizontalCuts);
 4         Arrays.sort(verticalCuts);
 5         int MOD = (int) Math.pow(10, 9) + 7;
 6         // corner case
 7         int maxH = Math.max(horizontalCuts[0], h - horizontalCuts[horizontalCuts.length - 1]);
 8         int maxV = Math.max(verticalCuts[0], w - verticalCuts[verticalCuts.length - 1]);
 9 
10         // normal case
11         for (int i = 1; i < horizontalCuts.length; i++) {
12             maxH = Math.max(maxH, horizontalCuts[i] - horizontalCuts[i - 1]);
13         }
14         for (int i = 1; i < verticalCuts.length; i++) {
15             maxV = Math.max(maxV, verticalCuts[i] - verticalCuts[i - 1]);
16         }
17         return (int) ((long) maxH * maxV % MOD);
18     }
19 }

LeetCode 题目总结

原文地址:https://www.cnblogs.com/cnoodle/p/14408996.html