forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStone Game VI.js
28 lines (27 loc) · 924 Bytes
/
Stone Game VI.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Runtime: 135 ms (Top 100.00%) | Memory: 65.6 MB (Top 100.00%)
var stoneGameVI = function(aliceValues, bobValues) {
let aliceVal = 0
let bobVal = 0
let turn = true
const combined = {}
let n = aliceValues.length
for (let i = 0; i < n; i++) {
if (combined[aliceValues[i] + bobValues[i]]) {
combined[aliceValues[i] + bobValues[i]].push({value: aliceValues[i] + bobValues[i], id: i})
} else {
combined[aliceValues[i] + bobValues[i]] = [{value: aliceValues[i] + bobValues[i], id: i}]
}
}
Object.values(combined).reverse().forEach((value) => {
value.forEach(val => {
if (turn) {
aliceVal += aliceValues[val.id]
} else {
bobVal += bobValues[val.id]
}
turn = !turn
})
})
if (aliceVal === bobVal) return 0
return aliceVal > bobVal ? 1 : -1
};