|
| 1 | +/** |
| 2 | + * 1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts |
| 3 | + * https://leetcode.com/problems/maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts/ |
| 4 | + * Difficulty: Medium |
| 5 | + * |
| 6 | + * You are given a rectangular cake of size h x w and two arrays of integers horizontalCuts and |
| 7 | + * verticalCuts where: |
| 8 | + * - horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal |
| 9 | + * cut and similarly, and |
| 10 | + * - verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut. |
| 11 | + * |
| 12 | + * Return the maximum area of a piece of cake after you cut at each horizontal and vertical position |
| 13 | + * provided in the arrays horizontalCuts and verticalCuts. Since the answer can be a large number, |
| 14 | + * return this modulo 109 + 7. |
| 15 | + */ |
| 16 | + |
| 17 | +/** |
| 18 | + * @param {number} h |
| 19 | + * @param {number} w |
| 20 | + * @param {number[]} horizontalCuts |
| 21 | + * @param {number[]} verticalCuts |
| 22 | + * @return {number} |
| 23 | + */ |
| 24 | +var maxArea = function(h, w, horizontalCuts, verticalCuts) { |
| 25 | + horizontalCuts.sort((a, b) => a - b); |
| 26 | + verticalCuts.sort((a, b) => a - b); |
| 27 | + |
| 28 | + let maxHeight = Math.max(horizontalCuts[0], h - horizontalCuts[horizontalCuts.length - 1]); |
| 29 | + let maxWidth = Math.max(verticalCuts[0], w - verticalCuts[verticalCuts.length - 1]); |
| 30 | + |
| 31 | + for (let i = 1; i < horizontalCuts.length; i++) { |
| 32 | + maxHeight = Math.max(maxHeight, horizontalCuts[i] - horizontalCuts[i - 1]); |
| 33 | + } |
| 34 | + for (let i = 1; i < verticalCuts.length; i++) { |
| 35 | + maxWidth = Math.max(maxWidth, verticalCuts[i] - verticalCuts[i - 1]); |
| 36 | + } |
| 37 | + |
| 38 | + return Number((BigInt(maxHeight) * BigInt(maxWidth)) % BigInt(1000000007)); |
| 39 | +}; |
0 commit comments