Number of Ships in a Rectangle
To solve this problem, you can use a divide-and-conquer approach. You need to search for ships within the given rectangle by calling the provided hasShips
method, and if a ship is present in the current rectangle, you need to further divide the rectangle into smaller parts to find the exact locations of the ships.
Here’s the code:
|
|
Key Takeaways:
- The solution applies divide-and-conquer to explore different parts of the rectangle recursively.
- By dividing the rectangle into smaller parts and calling
hasShips
on them, we can accurately determine the count of ships in the rectangle. - The constraints guarantee that there are at most 10 ships in the rectangle, and the algorithm will not make more than 400 calls to
hasShips
, adhering to the problem’s limitations.