Skip to content

Commit 989c1bf

Browse files
authored
Create 1823-find-the-winner-of-the-circular-game.js
1 parent 860df66 commit 989c1bf

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @param {number} n
3+
* @param {number} k
4+
* @return {number}
5+
*/
6+
const findTheWinner = function(n, k) {
7+
const arr = Array(n).fill(0)
8+
for(let i = 0; i < n; i++) arr[i] = i + 1
9+
let idx = 0
10+
while(arr.length > 1) {
11+
idx = (idx + k - 1) % arr.length
12+
arr.splice(idx, 1)
13+
}
14+
return arr.length ? arr[0] : -1
15+
};
16+

0 commit comments

Comments
 (0)