-
Notifications
You must be signed in to change notification settings - Fork 0
/
Climbing the Leaderboard.js
234 lines (203 loc) · 5.43 KB
/
Climbing the 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
//After a long time
2 wrongs && few time outs
let i = [100, 90, 90, 80, 75, 60]
let j = [50, 65, 77, 90, 102]
let x = [...new Set(i)].sort((a,b)=> b-a), ar = []
var flow = 1;
var flow1 = 1;
j.map((v)=> {x.forEach((s,i)=>{
if(v >= x[0] && flow){
ar.push(1)
flow = 0;
}
if(v < x[x.length-1] && flow1){
ar.push(x.length+1)
flow1 = 0;
}
if(v >= s && v < x[i-1]){
ar.push(i+1)
}
})})
return ar;
function climbingLeaderboard(ranked, player) {
// Write your code here
let arr = [...new Set(ranked)];
let len = arr.length;
let currScore = 0;
let res = [];
for(let i=0;i<player.length; i++) {
currScore=player[i];
if(currScore>=arr[0]) {
res.push(1);
continue;
} else if(currScore<arr[len-1]) {
res.push(len+1);
continue;
} else if(currScore===arr[len-1]) {
res.push(len);
continue;
}
let flag = false;
let l=0,h=len-1, m = Math.ceil((l+h)/2);
while(l<=h) {
if(currScore === arr[m]) {
flag = true;
res.push(m+1);
break;
} else if(currScore > arr[m]) {
h = m-1;
} else {
l = m+1;
}
m= Math.ceil((l+h)/2);
}
if(!flag) res.push(m+1);
}
return res;
}
function climbingLeaderboard(scores, alice) {
console.log(scores, alice);
let positions = [];
let distScore = [...new Set(scores)];
let aliceIndex = 0;
let i = distScore.length - 1;
alice.forEach(element => {
while (i >= 0) {
if (element >= distScore[i]) {
i--;
} else {
positions.push(i + 2);
break;
}
}
if (i < 0) positions.push(1);
});
return positions;
}
function climbingLeaderboard(ranked, player) {
const uniqueRanked = [...new Set(ranked)];
const result = [];
let j = uniqueRanked.length - 1;
for (let p in player) {
while (uniqueRanked[j] <= player[p]) {
j--;
}
result.push(j + 2);
}
return result;
}
// Complete the climbingLeaderboard function below.
function climbingLeaderboard(scores, alice) {
// Complete this function
let values = [...new Set(scores)];
let rank = values.length - 1;
return alice.reduce((target, item, index) => {
while (item > values[rank] && rank > 0) rank--;
target.push((item >= values[rank]) ? rank + 1 : rank + 2);
return target;
}, []);
}
function climbingLeaderboard(ranked, player) {
const result = [];
const newArr = [...new Set(ranked)];
for(let score of player){
result.push(lowerBound(newArr, score) + 1);
}
return result;
}
const lowerBound = (arr, target) => {
let left = 0;
let right = arr.length;
while(left < right){
const mid = Math.floor((left + right) / 2);
if(arr[mid] <= target) right = mid;
else left = mid + 1;
}
function climbingLeaderboard(ranked, player) {
const result = [];
const newArr = [...new Set(ranked)];
for(let score of player){
result.push(lowerBound(newArr, score) + 1);
}
return result;
}
const lowerBound = (arr, target) => {
let left = 0;
let right = arr.length;
while(left < right){
const mid = Math.floor((left + right) / 2);
if(arr[mid] <= target) right = mid;
else left = mid + 1;
}
//Using stacks
function climbingLeaderboard(scores, alice) {
// get the unique ranks sorted descending
scores = Array.from(new Set(scores));
const player_ranks = [];
for (const score of alice) {
while (scores && score >= scores[scores.length - 1])
scores.pop()
player_ranks.push(scores.length + 1)
}
return player_ranks
function climbingLeaderboard(scores, alice) {
// get the unique ranks sorted descending
scores = scores.reduce((ranking, score) => {
if (score !== ranking[ranking.length - 1]) {
ranking.push(score);
}
return ranking;
}, [scores[0]]);
const player_ranks = [];
for (const score of alice) {
while (scores && score >= scores[scores.length - 1])
scores.pop()
player_ranks.push(scores.length + 1)
}
//Git
function climbingLeaderboard(scores, alice) {
let rank = [];
for (let i = 0; i < scores.length; i++) {
if (rank.length === 0) {
rank.push(scores[i]);
} else {
if (rank[rank.length - 1] === scores[i]) {
continue;
}
rank.push(scores[i]);
}
}
let res = [];
let lowestRank = rank.length;
for (let i = 0; i < alice.length; i++) {
while (alice[i] >= rank[lowestRank - 1] && lowestRank > 0) {
rank.pop;
lowestRank--;
}
res.push(lowestRank + 1);
}
return res;
}
}
//Using binary tree
Function rankBinarySearch(score, uniqueScores) {
let start = 0;
let end = uniqueScores.length - 1;
while (true) {
let mid = Math.floor((start + end) / 2);
// base cases
if (uniqueScores[mid] === score) {
return mid + 1;
} else if (uniqueScores[mid] > score && uniqueScores[mid + 1] < score) {
return mid + 2;
} else if (uniqueScores[mid] < score && uniqueScores[mid - 1 > score]) {
return mid - 1;
}
// recursion
if (score < uniqueScores[mid]) {
start = mid + 1;
} else {
end = mid - 1;
}
}
}