{"id":8259,"date":"2021-03-20T09:47:45","date_gmt":"2021-03-20T16:47:45","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8259"},"modified":"2021-03-23T09:17:13","modified_gmt":"2021-03-23T16:17:13","slug":"leetcode-1799-maximize-score-after-n-operations","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-1799-maximize-score-after-n-operations\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1799. Maximize Score After N Operations"},"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 1799. Maximize Score After N Operations - \u5237\u9898\u627e\u5de5\u4f5c 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; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<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>\n\n\n\n<p>In the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;operation&nbsp;<strong>(1-indexed)<\/strong>, you will:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><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>\n\n\n\n<p>Return&nbsp;<em>the maximum score you can receive after performing&nbsp;<\/em><code>n<\/code><em>&nbsp;operations.<\/em><\/p>\n\n\n\n<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>\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 = [1,2]\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong>&nbsp;The optimal choice of operations is:\n(1 * gcd(1, 2)) = 1\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 = [3,4,6,8]\n<strong>Output:<\/strong> 11\n<strong>Explanation:<\/strong>&nbsp;The optimal choice of operations is:\n(1 * gcd(3, 6)) + (2 * gcd(4, 8)) = 3 + 8 = 11\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [1,2,3,4,5,6]\n<strong>Output:<\/strong> 14\n<strong>Explanation:<\/strong>&nbsp;The optimal choice of operations is:\n(1 * gcd(1, 5)) + (2 * gcd(2, 4)) + (3 * gcd(3, 6)) = 1 + 4 + 9 = 14\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><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>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Mask DP<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/03\/1799-ep390.png\"><img loading=\"lazy\" decoding=\"async\" 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=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/figure>\n\n\n\n<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>\n\n\n\n<p>Time complexity: O(n<sup>2<\/sup>*2<sup>2n<\/sup>)<br>Space complexity: O(2<sup>2n<\/sup>)<\/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, 184 ms, 8.3 MB\nclass Solution {\npublic:\n  int maxScore(vector<int>& nums) {\n    const int l = nums.size();\n    vector<int> cache(1 << l);\n    function<int(int)> dp = [&](int mask) {      \n      if (mask == 0) return 0;\n      int& ans = cache[mask];\n      if (ans > 0) return ans;\n      const int k = (l - __builtin_popcount(mask)) \/ 2 + 1;\n      for (int i = 0; i < l; ++i)\n        for (int j = i + 1; j < l; ++j)\n          if ((mask &#038; (1 << i)) &#038;&#038; (mask &#038; (1 << j)))\n            ans = max(ans, k * gcd(nums[i], nums[j])\n                           + dp(mask ^ (1 << i) ^ (1 << j)));\n      return ans;\n    };\n    return dp((1 << l) - 1);\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p>Bottom-Up<\/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, 156 ms, 8.3 MB\nclass Solution {\npublic:\n  int maxScore(vector<int>& nums) {\n    const int l = nums.size();\n    vector<int> dp(1 << l);    \n    for (int mask = 0; mask < 1 << l; ++mask) {\n      int c = __builtin_popcount(mask);\n      if (c &#038; 1) continue; \/\/ only do in pairs\n      int k = c \/ 2 + 1;\n      for (int i = 0; i < l; ++i)\n        for (int j = i + 1; j < l; ++j)\n          if ((mask &#038; (1 << i)) + (mask &#038; (1 << j)) == 0) {            \n            int new_mask = mask | (1 << i) | (1 << j);            \n            dp[new_mask] = max(dp[new_mask],\n                               k * gcd(nums[i], nums[j]) + dp[mask]);\n        }\n    }\n    return dp[(1 << l) - 1];\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given&nbsp;nums, an array of positive integers of size&nbsp;2 * n. You must perform&nbsp;n&nbsp;operations on this array. In the&nbsp;ith&nbsp;operation&nbsp;(1-indexed), you will: Choose two elements,&nbsp;x&nbsp;and&nbsp;y.&#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],"tags":[700,18,217,187],"class_list":["post-8259","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-bitmap","tag-dp","tag-hard","tag-mask","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8259","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=8259"}],"version-history":[{"count":6,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8259\/revisions"}],"predecessor-version":[{"id":8280,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8259\/revisions\/8280"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8259"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8259"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8259"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}