We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
publicstaticintminimumLoss(List<Long>price){// Write your code hereintn=price.size();longloss=Integer.MAX_VALUE;longlastPrice=0;intlastIdx=0;TreeMap<Long,Integer>map=newTreeMap<>();for(inti=0;i<n;i++){map.put(price.get(i),i);}for(Map.Entry<Long,Integer>e:map.entrySet()){longcurPrice=e.getKey();intcurIdx=e.getValue();if(curIdx<lastIdx){loss=Math.min(loss,curPrice-lastPrice);}lastPrice=curPrice;lastIdx=curIdx;}return(int)loss;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Minimum Loss 1
You are viewing a single comment's thread. Return to all comments →
Java8 using TreeMap