Social feed~22 min2s · 256 MBtarget O((n + m) log (n + m)) time, O(n + m) spaceheaps
You work on a social app that ranks creators by points. Each creator starts with 0 points. Fans give points, and a moderator can also take points away. The app shows the current leader on its home page.
Your team lead asks: "Who leads after every change?" A log holds up to 100,000 changes for up to 100,000 creators.
Write leader_after_each_update(creators, who, points).
0 to creators - 1. All start with a score of 0.who[i] and points[i] describe change i, in order. The score of creator who[i] goes up by points[i]. A negative points[i] lowers it.Input: creators = 3, who = [0, 1, 0, 1, 0], points = [3, 2, -2, 4, 5]
Output: [0, 0, 1, 1, 0]| After change | Scores of 0, 1, 2 | Leader |
|---|---|---|
| 0 gains 3 | 3, 0, 0 | 0 |
| 1 gains 2 | 3, 2, 0 | 0 |
| 0 loses 2 | 1, 2, 0 | 1 |
| 1 gains 4 | 1, 6, 0 | 1 |
| 0 gains 5 | 6, 6, 0 | 0: a tie, and 0 is the smaller number |
Input: creators = 1, who = [0, 0], points = [5, -9]
Output: [0, 0]Input: creators = 2, who = [0], points = [-5]
Output: [1]Creator 0 drops to -5, so creator 1, with 0 points, leads.
1 ≤ creators ≤ 100,0001 ≤ len(who) ≤ 100,000, and points has the same length.0 ≤ who[i] < creators-1,000 ≤ points[i] ≤ 1,000Scanning all creators after every change costs 100,000 steps for each of 100,000 changes, which is 10 billion steps. A heap gives the best entry in a few steps. But a heap cannot change an entry in place, and scores here go down as well as up.