{"id":9932,"date":"2023-02-04T21:47:10","date_gmt":"2023-02-05T05:47:10","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9932"},"modified":"2023-02-05T09:13:09","modified_gmt":"2023-02-05T17:13:09","slug":"leetcode-2560-house-robber-iv","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/binary-search\/leetcode-2560-house-robber-iv\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2560.\u00a0House Robber IV"},"content":{"rendered":"\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 2560. House Robber IV - \u5237\u9898\u627e\u5de5\u4f5c EP409\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/DulvOfbAdzI?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>There are several consecutive houses along a street, each of which has some money inside. There is also a robber, who wants to steal money from the homes, but he&nbsp;<strong>refuses to steal from adjacent homes<\/strong>.<\/p>\n\n\n\n<p>The&nbsp;<strong>capability<\/strong>&nbsp;of the robber is the maximum amount of money he steals from one house of all the houses he robbed.<\/p>\n\n\n\n<p>You are given an integer array&nbsp;<code>nums<\/code>&nbsp;representing how much money is stashed in each house. More formally, the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;house from the left has&nbsp;<code>nums[i]<\/code>&nbsp;dollars.<\/p>\n\n\n\n<p>You are also given an integer&nbsp;<code>k<\/code>, representing the&nbsp;<strong>minimum<\/strong>&nbsp;number of houses the robber will steal from.&nbsp;It is always possible to steal at least&nbsp;<code>k<\/code>&nbsp;houses.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>minimum<\/strong>&nbsp;<\/em>capability of the robber out of all the possible ways to steal at least&nbsp;<code>k<\/code>&nbsp;houses.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [2,3,5,9], k = 2\n<strong>Output:<\/strong> 5\n<strong>Explanation:<\/strong> \nThere are three ways to rob at least 2 houses:\n- Rob the houses at indices 0 and 2. Capability is max(nums[0], nums[2]) = 5.\n- Rob the houses at indices 0 and 3. Capability is max(nums[0], nums[3]) = 9.\n- Rob the houses at indices 1 and 3. Capability is max(nums[1], nums[3]) = 9.\nTherefore, we return min(5, 9, 9) = 5.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [2,7,9,3,1], k = 2\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> There are 7 ways to rob the houses. The way which leads to minimum capability is to rob the house at index 0 and 4. Return max(nums[0], nums[4]) = 2.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= nums.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= nums[i] &lt;= 10<sup>9<\/sup><\/code><\/li><li><code>1 &lt;= k &lt;= (nums.length + 1)\/2<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: Binary Search + DP<\/strong><\/h2>\n\n\n\n<p>It&#8217;s easy to see that higher capability means more houses we can rob. Thus this can be formulate as a binary search algorithm e.g. find the minimum C s.t. we can rob at least k houses.<\/p>\n\n\n\n<p>Then we can use dp(i) to calculate maximum houses we can rob if starting from the i&#8217;th house.<br>dp(i) = max(1 + dp(i + 2) if nums[i] &lt;= C else 0, dp(i + 1))<\/p>\n\n\n\n<p>Time complexity: O(n log m)<br>Space complexity: O(n)<\/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  int minCapability(vector<int>& nums, int k) {\n    int l = 0;\n    int r = *max_element(begin(nums), end(nums)) + 1;\n    vector<int> cache(nums.size(), -1);\n    function<int(int, int)> rob = [&](int m, int i) -> int {\n      if (i >= nums.size()) return 0;\n      if (cache[i] >= 0) return cache[i];\n      if (nums[i] > m) return rob(m, i + 1);\n      return cache[i] = max(1 + rob(m, i + 2), rob(m, i + 1));\n    };\n    while (l < r) {\n      int m = l + (r - l) \/ 2;\n      fill(begin(cache), end(cache), -1);\n      if (rob(m, 0) >= k)\n        r = m;\n      else\n        l = m + 1;\n    }\n    return l;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 2: Binary Search + Greedy<\/strong><\/h2>\n\n\n\n<p>From: dp(i) = max(1 + dp(i + 2) if nums[i] &lt;= C else 0, dp(i + 1)) we can see that if we can pick the i-th one, it will be the same or better if we skip and start from dp(i + 1). Thus we can convert this from DP to greedy. As long as we can pick the current one, we pick it first.<\/p>\n\n\n\n<p>Time complexity: O(n log m)<br>Space complexity: O(1)<\/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  int minCapability(vector<int>& nums, int k) {\n    int l = 0;\n    int r = *max_element(begin(nums), end(nums)) + 1;    \n    auto rob = [&](int m) -> int {\n      int ans = 0;\n      for (int i = 0; i < nums.size(); ++i)\n        if (nums[i] <= m) {\n          ++ans;\n          ++i;\n        }\n      return ans;\n    };\n    while (l < r) {\n      int m = l + (r - l) \/ 2;      \n      if (rob(m) >= k)\n        r = m;\n      else\n        l = m + 1;\n    }\n    return l;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>There are several consecutive houses along a street, each of which has some money inside. There is also a robber, who wants to steal money&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[149],"tags":[52,18,177],"class_list":["post-9932","post","type-post","status-publish","format-standard","hentry","category-binary-search","tag-binary-search","tag-dp","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9932","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=9932"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9932\/revisions"}],"predecessor-version":[{"id":9934,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9932\/revisions\/9934"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9932"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9932"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9932"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}