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 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
๐Ÿšข Naval Sonar Detection StrategySONAR๐Ÿšข๐Ÿšข๐Ÿšข๐Ÿ’ก Divide ocean into quadrants, scan only promising areasโœ… Efficient: ~12 sonar pings to find 3 ships vs 700 for brute force
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

n
2n
โšก Linearithmic
Space Complexity
O(log(A))

Recursion depth is logarithmic in the rectangle area

n
2n
โœ“ 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
Asked in
Google 35 Amazon 28 Meta 22 Microsoft 18
28.9K Views
Medium Frequency
~25 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen