Department leaderboard
MediumOnline Assessment
Build the quarterly bonus list for a company's sales departments: the top tiers of each department, with ties sharing a tier.
Each record is a string "dept name amount": a department, a salesperson, and an integer amount (negative for a refund), separated by single spaces. Departments and names are lowercase letters only. A person's total is the sum of their amounts within one department; someone who appears in two departments has a separate total in each.
Only people with a total greater than 0 are ranked. Within each department, rank them by total descending in two ways:
- dense rank: the best total is 1, the next different total is 2, and so on, without gaps;
- standard rank: 1 plus the number of people in that department with a strictly higher total (so totals 90, 90, 80 have standard ranks 1, 1, 3, but dense ranks 1, 1, 2).
Everyone whose dense rank is at most tiers gets a bonus and is listed, so a tier can hold several people.
Input: records, a list of strings in the format above, and tiers, an integer.
Output: a list of strings "dept name total dense standard", one per person who gets a bonus, ordered by department ascending, then dense rank ascending, then name ascending. If nobody qualifies, return [].
Example 1
- Input:
- [["sales ana 40","sales bo 90","sales ana 50","sales cy 80","ops di 5","ops di -2"],2]
- Output:
- ["ops di 3 1 1","sales ana 90 1 1","sales bo 90 1 1","sales cy 80 2 3"]
- Explanation
- In sales ana totals 40 + 50 = 90, bo has 90 and cy has 80. Ana and bo share dense and standard rank 1; cy is dense rank 2 but standard rank 3 because two people are above. In ops di totals 5 - 2 = 3, the only ranked person there. All have dense rank at most 2.
Example 2
- Input:
- [["sales ed 50","sales ed -50","support fay 40","sales ana 70","sales bo 60","sales cy 55"],1]
- Output:
- ["sales ana 70 1 1","support fay 40 1 1"]
- Explanation
- ed sells 50 and refunds 50, so the total is 0 and ed is not ranked at all. With tiers = 1 only dense rank 1 is listed: ana (70) in sales, whereas bo (60, dense rank 2) and cy (55, dense rank 3) are not, and fay in support, who is alone with 40.
Example 3
- Input:
- [["sales ana 10","ops ana 100","sales cy 25","sales bo 10","sales di 10","ops eve 100","ops fin 7","sales gus 3"],2]
- Output:
- ["ops ana 100 1 1","ops eve 100 1 1","ops fin 7 2 3","sales cy 25 1 1","sales ana 10 2 2","sales bo 10 2 2","sales di 10 2 2"]
- Explanation
- ana has separate totals: 10 in sales and 100 in ops. In ops ana and eve tie at 100 (rank 1 and 1), and fin with 7 is dense rank 2 but standard rank 3. In sales cy (25) is first; ana, bo and di tie at 10, all dense rank 2 and standard rank 2 (one person is above them), and are ordered by name. gus (3) is dense rank 3 and is not listed.
Constraints
0 <= records.length <= 3,0000 <= tiers <= 20- Departments and names are 1 to 8 lowercase letters;
-1,000,000 <= amount <= 1,000,000