Created
January 5, 2025 18:01
-
-
Save danicunhac/cc61666bec54da12a44d169135fc9b32 to your computer and use it in GitHub Desktop.
Higher Number Game (algorithms)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const game = (arr, k) => { | |
const rec = {}; | |
let winner; | |
while (true) { | |
const st = arr[0]; | |
const nd = arr[1]; | |
if (st >= nd) { | |
arr.push(nd); | |
arr.splice(1, 1); | |
rec[st] ? rec[st]++ : (rec[st] = 1); | |
if (rec[st] === k) { | |
winner = st; | |
break; | |
} | |
} else { | |
arr.push(st); | |
arr.splice(0, 1); | |
rec[nd] ? rec[nd]++ : (rec[nd] = 1); | |
if (rec[nd] === k) { | |
winner = nd; | |
break; | |
} | |
} | |
continue; | |
} | |
return winner; | |
}; | |
console.log(game([2, 3, 5, 7, 1, 4], 2)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment