<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>house robber Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/house-robber/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/house-robber/</link>
	<description></description>
	<lastBuildDate>Wed, 06 Dec 2017 07:05:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>house robber Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/house-robber/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 740. Delete and Earn</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-740-delete-and-earn/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-740-delete-and-earn/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Wed, 06 Dec 2017 05:40:51 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[Medium]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[house robber]]></category>
		<category><![CDATA[reduction]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=1121</guid>

					<description><![CDATA[<p>Problem: Given an array nums of integers, you can perform operations on the array. In each operation, you pick any nums[i] and delete it to earn nums[i] points. After, you must&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-740-delete-and-earn/">花花酱 LeetCode 740. Delete and Earn</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><iframe width="500" height="375" src="https://www.youtube.com/embed/YzZd-bsMthk?feature=oembed" frameborder="0" gesture="media" allow="encrypted-media" allowfullscreen></iframe></p>
<p><strong>Problem:</strong></p>
<p>Given an array <code>nums</code> of integers, you can perform operations on the array.</p>
<p>In each operation, you pick any <code>nums[i]</code> and delete it to earn <code>nums[i]</code> points. After, you must delete <b>every</b>element equal to <code>nums[i] - 1</code> or <code>nums[i] + 1</code>.</p>
<p>You start with 0 points. Return the maximum number of points you can earn by applying such operations.</p>
<p><b>Example 1:</b></p><pre class="crayon-plain-tag">Input: nums = [3, 4, 2]
Output: 6
Explanation: 
Delete 4 to earn 4 points, consequently 3 is also deleted.
Then, delete 2 to earn 2 points. 6 total points are earned.</pre><p><b>Example 2:</b></p><pre class="crayon-plain-tag">Input: nums = [2, 2, 3, 3, 3, 4]
Output: 9
Explanation: 
Delete 3 to earn 3 points, deleting both 2's and the 4.
Then, delete 3 again to earn 3 points, and 3 again to earn 3 points.
9 total points are earned.</pre><p><b>Note:</b></p>
<ul>
<li>The length of <code>nums</code> is at most <code>20000</code>.</li>
<li>Each element <code>nums[i]</code> is an integer in the range <code>[1, 10000]</code>.</li>
</ul>
<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 />
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script></p>
<p><strong>Idea:</strong></p>
<p>Reduce the problem to <a href="http://zxi.mytechroad.com/blog/dynamic-programming/leetcode-198-house-robber/">House Robber Problem</a></p>
<p><span style="font-weight: 400;">Key observations: If we take nums[i]</span></p>
<ol>
<li><span style="font-weight: 400;"> We can safely take all of its copies.</span></li>
<li><span style="font-weight: 400;"> We can’t take any of copies of nums[i &#8211; 1] and nums[i + 1]</span></li>
</ol>
<p><span style="font-weight: 400;">This problem is reduced to 198 House Robber.</span></p>
<p><span style="font-weight: 400;">Houses[i] has all the copies of num whose value is i.</span></p>
<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            </span></p>
<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>
<p><span style="font-weight: 400;">Time complexity: O(n+r) reduction + O(r) solving rob = O(n + r)</span></p>
<p><span style="font-weight: 400;">Space complexity: O(r)</span></p>
<p><span style="font-weight: 400;">r = max(nums) &#8211; min(nums) + 1</span></p>
<p><a href="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/12/740-ep125.png"><img 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="(max-width: 960px) 100vw, 960px" /></a></p>
<p>Time complexity: O(n + r)</p>
<p>Space complexity: O(r)</p>
<p><strong>Solution:</strong></p><pre class="crayon-plain-tag">// Author: Huahua
// Runtime: 6 ms
class Solution {
public:
    int deleteAndEarn(vector&lt;int&gt;&amp; nums) {
        if (nums.empty()) return 0;
        const auto range = minmax_element(nums.begin(), nums.end());
        const int l = *(range.first);
        const int r = *(range.second);        
        vector&lt;int&gt; points(r - l + 1, 0);
        for (const int num : nums)
            points[num - l] += num;
        return rob(points);
    }
private:
    // From LeetCode 198. House Robber
    int rob(const vector&lt;int&gt;&amp; nums) {
        int dp2 = 0;
        int dp1 = 0;
        for (int i = 0; i &lt; nums.size() ; ++i) {
            int dp = max(dp2 + nums[i], dp1);
            dp2 = dp1;
            dp1 = dp;
        }
        return dp1;
    }
};</pre><p><strong>Related Problem:</strong></p>
<ul>
<li><a href="http://zxi.mytechroad.com/blog/dynamic-programming/leetcode-198-house-robber/">花花酱 LeetCode 198. House Robber</a></li>
</ul>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-740-delete-and-earn/">花花酱 LeetCode 740. Delete and Earn</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-740-delete-and-earn/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
