{"id":1121,"date":"2017-12-05T21:40:51","date_gmt":"2017-12-06T05:40:51","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=1121"},"modified":"2017-12-05T23:05:06","modified_gmt":"2017-12-06T07:05:06","slug":"leetcode-740-delete-and-earn","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-740-delete-and-earn\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 740. Delete and Earn"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 740. Delete and Earn - \u5237\u9898\u627e\u5de5\u4f5c EP125\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/YzZd-bsMthk?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><\/p>\n<p><strong>Problem:<\/strong><\/p>\n<p>Given an array\u00a0<code>nums<\/code>\u00a0of integers, you can perform operations on the array.<\/p>\n<p>In each operation, you pick any\u00a0<code>nums[i]<\/code>\u00a0and delete it to earn\u00a0<code>nums[i]<\/code>\u00a0points. After, you must delete\u00a0<b>every<\/b>element equal to\u00a0<code>nums[i] - 1<\/code>\u00a0or\u00a0<code>nums[i] + 1<\/code>.<\/p>\n<p>You start with 0 points. Return the maximum number of points you can earn by applying such operations.<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"\">Input: nums = [3, 4, 2]\r\nOutput: 6\r\nExplanation: \r\nDelete 4 to earn 4 points, consequently 3 is also deleted.\r\nThen, delete 2 to earn 2 points. 6 total points are earned.\r\n<\/pre>\n<p><b>Example 2:<\/b><\/p>\n<pre class=\"\">Input: nums = [2, 2, 3, 3, 3, 4]\r\nOutput: 9\r\nExplanation: \r\nDelete 3 to earn 3 points, deleting both 2's and the 4.\r\nThen, delete 3 again to earn 3 points, and 3 again to earn 3 points.\r\n9 total points are earned.\r\n<\/pre>\n<p><b>Note:<\/b><\/p>\n<ul>\n<li>The length of\u00a0<code>nums<\/code>\u00a0is at most\u00a0<code>20000<\/code>.<\/li>\n<li>Each element\u00a0<code>nums[i]<\/code>\u00a0is an integer in the range\u00a0<code>[1, 10000]<\/code>.<\/li>\n<\/ul>\n<p><ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-2404451723245401\" data-ad-slot=\"7983117522\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<p><strong>Idea:<\/strong><\/p>\n<p>Reduce the problem to <a href=\"http:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-198-house-robber\/\">House Robber Problem<\/a><\/p>\n<p><span style=\"font-weight: 400;\">Key observations: If we take nums[i]<\/span><\/p>\n<ol>\n<li><span style=\"font-weight: 400;\"> We can safely take all of its copies.<\/span><\/li>\n<li><span style=\"font-weight: 400;\"> We can\u2019t take any of copies of nums[i &#8211; 1] and nums[i + 1]<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400;\">This problem is reduced to 198 House Robber.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Houses[i] has all the copies of num whose value is i.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">[3 4 2] -&gt; [0 2 3 4], rob([0 <\/span><b>2<\/b><span style=\"font-weight: 400;\"> 3 <\/span><b>4<\/b><span style=\"font-weight: 400;\">]) = 6 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">[2, 2, 3, 3, 3, 4] -&gt; [0 2*2 3*3 4], rob([0 2*2 <\/span><b>3*3<\/b><span style=\"font-weight: 400;\"> 4]) = 9<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Time complexity: O(n+r) reduction + O(r) solving rob = O(n + r)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Space complexity: O(r)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">r = max(nums) &#8211; min(nums) + 1<\/span><\/p>\n<p><a href=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/12\/740-ep125.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1128\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/12\/740-ep125.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/12\/740-ep125.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/12\/740-ep125-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/12\/740-ep125-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/p>\n<p>Time complexity: O(n + r)<\/p>\n<p>Space complexity: O(r)<\/p>\n<p><strong>Solution:<\/strong><\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Runtime: 6 ms\r\nclass Solution {\r\npublic:\r\n    int deleteAndEarn(vector&lt;int&gt;&amp; nums) {\r\n        if (nums.empty()) return 0;\r\n        const auto range = minmax_element(nums.begin(), nums.end());\r\n        const int l = *(range.first);\r\n        const int r = *(range.second);        \r\n        vector&lt;int&gt; points(r - l + 1, 0);\r\n        for (const int num : nums)\r\n            points[num - l] += num;\r\n        return rob(points);\r\n    }\r\nprivate:\r\n    \/\/ From LeetCode 198. House Robber\r\n    int rob(const vector&lt;int&gt;&amp; nums) {\r\n        int dp2 = 0;\r\n        int dp1 = 0;\r\n        for (int i = 0; i &lt; nums.size() ; ++i) {\r\n            int dp = max(dp2 + nums[i], dp1);\r\n            dp2 = dp1;\r\n            dp1 = dp;\r\n        }\r\n        return dp1;\r\n    }\r\n};<\/pre>\n<p><strong>Related Problem:<\/strong><\/p>\n<ul>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-198-house-robber\/\">\u82b1\u82b1\u9171 LeetCode 198. House Robber<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Problem: Given an array\u00a0nums\u00a0of integers, you can perform operations on the array. In each operation, you pick any\u00a0nums[i]\u00a0and delete it to earn\u00a0nums[i]\u00a0points. After, you must&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46,164],"tags":[18,181,182],"class_list":["post-1121","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","category-medium","tag-dp","tag-house-robber","tag-reduction","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1121","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=1121"}],"version-history":[{"count":8,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1121\/revisions"}],"predecessor-version":[{"id":1130,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1121\/revisions\/1130"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=1121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=1121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=1121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}