An arcade game player wants to climb to the top of the leaderboard and track their 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.
Example:
Input: n = 6 , ranked[] = {100, 90, 90, 80, 75, 60}, m =5, player[] = {50, 65, 77, 90, 102}
Output:
6 5 4 2 1
Approach
Java
import java.util.Vector;public class ClimbingLeaderboard {public static void main(String[] args) {int n = 6;int ranked[] = { 100, 90, 90, 80, 75, 60 };int m = 5;int player[] = { 50, 65, 77, 90, 102 };Vector<int[]> v = new Vector<int[]>();int i = 1;int rank = 1;while (i < n) {while (i < n && ranked[i] == ranked[i - 1]) {int e[] = { ranked[i - 1], rank };v.add(e);i++;}int e[] = { ranked[i - 1], rank };v.add(e);rank++;i++;}int e[] = { ranked[i - 1], rank };v.add(e);int j = n - 1;for (int i1 = 0; i1 < m; i1++) {if (player[i1] == v.get(j)[0]) {System.out.println(v.get(j)[1]);} else if (player[i1] < v.get(j)[0]) {System.out.println(v.get(j)[1] + 1);} else {while (j >= 0 && v.get(j)[0] < player[i1])j--;if (j >= 0) {if (player[i1] == v.get(j)[0])System.out.println(v.get(j)[1]);elseSystem.out.println(v.get(j)[1] + 1);} elseSystem.out.println(1);}}}}
C++
#include <bits/stdc++.h>using namespace std;int main(){int n = 6;int ranked[n] = {100, 90, 90, 80, 75, 60};int m = 5;int player[m] = {50, 65, 77, 90, 102};vector<pair<int, int>> v;int i = 1;int rank = 1;while (i < n){while (i < n && ranked[i] == ranked[i - 1]){v.push_back({ranked[i - 1], rank});i++;}v.push_back({ranked[i - 1], rank});rank++;i++;}v.push_back({ranked[i - 1], rank});int j = n - 1;for (int i = 0; i < m; i++){if (player[i] == v[j].first){cout << v[j].second << "\n";}else if (player[i] < v[j].first){cout << v[j].second + 1 << "\n";}else{while (j >= 0 && v[j].first < player[i])j--;if (j >= 0){if (player[i] == v[j].first)cout << v[j].second << "\n";elsecout << v[j].second + 1 << "\n";}elsecout << 1 << "\n";}}return 0;}
No comments:
Post a Comment