|
| 1 | +# [1799. Maximize Score After N Operations](https://leetcode.com/problems/maximize-score-after-n-operations) |
| 2 | + |
| 3 | +## Description |
| 4 | + |
| 5 | +<div class="elfjS" data-track-load="description_content"><p>You are given <code>nums</code>, an array of positive integers of size <code>2 * n</code>. You must perform <code>n</code> operations on this array.</p> |
| 6 | + |
| 7 | +<p>In the <code>i<sup>th</sup></code> operation <strong>(1-indexed)</strong>, you will:</p> |
| 8 | + |
| 9 | +<ul> |
| 10 | + <li>Choose two elements, <code>x</code> and <code>y</code>.</li> |
| 11 | + <li>Receive a score of <code>i * gcd(x, y)</code>.</li> |
| 12 | + <li>Remove <code>x</code> and <code>y</code> from <code>nums</code>.</li> |
| 13 | +</ul> |
| 14 | + |
| 15 | +<p>Return <em>the maximum score you can receive after performing </em><code>n</code><em> operations.</em></p> |
| 16 | + |
| 17 | +<p>The function <code>gcd(x, y)</code> is the greatest common divisor of <code>x</code> and <code>y</code>.</p> |
| 18 | + |
| 19 | +<p> </p> |
| 20 | +<p><strong class="example">Example 1:</strong></p> |
| 21 | + |
| 22 | +<pre><strong>Input:</strong> nums = [1,2] |
| 23 | +<strong>Output:</strong> 1 |
| 24 | +<strong>Explanation:</strong> The optimal choice of operations is: |
| 25 | +(1 * gcd(1, 2)) = 1 |
| 26 | +</pre> |
| 27 | + |
| 28 | +<p><strong class="example">Example 2:</strong></p> |
| 29 | + |
| 30 | +<pre><strong>Input:</strong> nums = [3,4,6,8] |
| 31 | +<strong>Output:</strong> 11 |
| 32 | +<strong>Explanation:</strong> The optimal choice of operations is: |
| 33 | +(1 * gcd(3, 6)) + (2 * gcd(4, 8)) = 3 + 8 = 11 |
| 34 | +</pre> |
| 35 | + |
| 36 | +<p><strong class="example">Example 3:</strong></p> |
| 37 | + |
| 38 | +<pre><strong>Input:</strong> nums = [1,2,3,4,5,6] |
| 39 | +<strong>Output:</strong> 14 |
| 40 | +<strong>Explanation:</strong> The optimal choice of operations is: |
| 41 | +(1 * gcd(1, 5)) + (2 * gcd(2, 4)) + (3 * gcd(3, 6)) = 1 + 4 + 9 = 14 |
| 42 | +</pre> |
| 43 | + |
| 44 | +<p> </p> |
| 45 | +<p><strong>Constraints:</strong></p> |
| 46 | + |
| 47 | +<ul> |
| 48 | + <li><code>1 <= n <= 7</code></li> |
| 49 | + <li><code>nums.length == 2 * n</code></li> |
| 50 | + <li><code>1 <= nums[i] <= 10<sup>6</sup></code></li> |
| 51 | +</ul> |
| 52 | +</div> |
| 53 | + |
| 54 | +<p> </p> |
| 55 | + |
| 56 | +## Solutions |
| 57 | + |
| 58 | +**Solution: `Dynamic Programming + Bit Manipulation`** |
| 59 | + |
| 60 | +- Time complexity: <em>O(2<sup>n</sup>\*n<sup>2</sup>)</em> |
| 61 | +- Space complexity: <em>O(2<sup>n</sup>)</em> |
| 62 | + |
| 63 | +<p> </p> |
| 64 | + |
| 65 | +### **JavaScript** |
| 66 | + |
| 67 | +```js |
| 68 | +/** |
| 69 | + * @param {number[]} nums |
| 70 | + * @return {number} |
| 71 | + */ |
| 72 | +const maxScore = function (nums) { |
| 73 | + const n = nums.length; |
| 74 | + const totalMask = (1 << n) - 1; |
| 75 | + const dp = Array.from({ length: totalMask + 1 }, () => -1); |
| 76 | + |
| 77 | + const gcd = (a, b) => (b ? gcd(b, a % b) : a); |
| 78 | + |
| 79 | + const receiveScore = (mask, i) => { |
| 80 | + if (mask === totalMask) return 0; |
| 81 | + if (dp[mask] !== -1) return dp[mask]; |
| 82 | + let result = 0; |
| 83 | + |
| 84 | + for (let a = 0; a < n - 1; a++) { |
| 85 | + if (mask & (1 << a)) continue; |
| 86 | + |
| 87 | + for (let b = a + 1; b < n; b++) { |
| 88 | + if (mask & (1 << b)) continue; |
| 89 | + const score = i * gcd(nums[a], nums[b]); |
| 90 | + const nextMask = mask | (1 << a) | (1 << b); |
| 91 | + const totalScore = score + receiveScore(nextMask, i + 1); |
| 92 | + |
| 93 | + result = Math.max(totalScore, result); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + dp[mask] = result; |
| 98 | + |
| 99 | + return result; |
| 100 | + }; |
| 101 | + |
| 102 | + return receiveScore(0, 1); |
| 103 | +}; |
| 104 | +``` |
0 commit comments