1121. Grades in Linear Time

Easy1.5 points estimatedArrayCounting Sort3 hints

A school office has to print the results of a national exam as a ranked list. The array grades holds one whole-number score per student, and every score is between 0 and 100 inclusive. Up to a hundred thousand students sat the exam, but the score range is tiny, so the office wants a sort that exploits this fact instead of comparing pairs of scores.

Return a new array with the same scores arranged from the lowest to the highest. Equal scores simply appear next to each other, as often as they occurred in grades.

Example 1

Input:
grades = [88,72,100,72,0]
Output:
[0,72,72,88,100]
Explanation:

The lowest score 0 comes first, then both 72s, then 88 and 100.

Example 2

Input:
grades = [55,55,55]
Output:
[55,55,55]
Explanation:

All scores are equal, so the array is already in order.

Constraints

1 ≤ grades.length ≤ 105

0 ≤ grades[i] ≤ 100

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.

Expected complexity

Time
O(n + k)
Space
O(n + k)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…