Last active
October 10, 2021 04:54
-
-
Save sreeprasad/8852f68e5d658ef4b546b03425cfd171 to your computer and use it in GitHub Desktop.
Revisions
-
sreeprasad revised this gist
Oct 10, 2021 . 1 changed file with 57 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -6,4 +6,60 @@ output [null,null,2308,2308,2308,2308,2308,2308,2308,2308,2308,null,7876,2308,null,7876,1866,1866,7876,null,121,7876,null,7876,7876,5339,null,5339,121,null,null,1866,1866,null,3192,null,9369,null,1006] expected [null,null,2308,2308,2308,2308,2308,2308,2308,2308,2308,null,7876,2308,null,7876,1866,1866,7876,null,121,7876,null,7876,7876,1866,null,1866,121,null,null,1866,1866,null,1866,null,9369,null,1006] class StockPrice { TreeMap<Integer,Integer> timeToPrice; TreeMap<Integer,Set<Integer>> priceToTime; int current; public StockPrice() { timeToPrice = new TreeMap<>(); priceToTime = new TreeMap<>(); current =0; } /* time price [1,5], [2,5], [1,6] price to time 6 -> 1 5 -> 2 time to price 1 -> 6 2- > 5 */ public void update(int timestamp, int price) { if (timeToPrice.containsKey(timestamp)) { int priceToRemove = timeToPrice.get(timestamp); priceToTime.get(priceToRemove).remove(timestamp); if (priceToTime.get(priceToRemove).isEmpty()) priceToTime.remove(priceToRemove); } else { current = price; } timeToPrice.put(timestamp, price); priceToTime.computeIfAbsent(price, s-> new HashSet<Integer>()).add(timestamp); } public int current() { return current; } public int maximum() { return priceToTime.lastKey(); } public int minimum() { return priceToTime.firstKey(); } } -
sreeprasad created this gist
Oct 10, 2021 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,9 @@ ["StockPrice","update","maximum","current","minimum","maximum","maximum","maximum","minimum","minimum","maximum","update","maximum","minimum","update","maximum","minimum","current","maximum","update","minimum","maximum","update","maximum","maximum","current","update","current","minimum","update","update","minimum","minimum","update","current","update","maximum","update","minimum"] [[],[38,2308],[],[],[],[],[],[],[],[],[],[47,7876],[],[],[58,1866],[],[],[],[],[43,121],[],[],[40,5339],[],[],[],[32,5339],[],[],[43,6414],[49,9369],[],[],[36,3192],[],[48,1006],[],[53,8013],[]] output [null,null,2308,2308,2308,2308,2308,2308,2308,2308,2308,null,7876,2308,null,7876,1866,1866,7876,null,121,7876,null,7876,7876,5339,null,5339,121,null,null,1866,1866,null,3192,null,9369,null,1006] expected [null,null,2308,2308,2308,2308,2308,2308,2308,2308,2308,null,7876,2308,null,7876,1866,1866,7876,null,121,7876,null,7876,7876,1866,null,1866,121,null,null,1866,1866,null,1866,null,9369,null,1006]