Payments app~15 min2s · 256 MBtarget O(n log n) time, O(n) spacesorting
A payments app lists the payouts of the day. Each row has a merchant name and the units paid to that merchant. Merchants can appear more than once.
The report shows the largest payout first. When two rows have the same units, the one with the earlier name comes first. Your team lead says: "The Python job, the Java service, the C++ tool and the JavaScript page must all print the same order."
Write rank_payouts(names, units).
i has the merchant name names[i] and the payout units[i].a before b, and a shorter name before a longer one that starts with it.Input: names = ["tess", "ravi", "omar", "ravi", "jo"], units = [300, 500, 300, 500, 100]
Output: ["ravi", "ravi", "omar", "tess", "jo"]Rows with 500 units come first. Then the rows with 300 units, ordered by name: omar before tess. The row with 100 units is last.
| Row | Name | Units | Key (-units, name) |
|---|---|---|---|
| 0 | tess | 300 | (-300, tess) |
| 1 | ravi | 500 | (-500, ravi) |
| 2 | omar | 300 | (-300, omar) |
| 3 | ravi | 500 | (-500, ravi) |
| 4 | jo | 100 | (-100, jo) |
Input: names = [], units = []
Output: []There are no rows, so the answer is an empty list.
Input: names = ["bo", "al"], units = [7, 7]
Output: ["al", "bo"]The units are equal, so the names decide.
0 ≤ names.length = units.length ≤ 100,000 (payout rows)1 ≤ names[i].length ≤ 12, letters a to z only0 ≤ units[i] ≤ 1,000,000,000Insertion with a two-key test makes about n × n / 4 comparisons, which is 2,499,975,000 at n = 100,000. A library sort with a key or comparator makes about n log n, roughly 1.5 million comparisons for 100,000 rows.