Leetcode 1465: Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts

grid47
grid47
Exploring patterns and algorithms
Jun 13, 2024 7 min read

You are given a rectangular cake of size h x w. There are two arrays of integers, horizontalCuts and verticalCuts. Each element in horizontalCuts represents a horizontal cut, and each element in verticalCuts represents a vertical cut. Your task is to find the maximum area of a piece of cake that can be obtained after making all the cuts specified in the arrays. Since the result can be a large number, return it modulo 10^9 + 7.
Problem
Approach
Steps
Complexity
Input: The input consists of three parameters: the height h, the width w, and two arrays, horizontalCuts and verticalCuts. Each array contains the positions of the cuts. The arrays are non-empty, and the cuts are distinct.
Example: Input: h = 6, w = 5, horizontalCuts = [1, 4], verticalCuts = [2, 3]
Constraints:
• 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
• All elements in horizontalCuts and verticalCuts are distinct.
Output: The output is a single integer, representing the maximum area of the piece of cake after the cuts, modulo 10^9 + 7.
Example: Output: 6
Constraints:
• The result should be computed modulo 10^9 + 7.
Goal: The goal is to compute the maximum possible area of a piece of cake after all the cuts. This can be done by calculating the maximum gaps between horizontal and vertical cuts, and then multiplying those gaps to get the area.
Steps:
• Sort both the horizontalCuts and verticalCuts arrays.
• Find the maximum gap between consecutive horizontal cuts, including the gaps from the top and bottom of the cake.
• Find the maximum gap between consecutive vertical cuts, including the gaps from the left and right sides of the cake.
• Multiply the two maximum gaps to get the maximum area, and return it modulo 10^9 + 7.
Goal: The problem's constraints ensure that the solution can be computed efficiently even for large input sizes.
Steps:
• The dimensions of the cake are large, so an efficient approach is necessary.
• The number of cuts is small enough (up to 10^5), allowing us to sort the cut arrays efficiently.
Assumptions:
• The input arrays are valid and do not contain duplicate values.
• The height and width are sufficiently large to allow multiple cuts.
Input: Input: h = 6, w = 5, horizontalCuts = [1, 4], verticalCuts = [2, 3]
Explanation: Output: 6. The maximum vertical gap is 2 (between cuts 2 and 3) and the maximum horizontal gap is 3 (between cuts 1 and 4). The area is 3 * 2 = 6.

Input: Input: h = 7, w = 6, horizontalCuts = [2, 5], verticalCuts = [1, 4]
Explanation: Output: 9. The maximum vertical gap is 3 (between cuts 1 and 4) and the maximum horizontal gap is 3 (between cuts 2 and 5). The area is 3 * 3 = 9.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus