|
| 1 | +<!--|This file generated by command(leetcode description); DO NOT EDIT. |--> |
| 2 | +<!--+----------------------------------------------------------------------+--> |
| 3 | +<!--|@author openset <[email protected]> |--> |
| 4 | +<!--|@link https://github.com/openset |--> |
| 5 | +<!--|@home https://github.com/openset/leetcode |--> |
| 6 | +<!--+----------------------------------------------------------------------+--> |
| 7 | + |
| 8 | +[< Previous](https://github.com/openset/leetcode/tree/master/problems/delete-tree-nodes "Delete Tree Nodes") |
| 9 | + |
| 10 | +Next > |
| 11 | + |
| 12 | +## [1274. Number of Ships in a Rectangle (Hard)](https://leetcode.com/problems/number-of-ships-in-a-rectangle "矩形内船只的数目") |
| 13 | + |
| 14 | +<p><em>(This problem is an <strong>interactive problem</strong>.)</em></p> |
| 15 | + |
| 16 | +<p>On the sea represented by a cartesian plane, each ship is located at an integer point, and each integer point may contain at most 1 ship.</p> |
| 17 | + |
| 18 | +<p>You have a function <code>Sea.hasShips(topRight, bottomLeft)</code> which takes two points as arguments and returns <code>true</code> if and only if there is at least one ship in the rectangle represented by the two points, including on the boundary.</p> |
| 19 | + |
| 20 | +<p>Given two points, which are 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 <strong>at most 10 ships</strong> in that rectangle.</p> |
| 21 | + |
| 22 | +<p>Submissions making <strong>more than 400 calls</strong> to <code>hasShips</code> will be judged <em>Wrong Answer</em>. Also, any solutions that attempt to circumvent the judge will result in disqualification.</p> |
| 23 | + |
| 24 | +<p> </p> |
| 25 | +<p><strong>Example :</strong></p> |
| 26 | + |
| 27 | +<p><img alt="" src="https://assets.leetcode.com/uploads/2019/07/26/1445_example_1.PNG" style="width: 400px; height: 404px;" /></p> |
| 28 | + |
| 29 | +<pre> |
| 30 | +<strong>Input:</strong> |
| 31 | +ships = [[1,1],[2,2],[3,3],[5,5]], topRight = [4,4], bottomLeft = [0,0] |
| 32 | +<strong>Output:</strong> 3 |
| 33 | +<strong>Explanation:</strong> From [0,0] to [4,4] we can count 3 ships within the range. |
| 34 | +</pre> |
| 35 | + |
| 36 | +<p> </p> |
| 37 | +<p><strong>Constraints:</strong></p> |
| 38 | + |
| 39 | +<ul> |
| 40 | + <li>On the input <code>ships</code> is only given to initialize the map internally. You must solve this problem "blindfolded". In other words, you must find the answer using the given <code>hasShips</code> API, without knowing the <code>ships</code> position.</li> |
| 41 | + <li><code>0 <= bottomLeft[0] <= topRight[0] <= 1000</code></li> |
| 42 | + <li><code>0 <= bottomLeft[1] <= topRight[1] <= 1000</code></li> |
| 43 | +</ul> |
| 44 | + |
| 45 | +### Related Topics |
| 46 | + [[Divide and Conquer](https://github.com/openset/leetcode/tree/master/tag/divide-and-conquer/README.md)] |
| 47 | + |
| 48 | +### Hints |
| 49 | +<details> |
| 50 | +<summary>Hint 1</summary> |
| 51 | +Use divide and conquer technique. |
| 52 | +</details> |
| 53 | + |
| 54 | +<details> |
| 55 | +<summary>Hint 2</summary> |
| 56 | +Divide the query rectangle into 4 rectangles. |
| 57 | +</details> |
| 58 | + |
| 59 | +<details> |
| 60 | +<summary>Hint 3</summary> |
| 61 | +Use recursion to continue with the rectangles that has ships only. |
| 62 | +</details> |
0 commit comments