{"id":5900,"date":"2019-12-01T13:06:00","date_gmt":"2019-12-01T21:06:00","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5900"},"modified":"2019-12-01T13:11:52","modified_gmt":"2019-12-01T21:11:52","slug":"leetcode-1274-number-of-ships-in-a-rectangle","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/divide-and-conquer\/leetcode-1274-number-of-ships-in-a-rectangle\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1274. Number of Ships in a Rectangle"},"content":{"rendered":"\n<p><em>(This problem is an&nbsp;<strong>interactive problem<\/strong>.)<\/em><\/p>\n\n\n\n<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>\n\n\n\n<p>You have a function&nbsp;<code>Sea.hasShips(topRight, bottomLeft)<\/code>&nbsp;which takes two points&nbsp;as arguments and returns&nbsp;<code>true<\/code>&nbsp;if and only if there is at least one ship in the rectangle represented by the two points, including on the boundary.<\/p>\n\n\n\n<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.&nbsp;&nbsp;It is guaranteed that there are&nbsp;<strong>at most 10 ships<\/strong>&nbsp;in that rectangle.<\/p>\n\n\n\n<p>Submissions making&nbsp;<strong>more than 400 calls<\/strong>&nbsp;to&nbsp;<code>hasShips<\/code>&nbsp;will be judged&nbsp;<em>Wrong Answer<\/em>.&nbsp; Also, any solutions that attempt to circumvent the judge&nbsp;will result in disqualification.<\/p>\n\n\n\n<p><strong>Example :<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2019\/07\/26\/1445_example_1.PNG\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> \nships = [[1,1],[2,2],[3,3],[5,5]], topRight = [4,4], bottomLeft = [0,0]\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> From [0,0] to [4,4] we can count 3 ships within the range.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>On the input&nbsp;<code>ships<\/code>&nbsp;is only given to initialize the map internally.&nbsp;You must solve this problem &#8220;blindfolded&#8221;. In other words, you must find the answer using the given&nbsp;<code>hasShips<\/code>&nbsp;API, without knowing the&nbsp;<code>ships<\/code>&nbsp;position.<\/li><li><code>0 &lt;=&nbsp;bottomLeft[0]&nbsp;&lt;= topRight[0]&nbsp;&lt;= 1000<\/code><\/li><li><code>0 &lt;=&nbsp;bottomLeft[1]&nbsp;&lt;= topRight[1]&nbsp;&lt;= 1000<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Divide and Conquer<\/strong><\/h2>\n\n\n\n<p>If the current rectangle contains ships, subdivide it into 4 smaller ones until <br>1) no ships contained<br>2) the current rectangle is a single point (e.g. topRight == bottomRight)<\/p>\n\n\n\n<p>Time complexity: O(logn)<br>Space complexity: O(logn)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  \/\/ Modify the interface to pass sea as a reference.\n  int countShips(Sea& sea, vector<int> topRight, vector<int> bottomLeft) {\n    int x1 = bottomLeft[0], y1 = bottomLeft[1];\n    int x2 = topRight[0], y2 = topRight[1];    \n    if (x1 > x2 || y1 > y2 || !sea.hasShips(topRight, bottomLeft))\n      return 0;\n    if (x1 == x2 && y1 == y2)\n      return 1;\n    int xm = x1 + (x2 - x1) \/ 2;\n    int ym = y1 + (y2 - y1) \/ 2;\n    return countShips(sea, {xm, ym}, {x1, y1}) + countShips(sea, {xm, y2}, {x1, ym + 1})\n         + countShips(sea, {x2, ym}, {xm + 1, y1}) + countShips(sea, {x2, y2}, {xm + 1, ym + 1});\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>(This problem is an&nbsp;interactive problem.) On the sea represented by a cartesian plane, each ship is located at an integer point, and each integer point&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[313,217,253,341],"class_list":["post-5900","post","type-post","status-publish","format-standard","hentry","category-divide-and-conquer","tag-divide-and-conquer","tag-hard","tag-interactive","tag-quad-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5900","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=5900"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5900\/revisions"}],"predecessor-version":[{"id":5902,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5900\/revisions\/5902"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5900"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5900"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5900"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}