Platform Referral Rewards
Problem statement
Each string in records has the form worker referrer bonus. referrer is another worker name or None. Records may arrive in any order.
For every worker with a referrer and bonus b, the direct referrer earns b, that referrer's referrer earns b+1, the next ancestor earns b+2, and so on to the root. Bonuses may be positive, zero, or negative. Workers do not earn their own listed bonus.
Return all worker names sorted by decreasing total reward. Break equal-reward ties lexicographically.
Function
platformReferralRanking(records: String[]) → String[]Examples
Example 1
records = ["A None 0","B C 5","C None 0","D A 10","E D -3"]return = ["A","C","B","E","D"]Rewards are A=8, C=5, B=0, E=0, and D=-3.
Example 2
records = ["B A 2","C B 2","A None 0"]return = ["A","B","C"]A earns 2 from B and 3 from C, while B earns 2 from C.
Example 3
records = ["Z None 0","A None 0"]return = ["A","Z"]Both rewards are zero, so lexicographic order decides the result.
Constraints
1 <= records.length <= 200000.- Every record contains a unique worker name, a referrer name or
None, and a signed 32-bit bonus. - Worker names are nonempty ASCII alphanumeric strings of length at most
40. - The referral relation is acyclic and every named referrer belongs to the same forest.
- Every total reward fits in a signed 64-bit integer.