|
| 1 | +// Source : https://leetcode.com/problems/maximize-score-after-n-operations/submissions/ |
| 2 | +// Author : Hao Chen |
| 3 | +// Date : 2021-03-23 |
| 4 | + |
| 5 | +/***************************************************************************************************** |
| 6 | + * |
| 7 | + * You are given nums, an array of positive integers of size 2 * n. You must perform n operations on |
| 8 | + * this array. |
| 9 | + * |
| 10 | + * In the i^th operation (1-indexed), you will: |
| 11 | + * |
| 12 | + * Choose two elements, x and y. |
| 13 | + * Receive a score of i * gcd(x, y). |
| 14 | + * Remove x and y from nums. |
| 15 | + * |
| 16 | + * Return the maximum score you can receive after performing n operations. |
| 17 | + * |
| 18 | + * The function gcd(x, y) is the greatest common divisor of x and y. |
| 19 | + * |
| 20 | + * Example 1: |
| 21 | + * |
| 22 | + * Input: nums = [1,2] |
| 23 | + * Output: 1 |
| 24 | + * Explanation: The optimal choice of operations is: |
| 25 | + * (1 * gcd(1, 2)) = 1 |
| 26 | + * |
| 27 | + * Example 2: |
| 28 | + * |
| 29 | + * Input: nums = [3,4,6,8] |
| 30 | + * Output: 11 |
| 31 | + * Explanation: The optimal choice of operations is: |
| 32 | + * (1 * gcd(3, 6)) + (2 * gcd(4, 8)) = 3 + 8 = 11 |
| 33 | + * |
| 34 | + * Example 3: |
| 35 | + * |
| 36 | + * Input: nums = [1,2,3,4,5,6] |
| 37 | + * Output: 14 |
| 38 | + * Explanation: The optimal choice of operations is: |
| 39 | + * (1 * gcd(1, 5)) + (2 * gcd(2, 4)) + (3 * gcd(3, 6)) = 1 + 4 + 9 = 14 |
| 40 | + * |
| 41 | + * Constraints: |
| 42 | + * |
| 43 | + * 1 <= n <= 7 |
| 44 | + * nums.length == 2 * n |
| 45 | + * 1 <= nums[i] <= 10^6 |
| 46 | + ******************************************************************************************************/ |
| 47 | + |
| 48 | +class Solution { |
| 49 | +private: |
| 50 | + // Euclidean algorithm |
| 51 | + // https://en.wikipedia.org/wiki/Euclidean_algorithm |
| 52 | + int gcd(int a, int b) { |
| 53 | + while(a != b) { |
| 54 | + if(a > b) a = a - b; |
| 55 | + else b = b - a; |
| 56 | + } |
| 57 | + return a; |
| 58 | + } |
| 59 | + unordered_map<int, int> cache; |
| 60 | +public: |
| 61 | + int maxScore(vector<int>& nums) { |
| 62 | + int n = nums.size(); |
| 63 | + |
| 64 | + vector<vector<int>> pair_gcd(n, vector<int>(n, 0) ); |
| 65 | + |
| 66 | + for (int i=0; i< n - 1; i++) { |
| 67 | + for (int j=i+1; j < n; j++ ) { |
| 68 | + pair_gcd[i][j] = gcd(nums[i], nums[j]); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + // used_mark[] - remember the num has been used. |
| 73 | + return maxScore(pair_gcd, 0, n, n/2); |
| 74 | + } |
| 75 | + |
| 76 | + int maxScore(vector<vector<int>>& pair_gcd, int mask, int n, int step) { |
| 77 | + if (cache.find(mask) != cache.end()) { |
| 78 | + return cache[mask]; |
| 79 | + } |
| 80 | + int m = 0; |
| 81 | + |
| 82 | + for (int i=0; i< n - 1; i++) { |
| 83 | + if ( (1<<i) & mask ) continue; |
| 84 | + for (int j=i+1; j < n; j++ ) { |
| 85 | + if ((1<<j) & mask) continue; |
| 86 | + if (step == 1) { |
| 87 | + return pair_gcd[i][j]; |
| 88 | + } |
| 89 | + |
| 90 | + m = max(m, step * pair_gcd[i][j] + |
| 91 | + maxScore(pair_gcd, mask | (1<<i) | (1<<j), n, step-1)); |
| 92 | + |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + cache[mask] = m; |
| 97 | + return m; |
| 98 | + } |
| 99 | +}; |
0 commit comments