Preference Title Sort
EasySorting · Netflix
You are ranking a row of titles on a user's home screen.
titles is a list of distinct title id strings. preferences is a list of events [title_id, points], where points is an integer that may be positive, zero, or negative. A title's score is the sum of points over all of its events; a title with no events scores 0. Events for ids that are not in titles are ignored.
Return titles reordered by score, highest first. When two titles have the same score, the one whose id is smaller in ordinary string comparison (Python's <, so "B" < "a" and "10" < "9") comes first.
Example 1
- Input:
- titles = ["heat", "dune", "up", "jaws"], preferences = [["up", 4], ["dune", 2], ["up", -3], ["jaws", 2], ["alien", 9]]
- Output:
- ["dune", "jaws", "up", "heat"]
- Explanation
- Totals are
dune = 2,jaws = 2,up = 4 - 3 = 1, andheat = 0(no events).alienis not intitles, so it is ignored.duneandjawstie at 2 andduneis smaller, so it comes first.
Example 2
- Input:
- titles = ["a", "b", "c"], preferences = [["a", -5], ["b", -1]]
- Output:
- ["c", "b", "a"]
- Explanation
chas no events and scores 0, which beatsbat -1 andaat -5.
Example 3
- Input:
- titles = ["m2", "M1", "m10"], preferences = []
- Output:
- ["M1", "m10", "m2"]
- Explanation
- Every title scores 0, so the order is plain string order: uppercase letters sort before lowercase, and
"m10" < "m2"because'1' < '2'.
Constraints
- 0 ≤ titles.length ≤ 105
- 0 ≤ preferences.length ≤ 105
- Title ids in
titlesare distinct, non-empty strings of letters and digits. - -104 ≤ points ≤ 104