<?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>bitmap Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/bitmap/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/bitmap/</link>
	<description></description>
	<lastBuildDate>Tue, 23 Mar 2021 16:17:13 +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>bitmap Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/bitmap/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1799. Maximize Score After N Operations</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1799-maximize-score-after-n-operations/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1799-maximize-score-after-n-operations/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 20 Mar 2021 16:47:45 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[bitmap]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[mask]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8259</guid>

					<description><![CDATA[<p>You are given&#160;nums, an array of positive integers of size&#160;2 * n. You must perform&#160;n&#160;operations on this array. In the&#160;ith&#160;operation&#160;(1-indexed), you will: Choose two elements,&#160;x&#160;and&#160;y.&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1799-maximize-score-after-n-operations/">花花酱 LeetCode 1799. Maximize Score After N Operations</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<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">
<iframe title="花花酱 LeetCode 1799. Maximize Score After N Operations - 刷题找工作 EP390" width="500" height="281" src="https://www.youtube.com/embed/M-rv0PV_NnE?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<p>You are given&nbsp;<code>nums</code>, an array of positive integers of size&nbsp;<code>2 * n</code>. You must perform&nbsp;<code>n</code>&nbsp;operations on this array.</p>



<p>In the&nbsp;<code>i<sup>th</sup></code>&nbsp;operation&nbsp;<strong>(1-indexed)</strong>, you will:</p>



<ul><li>Choose two elements,&nbsp;<code>x</code>&nbsp;and&nbsp;<code>y</code>.</li><li>Receive a score of&nbsp;<code>i * gcd(x, y)</code>.</li><li>Remove&nbsp;<code>x</code>&nbsp;and&nbsp;<code>y</code>&nbsp;from&nbsp;<code>nums</code>.</li></ul>



<p>Return&nbsp;<em>the maximum score you can receive after performing&nbsp;</em><code>n</code><em>&nbsp;operations.</em></p>



<p>The function&nbsp;<code>gcd(x, y)</code>&nbsp;is the greatest common divisor of&nbsp;<code>x</code>&nbsp;and&nbsp;<code>y</code>.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong>&nbsp;The optimal choice of operations is:
(1 * gcd(1, 2)) = 1
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [3,4,6,8]
<strong>Output:</strong> 11
<strong>Explanation:</strong>&nbsp;The optimal choice of operations is:
(1 * gcd(3, 6)) + (2 * gcd(4, 8)) = 3 + 8 = 11
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,2,3,4,5,6]
<strong>Output:</strong> 14
<strong>Explanation:</strong>&nbsp;The optimal choice of operations is:
(1 * gcd(1, 5)) + (2 * gcd(2, 4)) + (3 * gcd(3, 6)) = 1 + 4 + 9 = 14
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>1 &lt;= n &lt;= 7</code></li><li><code>nums.length == 2 * n</code></li><li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li></ul>



<h2><strong>Solution: Mask DP</strong></h2>



<figure class="wp-block-image size-large"><a href="https://zxi.mytechroad.com/blog/wp-content/uploads/2021/03/1799-ep390.png"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2021/03/1799-ep390.png" alt="" class="wp-image-8277" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2021/03/1799-ep390.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2021/03/1799-ep390-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2021/03/1799-ep390-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></a></figure>



<p>dp(mask, i) := max score of numbers (represented by a binary mask) at the i-th operations.<br>ans = dp(1, mask)<br>base case: dp = 0 if mask == 0<br>Transition: dp(mask, i) = max(dp(new_mask, i + 1) + i * gcd(nums[m], nums[n]))<br></p>



<p>Time complexity: O(n<sup>2</sup>*2<sup>2n</sup>)<br>Space complexity: O(2<sup>2n</sup>)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua, 184 ms, 8.3 MB
class Solution {
public:
  int maxScore(vector&lt;int&gt;&amp; nums) {
    const int l = nums.size();
    vector&lt;int&gt; cache(1 &lt;&lt; l);
    function&lt;int(int)&gt; dp = [&amp;](int mask) {      
      if (mask == 0) return 0;
      int&amp; ans = cache[mask];
      if (ans &gt; 0) return ans;
      const int k = (l - __builtin_popcount(mask)) / 2 + 1;
      for (int i = 0; i &lt; l; ++i)
        for (int j = i + 1; j &lt; l; ++j)
          if ((mask &amp; (1 &lt;&lt; i)) &amp;&amp; (mask &amp; (1 &lt;&lt; j)))
            ans = max(ans, k * gcd(nums[i], nums[j])
                           + dp(mask ^ (1 &lt;&lt; i) ^ (1 &lt;&lt; j)));
      return ans;
    };
    return dp((1 &lt;&lt; l) - 1);
  }
};</pre>
</div></div>



<p>Bottom-Up</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua, 156 ms, 8.3 MB
class Solution {
public:
  int maxScore(vector&lt;int&gt;&amp; nums) {
    const int l = nums.size();
    vector&lt;int&gt; dp(1 &lt;&lt; l);    
    for (int mask = 0; mask &lt; 1 &lt;&lt; l; ++mask) {
      int c = __builtin_popcount(mask);
      if (c &amp; 1) continue; // only do in pairs
      int k = c / 2 + 1;
      for (int i = 0; i &lt; l; ++i)
        for (int j = i + 1; j &lt; l; ++j)
          if ((mask &amp; (1 &lt;&lt; i)) + (mask &amp; (1 &lt;&lt; j)) == 0) {            
            int new_mask = mask | (1 &lt;&lt; i) | (1 &lt;&lt; j);            
            dp[new_mask] = max(dp[new_mask],
                               k * gcd(nums[i], nums[j]) + dp[mask]);
        }
    }
    return dp[(1 &lt;&lt; l) - 1];
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1799-maximize-score-after-n-operations/">花花酱 LeetCode 1799. Maximize Score After N Operations</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-1799-maximize-score-after-n-operations/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
