![]() |
VOOZH | about |
Given two positive integers h and w representing the height and width of a rectangle. Additionally, we have two arrays, horizontalCuts and verticalCuts, where:
The task is to find the maximum area of the rectangle after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts.
Examples:
Input: h = 6, w = 4, horizontalCuts = [2, 5], verticalCuts = [1, 3]
Output: 6
Explanation: The figure below represents the given rectangle.
👁 2geeksforgeek
Red lines are the horizontal cuts and blue lines are vertical cuts. After the rectangle is cut, the green piece of rectangle has the maximum area.Input: h = 5, w = 4, horizontalCuts = [3, 1], verticalCuts = [1]
Output: 9
The idea is to maximize the area of a rectangular piece formed by the cuts. Since the largest area is determined by the widest horizontal and vertical gaps, we first sort the cut positions and find the maximum gaps between consecutive cuts. The final area is obtained by multiplying the largest horizontal gap with the largest vertical gap. Sorting ensures efficient gap calculation, making the approach optimal.
Steps to implement the above idea:
6