-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclimbing-leaderboard.js
52 lines (42 loc) · 1.01 KB
/
climbing-leaderboard.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
function climbingLeaderboard(scores, alice) {
var score_map = new Map();
var rank = 1
for (let i = 0; i < scores.length; i++) {
if (!score_map.has(scores[i])) {
score_map.set(scores[i], rank)
rank++;
}
}
var arr = new Array();
score_map.forEach((rank, score) => {
arr.unshift({score, rank})
});
console.log(arr)
var alice_rank = new Array();
var score_pos = 0;
for (let i = 0; i < alice.length; i++) {
var found = false;
while (!found) {
// console.log(arr[score_pos])
if (!arr[score_pos]) {
// console.log("HIT")
alice_rank.push(1);
found = !found
} else {
if (alice[i] < arr[score_pos].score) {
alice_rank.push(arr[score_pos].rank + 1);
found = !found
} else if (alice[i] === arr[score_pos].score) {
alice_rank.push(arr[score_pos].rank);
found = !found
} else {
score_pos++;
}
}
}
}
return alice_rank
}
var scores = [100, 100, 50, 40, 40, 20, 10]
var alice = [5, 25, 50, 120]
console.log(climbingLeaderboard(scores, alice))