Similar Problems
Similar Problems not available
Number Of Ships In A Rectangle - Leetcode Solution
Companies:
LeetCode: Number Of Ships In A Rectangle Leetcode Solution
Difficulty: Hard
Topics: array
Problem Statement
Given two arrays, horizontalCuts and verticalCuts, where horizontalCuts[i] represents a cut along the line y = horizontalCuts[i], and verticalCuts[j] represents a cut along the line x = verticalCuts[j], you have to find the maximum area of a rectangle formed by the cuts and count the number of such rectangles. Since the answer can be very large, you have to return the answer modulo 10^9 + 7.
Solution
In order to solve this problem, we need to find the coordinates of all the corners of the rectangles formed by the cuts. If we have the coordinates of all the corners, we can calculate the area of each rectangle and find the one with the maximum area. Additionally, we can also count the number of rectangles with the maximum area.
Step 1: Find the coordinates of all corners
To find the coordinates of all the corners, we need to sort the arrays horizontalCuts and verticalCuts in ascending order and then add 0 and the maximum value of height and width to the respective arrays. This will ensure that we have all the corners in the arrays.
For example, if we have horizontalCuts = [1,3,2] and verticalCuts = [3,1], then after sorting and adding the maximum values, the arrays become horizontalCuts = [0, 1, 2, 3, height] and verticalCuts = [0, 1, 3, width].
We can then iterate over the sorted arrays and create a list of all the corner points. For example, using the above input, the list of corner points would be [(0, 0), (1, 0), (2, 0), (3, 0), (height, 0), (0, 1), (1, 1), (2, 1), (3, 1), (height, 1), (0, 3), (1, 3), (2, 3), (3, 3), (height, 3), (0, width), (1, width), (2, width), (3, width), (height, width)]. We can store these points in a list in the format [(x1,y1), (x2,y2), ...].
Step 2: Find the area of each rectangle
To find the area of each rectangle, we can iterate over the list of corner points and calculate the distance between adjacent points. We only need to consider adjacent points that have the same x-coordinate or y-coordinate, as those will form the sides of a rectangle. For example, using the above list of corner points, we can calculate the area of the first rectangle as (1-0) * (1-0) = 1 and its sides are formed by the points (0,0), (1,0), (1,1), and (0,1).
Step 3: Find the maximum area
After calculating the area of each rectangle, we can find the maximum area and the number of rectangles with the maximum area. To do this, we can iterate over the list of areas and keep track of the maximum area and the number of rectangles with that area.
Code Implementation
Here is the Python code for the solution:
class Solution:
def maxArea(self, height: int, width: int, horizontalCuts: List[int], verticalCuts: List[int]) -> int:
mod = 10**9 + 7
horizontalCuts.sort()
verticalCuts.sort()
horizontalCuts.append(height)
verticalCuts.append(width)
corners = []
for x in horizontalCuts:
for y in verticalCuts:
corners.append((x, y))
corners.append((0, 0))
corners.sort()
max_area = 0
max_count = 0
for i in range(1, len(corners)):
x1, y1 = corners[i-1]
x2, y2 = corners[i]
if x1 == x2:
width = y2 - y1
max_area = max(max_area, width * (x1 - corners[i-2][0]))
elif y1 == y2:
height = x2 - x1
max_area = max(max_area, height * (y1 - corners[i-2][1]))
for i in range(1, len(corners)):
x1, y1 = corners[i-1]
x2, y2 = corners[i]
if x1 == x2:
width = y2 - y1
if max_area == width * (x1 - corners[i-2][0]):
max_count += 1
elif y1 == y2:
height = x2 - x1
if max_area == height * (y1 - corners[i-2][1]):
max_count += 1
return max_area % mod, max_count % mod
Time Complexity
The sorting of the two arrays takes O(n log n) time, where n is the length of the array. Iterating over all the corner points takes O(n^2) time, but in practice, the number of corner points is typically much smaller than the square of the length of the arrays. Therefore, we can consider the time complexity to be O(n log n).
Space Complexity
The space complexity of the solution is O(n), where n is the number of corner points. This is because we store all the corner points in a list.
Number Of Ships In A Rectangle Solution Code
1