Climbing the Leaderboard

Climbing the Leaderboard

Alice is playing an arcade game and wants to climb to the top of the leaderboard and wants to track her ranking. The game uses Dense Ranking, so its leaderboard works like this:

  • The player with the highest score is ranked number 1 on the leaderboard.
  • Players who have equal scores receive the same ranking number, and the next player(s) receive the immediately following ranking number.

For example, the four players on the leaderboard have high scores of 100, 90, 90, and 80. Those players will have ranks 1, 2, 2, and 3, respectively. If Alice’s scores are 70, 80 and 105, her rankings after each game are 4th, 3rd and 1st.

Function Description

Complete the climbingLeaderboard function in the editor below. It should return an integer array where each element res[j] represents Alice’s rank after the jth game.

climbingLeaderboard has the following parameter(s):

  • scores: an array of integers that represent leaderboard scores
  • alice: an array of integers that represent Alice’s scores

Input Format

The first line contains an integer n, the number of players on the leaderboard.
The next line contains n space-separated integers scores[i], the leaderboard scores in decreasing order.
The next line contains an integer, m, denoting the number games Alice plays.
The last line contains m space-separated integers alice[j], the game scores.

Constraints

  • 1 <= n <= 2 X 105
  • 1 <= m <= 2 X 105
  • 0 <= scores[i] <= 109 for 0 <= i < n
  • 0 <= alice[j] <= 109 for 0 <= j < m
  • The existing leaderboard, scores, is in descending order.
  • Alice’s scores, alice, are in ascending order.

Subtask

For 60% of the maximum score:

  • 1 <= n <= 200
  • 1 <= m <= 200

Output Format

Print m integers. The jth integer should indicate Alice’s rank after playing the jth game.

Sample Input 1

| ‘Array: scores’ |
| — | — | — | — | — | — | — |
| 100 | 100 | 50 | 40 | 40 | 20 | 10 |

| ‘Array: alice’ |
| — | — | — | — |
| 5 | 25 | 50 | 120 |

1
2
3
4
7
100 100 50 40 40 20 10
4
5 25 50 120

Sample Output 1

1
2
3
4
6
4
2
1

Explanation 1

Alice starts playing with 7 players already on the leaderboard, which looks like this:

After Alice finishes game 0, her score is 5 and her ranking is 6:

After Alice finishes game 1, her score is 25 and her ranking is 4:

After Alice finishes game 2, her score is 50 and her ranking is tied with Caroline at 2:

After Alice finishes game 3, her score is 120 and her ranking is 1:

Sample Input 2

| ‘Array: scores’ |
| — | — | — | — | — | — |
| 100 | 90 | 90 | 80 | 75 | 60 |

| ‘Array: alice’ |
| — | — | — | — | — |
| 50 | 65 | 77 | 90 | 102 |

1
2
3
4
6
100 90 90 80 75 60
5
50 65 77 90 102

Sample Output 2

1
2
3
4
5
6
5
4
2
1

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 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;
}, []);
}
Buy Me A Coffee

Comments

10Days of JS 30Days of Code Algorithm Android Debug Bridge Android Debugging Basic for Web Blog Browsers Chrome으로 Android Debugging 방법 Correctness and the Loop Invariant hackerrank solution in javascript Debug Tools Development Environment in MacOS ES6 Front-End Funny String of Algorithms hackerrank solution in javascript Funny String of Algorithms hackerrank solution in typescript Generator Github Page with Hexo Github Pages HackerRank HackerRank in a String of Algorithms hackerrank solution in javascript HackerRank in a String of Algorithms hackerrank solution in typescript Hexo Hexo Icarus theme Hexo 블로그 만들기 Hexo+Github How Browsers work Insertion Sort - Part 1 hackerrank solution in javascript Insertion Sort - Part 2 hackerrank solution in javascript JS Library JavaScript Level1 Level2 Loops MacOS 개발 환경 설정하기 Mobile web Debugging Node.js Pangrams of Algorithms hackerrank solution in javascript Pangrams of Algorithms hackerrank solution in typescript Problem Solving Programmers Quicksort 1 - Partition hackerrank solution in javascript React RoadMap Running Time of Algorithms hackerrank solution in javascript Safari Debugging Safari Technology Preview Settings Sorting String Strings Strong Password of Algorithms hackerrank solution in javascript TypeScript blog iPhone Safari Debugging 방법 insertion sort 모바일 웹 디버깅 아이폰 사파리를 디버깅하는 방법 안드로이드 디버그 브리지 안드로이드 디버깅하는 방법
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×