
Sponsored
Sponsored
This approach uses backtracking to explore all possible pairs of elements for the operations. The goal is to maximize the sum of scores obtained from each operation. To optimize the solution, pruning is applied by avoiding redundant calculations using a memoization table. By systematically trying all combinations, we gradually eliminate suboptimal solutions and select the maximal score possible.
Time Complexity: O((2n)! / 2^n), the approach explores all possible combinations of pairs.
Space Complexity: O(2^n), for memoization array to store intermediate results.
1function gcd(a, b) {
2 while (b !== 0) {
3 let temp = b;
4 b = a % b;
5 a = temp;
6 }
7 return a;
8}
9
10function backtrack(nums, n, used, currentScore, maxScore, memo) {
11 if (used === (1 << n) - 1) {
12 maxScore[0] = Math.max(maxScore[0], currentScore);
13 return;
14 }
15
16 if (memo[used] >= currentScore) {
17 return;
18 }
19 memo[used] = currentScore;
20
21 for (let i = 0; i < n; ++i) {
22 if (!(used & (1 << i))) {
23 for (let j = i + 1; j < n; ++j) {
24 if (!(used & (1 << j))) {
25 let pairScore = gcd(nums[i], nums[j]) * (bitCount(used) / 2 + 1);
26 backtrack(nums, n, used | (1 << i) | (1 << j), currentScore + pairScore, maxScore, memo);
27 }
28 }
29 }
30 }
31}
32
33function bitCount(num) {
34 let count = 0;
35 while (num !== 0) {
36 count += num & 1;
37 num >>= 1;
38 }
39 return count;
40}
41
42function maxScore(nums) {
43 const n = nums.length / 2;
44 let maxScore = [0];
45 const memo = new Array(1 << nums.length).fill(0);
46 backtrack(nums, n, 0, 0, maxScore, memo);
47 return maxScore[0];
48}
49
50console.log(maxScore([1, 2, 3, 4, 5, 6]));JavaScript implementation uses recursive backtracking with bit manipulation. It aims to maximize scores through attempted pairing and memo storage.
This approach leverages dynamic programming along with bit masking to compute the maximum score possible. The idea is to use a DP state to remember the best score possible with a certain combination of pairs already taken. The state is determined by a bitmask which indicates the taken pairs. Recursively calculate the maximum possible score, storing intermediate results to avoid repeated calculations.
Time Complexity: O(n^2 * 2^n), where n is the number of elements in 'nums'.
Space Complexity: O(2^n) for the DP array.
public class MaxScore {
public static int GCD(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
public static int MaxScore(int[] nums) {
int n = nums.Length / 2;
int[] dp = new int[1 << nums.Length];
Array.Fill(dp, int.MinValue);
dp[0] = 0;
for (int mask = 0; mask < (1 << nums.Length); ++mask) {
int ops = BitCount(mask) / 2 + 1;
for (int i = 0; i < nums.Length; ++i) {
if ((mask & (1 << i)) == 0) {
for (int j = i + 1; j < nums.Length; ++j) {
if ((mask & (1 << j)) == 0) {
int new_mask = mask | (1 << i) | (1 << j);
dp[new_mask] = Math.Max(dp[new_mask], dp[mask] + ops * GCD(nums[i], nums[j]));
}
}
}
}
}
return dp[(1 << nums.Length) - 1];
}
public static int BitCount(int n) {
int count = 0;
while (n != 0) {
count += n & 1;
n >>= 1;
}
return count;
}
public static void Main() {
int[] nums = {1, 2, 3, 4, 5, 6};
Console.WriteLine("Max Score: " + MaxScore(nums));
}
}C# employs DP using bitmasking to store and update scores for different sets of paired selections, aiming to capture the maximal achievable score.