Skip to content

Commit f2291a2

Browse files
committed
New Problem Solution -"Maximize Score After N Operations"
1 parent 2aa068b commit f2291a2

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ LeetCode
1313
|1802|[Maximum Value at a Given Index in a Bounded Array](https://leetcode.com/problems/maximum-value-at-a-given-index-in-a-bounded-array/) | [C++](./algorithms/cpp/maximumValueAtAGivenIndexInABoundedArray/MaximumValueAtAGivenIndexInABoundedArray.cpp)|Medium|
1414
|1801|[Number of Orders in the Backlog](https://leetcode.com/problems/number-of-orders-in-the-backlog/) | [C++](./algorithms/cpp/numberOfOrdersInTheBacklog/NumberOfOrdersInTheBacklog.cpp)|Medium|
1515
|1800|[Maximum Ascending Subarray Sum](https://leetcode.com/problems/maximum-ascending-subarray-sum/) | [C++](./algorithms/cpp/maximumAscendingSubarraySum/MaximumAscendingSubarraySum.cpp)|Easy|
16+
|1799|[Maximize Score After N Operations](https://leetcode.com/problems/maximize-score-after-n-operations/submissions/) | [C++](./algorithms/cpp/maximizeScoreAfterNOperations/MaximizeScoreAfterNOperations.cpp)|Hard|
1617
|1798|[Maximum Number of Consecutive Values You Can Make](https://leetcode.com/problems/maximum-number-of-consecutive-values-you-can-make/submissions/) | [C++](./algorithms/cpp/maximumNumberOfConsecutiveValuesYouCanMake/MaximumNumberOfConsecutiveValuesYouCanMake.cpp)|Medium|
1718
|1797|[Design Authentication Manager](https://leetcode.com/problems/design-authentication-manager/) | [C++](./algorithms/cpp/designAuthenticationManager/DesignAuthenticationManager.cpp)|Medium|
1819
|1796|[Second Largest Digit in a String](https://leetcode.com/problems/second-largest-digit-in-a-string/) | [C++](./algorithms/cpp/secondLargestDigitInAString/SecondLargestDigitInAString.cpp)|Easy|
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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

Comments
 (0)