Number of Ships in a Rectangle Problem
Description
LeetCode Problem 1274.
(This problem is an interactive problem.)
Each ship is located at an integer point on the sea represented by a cartesian plane, and each integer point may contain at most 1 ship.
You have a function Sea.hasShips(topRight, bottomLeft) which takes two points as arguments and returns true If there is at least one ship in the rectangle represented by the two points, including on the boundary.
Given two points: the top right and bottom left corners of a rectangle, return the number of ships present in that rectangle. It is guaranteed that there are at most 10 ships in that rectangle.
Submissions making more than 400 calls to hasShips will be judged Wrong Answer. Also, any solutions that attempt to circumvent the judge will result in disqualification.
Example :
Input:
ships = [[1,1],[2,2],[3,3],[5,5]], topRight = [4,4], bottomLeft = [0,0]
Output: 3
Explanation: From [0,0] to [4,4] we can count 3 ships within the range.
Example 2:
Input: ans = [[1,1],[2,2],[3,3]], topRight = [1000,1000], bottomLeft = [0,0]
Output: 3
Constraints:
- On the input
shipsis only given to initialize the map internally. You must solve this problem "blindfolded". In other words, you must find the answer using the givenhasShipsAPI, without knowing theshipsposition. 0 <= bottomLeft[0] <= topRight[0] <= 10000 <= bottomLeft[1] <= topRight[1] <= 1000topRight != bottomLeft
Solution
Approach: Divide and Conquer into Quarters
We can only ask whether a rectangle contains at least one ship. That is enough to find them all by splitting:
- If the rectangle has no ship, return
0and do not look inside it. - If it has a ship and is a single point, return
1: a point holds at most one ship. - Otherwise cut it at the middle row and the middle column,
mx = (x1 + x2) / 2andmy = (y1 + y2) / 2, into four quarters, and add up the counts of the quarters.
The quarters [x1..mx] x [y1..my], [mx+1..x2] x [y1..my], [x1..mx] x [my+1..y2] and [mx+1..x2] x [my+1..y2] cover the rectangle without overlapping, so every ship is counted exactly once. When the rectangle is only one point wide (or tall), some of those quarters are empty, with x1 > x2 or y1 > y2. We return 0 for those before calling hasShips, so no call is wasted on them.
Empty rectangles are never split, so the calls only follow the paths that lead to ships. A side of at most 1001 points shrinks to a single point after 10 halvings. At each level at most 10 rectangles contain a ship, and each is split into four, so a level costs at most 40 calls (fewer at the top, where there are only 1 and then 4 rectangles). That is at most 1 + 4 + 16 + 8 · 40 = 341 calls, under the limit of 400.
Complexity: O(S · log M) calls to hasShips, where S <= 10 is the number of ships and M the longer side of the rectangle. The recursion depth is O(log M).
# """
# This is Sea's API interface.
# You should not implement it, or speculate about its implementation
# """
#class Sea:
# def hasShips(self, topRight: 'Point', bottomLeft: 'Point') -> bool:
#
#class Point:
# def __init__(self, x: int, y: int):
# self.x = x
# self.y = y
class Solution:
def countShips(self, sea: 'Sea', topRight: 'Point', bottomLeft: 'Point') -> int:
x1, y1 = bottomLeft.x, bottomLeft.y
x2, y2 = topRight.x, topRight.y
if x1 > x2 or y1 > y2:
return 0 # an empty quarter (the rectangle was one cell wide or tall)
if not sea.hasShips(topRight, bottomLeft):
return 0 # nothing here: do not split further
if x1 == x2 and y1 == y2:
return 1 # a single point holds at most one ship
mx, my = (x1 + x2) // 2, (y1 + y2) // 2
return (self.countShips(sea, topRight, Point(mx + 1, my + 1))
+ self.countShips(sea, Point(mx, y2), Point(x1, my + 1))
+ self.countShips(sea, Point(mx, my), bottomLeft)
+ self.countShips(sea, Point(x2, my), Point(mx + 1, y1)))