Ad platform~15 min2s · 256 MBtarget O(n + k) time, O(k) spaceclassic twin: lc-506sorting
An ad platform runs a weekly contest between campaigns. Each campaign has a score, and no two campaigns have the same score. The leaderboard page shows each campaign's place: Gold, Silver and Bronze for the top three, and the place number as text, such as 4, for the rest. The page lists the campaigns in the order they were created, not in score order.
Write label_the_ranks(scores).
scores[i] is the score of campaign i. All scores are different.labels, where labels[i] is the label of campaign i.Gold for place 1, Silver for place 2 and Bronze for place 3. For the rest, use the place number as text.Input: scores = [40, 90, 70, 10, 55]
Output: ["4", "Gold", "Silver", "5", "Bronze"]Order the scores from highest to lowest and number the places. Then put each label back at the campaign that earned it.
| Score | Rank | Label |
|---|---|---|
| 90 | 1 | Gold |
| 70 | 2 | Silver |
| 55 | 3 | Bronze |
| 40 | 4 | 4 |
| 10 | 5 | 5 |
Campaign 0 scored 40, which is place 4. The labels in creation order are ["4", "Gold", "Silver", "5", "Bronze"].
Input: scores = [7]
Output: ["Gold"]One campaign is in first place, so the answer is ["Gold"].
Input: scores = [1, 3, 2]
Output: ["Bronze", "Gold", "Silver"]The labels follow the input order, not the score order. The answer is ["Bronze", "Gold", "Silver"].
0 ≤ scores.length ≤ 100,000 (campaigns)0 ≤ scores[i] ≤ 1,000,000, and all scores are different.The scores come from 1,000,001 possible values. An array with one place per score is small enough, so you can place each campaign by score and avoid comparing scores at all.