Implement the RunningCommodityPrice class to process a stream of commodity prices. Each event provides a timestamp and a price. Events may arrive out of timestamp order.
RunningCommodityPrice()
void upsertCommodityPrice(int timestamp, int commodityPrice)
timestamp: The timestamp of the price record.commodityPrice: The price to store at that timestamp.commodityPrice.int getMaxCommodityPrice()
0 ≤ timestamp ≤ 1,000,000,0000 ≤ commodityPrice ≤ 1,000,000,000100,000 method calls are made per object.getMaxCommodityPrice() is called only after at least one call to upsertCommodityPrice.Each example starts with a new object. Parameter names in the calls are labels for readability.
RunningCommodityPrice r = new RunningCommodityPrice();r.upsertCommodityPrice(timestamp = 14, commodityPrice = 70);r.upsertCommodityPrice(timestamp = 3, commodityPrice = 55);r.upsertCommodityPrice(timestamp = 9, commodityPrice = 62);r.getMaxCommodityPrice(); Output: 70.r.upsertCommodityPrice(timestamp = 14, commodityPrice = 40);r.getMaxCommodityPrice(); Output: 62.The price at timestamp 14 changes from 70 to 40. The maximum is now 62, stored at timestamp 9.
RunningCommodityPrice r = new RunningCommodityPrice();r.upsertCommodityPrice(timestamp = 8, commodityPrice = 36);r.upsertCommodityPrice(timestamp = 2, commodityPrice = 36);r.getMaxCommodityPrice(); Output: 36.r.upsertCommodityPrice(timestamp = 8, commodityPrice = 21);r.getMaxCommodityPrice(); Output: 36.r.upsertCommodityPrice(timestamp = 2, commodityPrice = 50);r.getMaxCommodityPrice(); Output: 50.Lowering the price at timestamp 8 leaves the maximum unchanged because timestamp 2 still stores 36. Updating timestamp 2 to 50 raises the maximum.