Trade Volume Leaderboard
Problem statement
Process a sequence of company trade-volume operations. The leaderboard starts empty.
["TRADE", company, volume]adds the positive integervolumeto that company's cumulative traded volume.["TOP", k]requests the current topkcompanies.
For every TOP operation, return one row whose entries have the form "company|cumulativeVolume", ranked by decreasing cumulative volume and then lexicographically by company when volumes tie. Return the query rows in encounter order.
Function
tradeVolumeLeaderboard(operations: String[][]) → String[][]Examples
Example 1
operations = [["TRADE","MSFT","900"],["TRADE","APPL","300"],["TRADE","GOOG","1000"],["TRADE","APPL","400"],["TRADE","META","200"],["TRADE","BA","500"],["TOP","2"]]return = [["GOOG|1000","MSFT|900"]]APPL accumulates to 700, but GOOG and MSFT have the two largest totals.
Example 2
operations = [["TRADE","A","5"],["TRADE","B","7"],["TOP","2"],["TRADE","A","4"],["TOP","1"]]return = [["B|7","A|5"],["A|9"]]The second trade raises A's cumulative volume from 5 to 9.
Example 3
operations = [["TRADE","ZZ","10"],["TRADE","AA","10"],["TRADE","MM","10"],["TOP","3"]]return = [["AA|10","MM|10","ZZ|10"]]Equal totals are ordered lexicographically by company.
Constraints
1 <= operations.length <= 100000.- Every operation is
TRADEorTOP. companycontains between1and10uppercase English letters.1 <= volume <= 10^9.- For every query,
1 <= k <=the number of companies seen so far. - Each cumulative volume fits in a signed 64-bit integer.
- At least one
TOPoperation is present.