Top Ten Trades by Notional Value
Problem statement
Given a finite batch of buy and sell trade records, return the IDs of the ten trades with the largest notional values. If there are fewer than ten trades, return every trade ID.
Each row of trades contains exactly four strings in this order:
tradeId: a unique identifier.side: eitherBUYorSELL.priceCents: a positive integer price in cents.quantity: a positive integer quantity.
A trade's notional value is priceCents * quantity. Compute it with integer arithmetic. Rank trades by descending notional value. When two trades have the same notional value, the trade that appears earlier in the input ranks first.
Return the ranked tradeId values. Use a heap that retains at most ten candidates instead of sorting the complete input.
Function
topTenTrades(trades: String[][]) → String[]Examples
Example 1
trades = [["t1","BUY","100","1"],["t2","SELL","50","10"],["t3","BUY","200","3"],["t4","SELL","90","2"],["t5","BUY","60","4"],["t6","SELL","75","5"],["t7","BUY","40","8"],["t8","SELL","110","2"],["t9","BUY","30","9"],["t10","SELL","20","20"],["t11","BUY","10","2"],["t12","SELL","300","2"]]return = ["t3","t12","t2","t10","t6","t7","t9","t5","t8","t4"]t3 and t12 both have notional value 600, so their input order breaks the tie. The two smallest trades, t11 and t1, do not enter the top ten.
Example 2
trades = [["a","BUY","1250","4"],["b","SELL","2500","2"],["c","BUY","999","3"]]return = ["a","b","c"]All three records are returned. Trades a and b both have notional value 5000, so a remains first because it appeared earlier.
Constraints
0 <= trades.length <= 200000.- Every row contains exactly four values in the documented order.
- Every
tradeIdis unique and non-empty. sideis eitherBUYorSELL.1 <= priceCents, quantity <= 10^9.- Numeric row fields are canonical base-10 integer strings without signs or separators.
- The product
priceCents * quantityfits in a signed 64-bit integer. - The intended selection complexity is
O(n log 10)time withO(10)heap space, excluding the returned IDs.