Skip to content

Commit bb76d23

Browse files
authored
Update 1799-maximize-score-after-n-operations.js
1 parent 8ff315b commit bb76d23

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

1799-maximize-score-after-n-operations.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,43 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
const maxScore = function (nums) {
6+
const len = nums.length
7+
const n = len / 2
8+
const allMask = 2 ** 14 - 1
9+
const memo = Array.from({ length: n + 1 }, () => Array())
10+
11+
return helper(1, 0)
12+
13+
function helper(op, mask) {
14+
if(op > n) return 0
15+
if(memo[op][mask]) return memo[op][mask]
16+
17+
let res = 0
18+
for(let i = 0; i < len; i++) {
19+
const a = nums[i]
20+
for(let j = i + 1; j < len; j++) {
21+
const b = nums[j]
22+
const newMask = (1 << i) + (1 << j)
23+
if((mask & newMask) === 0) {
24+
res = Math.max(res, op * gcd(a, b) + helper(op + 1, mask | newMask))
25+
}
26+
}
27+
}
28+
// console.log(res)
29+
memo[op][mask] = res
30+
31+
return memo[op][mask]
32+
}
33+
}
34+
35+
function gcd(a, b) {
36+
return b === 0 ? a : gcd(b, a % b)
37+
}
38+
39+
// another
40+
141
/**
242
* @param {number[]} nums
343
* @return {number}

0 commit comments

Comments
 (0)