Mutable Stock Price Top K
Problem statement
Maintain the current integer price of each stock while processing a finite sequence of operations.
["update", symbol, price]creates the stock if necessary and replaces its current price.["topK", k]returns up tokcurrent stock symbols, ordered by descending price and then lexicographically by symbol when prices are equal.
If fewer than k stocks exist, return every current stock. Return one symbol list for each topK operation, in encounter order.
Function
trackTopStocks(operations: String[][]) → String[][]Examples
Example 1
operations = [["update","AAPL","150"],["update","MSFT","310"],["update","GOOG","310"],["topK","2"],["update","AAPL","400"],["topK","3"]]return = [["GOOG","MSFT"],["AAPL","GOOG","MSFT"]]The first query breaks the price tie between GOOG and MSFT lexicographically. After AAPL is updated to 400, it ranks first.
Example 2
operations = [["update","IBM","100"],["topK","3"],["update","IBM","90"],["topK","1"]]return = [["IBM"],["IBM"]]The first query returns every current stock because only one exists. Updating IBM replaces its old price rather than adding a second stock entry.
Constraints
1 <= operations.length <= 100000.- Every operation is either
updateortopK. symbolcontains between1and10uppercase English letters.1 <= price <= 1000000000.1 <= k <= 100000.