Number of Ships in a Rectangle - Problem
Interactive Ship Detection Challenge
Imagine you're a naval commander using sonar to detect ships in a rectangular area of the ocean! ๐ข Each ship occupies exactly one integer coordinate point on a 2D grid, and no two ships can occupy the same position.
You have access to a special sonar function
โ ๏ธ Critical Constraints:
โข Maximum 10 ships in the target rectangle
โข You can only call
โข Must use efficient divide-and-conquer strategy
๐ฏ Goal: Return the total count of ships in the specified rectangular region using minimal sonar calls.
Imagine you're a naval commander using sonar to detect ships in a rectangular area of the ocean! ๐ข Each ship occupies exactly one integer coordinate point on a 2D grid, and no two ships can occupy the same position.
You have access to a special sonar function
Sea.hasShips(topRight, bottomLeft) that can scan any rectangular region and tell you if there's at least one ship in that area (including the boundaries). Your mission: count the exact number of ships in a given rectangle.โ ๏ธ Critical Constraints:
โข Maximum 10 ships in the target rectangle
โข You can only call
hasShips() up to 400 timesโข Must use efficient divide-and-conquer strategy
๐ฏ Goal: Return the total count of ships in the specified rectangular region using minimal sonar calls.
Input & Output
example_1.py โ Basic Case
$
Input:
ships = [[1,1],[2,2],[3,3],[5,5]], topRight = [4,4], bottomLeft = [0,0]
โบ
Output:
3
๐ก Note:
In the rectangle from [0,0] to [4,4], ships at [1,1], [2,2], and [3,3] are within bounds. Ship at [5,5] is outside the rectangle, so we count 3 ships total.
example_2.py โ Edge Boundaries
$
Input:
ships = [[0,0],[1,1],[2,2]], topRight = [2,2], bottomLeft = [0,0]
โบ
Output:
3
๐ก Note:
All three ships are exactly on the boundary points of the rectangle. The hasShips function includes boundary points, so all ships are counted.
example_3.py โ Empty Region
$
Input:
ships = [[10,10],[15,15]], topRight = [5,5], bottomLeft = [0,0]
โบ
Output:
0
๐ก Note:
No ships exist within the rectangle [0,0] to [5,5]. The hasShips call returns false immediately, and we return 0 without further subdivision.
Visualization
Tap to expand
Understanding the Visualization
1
Wide Area Scan
First sonar ping covers the entire search rectangle
2
Quadrant Division
If ships detected, divide into 4 smaller regions
3
Selective Scanning
Only ping quadrants that showed positive results
4
Pinpoint Location
Continue until each ship is isolated to a single coordinate
Key Takeaway
๐ฏ Key Insight: Only search regions that actually contain ships - empty ocean areas can be completely ignored, reducing API calls by 90%+
Time & Space Complexity
Time Complexity
O(S ร log(A))
Where S is number of ships (โค10) and A is rectangle area. Each ship requires log(A) calls to locate
โก Linearithmic
Space Complexity
O(log(A))
Recursion depth is logarithmic in the rectangle area
โ Linear Space
Constraints
- 1 โค ships.length โค 10
- ships[i].length == 2
- bottomLeft.length == topRight.length == 2
- 0 โค bottomLeft[0] โค topRight[0] โค 1000
- 0 โค bottomLeft[1] โค topRight[1] โค 1000
- At most 400 calls to hasShips are allowed
- ships[i] are unique coordinates
- At most 10 ships in the given rectangle
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code