Skip to content

Commit 57bf73c

Browse files
authored
Create 2836-maximize-value-of-function-in-a-ball-passing-game.js
1 parent c03f932 commit 57bf73c

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
const N = 34
2+
const checkIthBit64 = (x, i) => {
3+
let s = x.toString(2),
4+
n = s.length
5+
for (let j = 0; j < n; j++) {
6+
if (n - j - 1 == i && s[j] == '1') return 1
7+
}
8+
return 0
9+
}
10+
11+
/**
12+
* @param {number[]} receiver
13+
* @param {number} k
14+
* @return {number}
15+
*/
16+
var getMaxFunctionValue = function(receiver, k) {
17+
const a = receiver
18+
let n = a.length,
19+
ia = [],
20+
res = [],
21+
iaP = [...a],
22+
resP = [...a]
23+
for (let i = 0; i < n; i++) {
24+
ia.push(i)
25+
res.push(i)
26+
}
27+
for (let i = 0; i < N; i++) {
28+
if (checkIthBit64(k, i)) {
29+
;[res, ia] = update(res, resP, ia, iaP)
30+
}
31+
resP = updateResP(res, resP, iaP)
32+
iaP = updateIaP(iaP)
33+
}
34+
return Math.max(...res)
35+
};
36+
37+
const update = (cur, curP, ia, iaP) => {
38+
let n = cur.length,
39+
nextRes = [],
40+
nextPos = []
41+
for (let i = 0; i < n; i++) {
42+
nextRes.push(cur[i] + curP[ia[i]])
43+
nextPos.push(ia[iaP[i]])
44+
}
45+
return [nextRes, nextPos]
46+
}
47+
48+
const updateResP = (cur, curP, iaP) => {
49+
let n = cur.length,
50+
next = []
51+
for (let i = 0; i < n; i++) next.push(curP[i] + curP[iaP[i]])
52+
return next
53+
}
54+
55+
const updateIaP = (iaP) => {
56+
let n = iaP.length,
57+
next = []
58+
for (let i = 0; i < n; i++) next.push(iaP[iaP[i]])
59+
return next
60+
}

0 commit comments

Comments
 (0)