You are viewing a single comment's thread. Return to all comments →
Queue<Integer> heap = new PriorityQueue<>(); long luck = 0; for(int i=0; i<N; i++) { if(T[i]!=1) luck+=L[i]; else if(heap.size()<K) heap.add(L[i]); else if(K<1 || L[i]<=heap.peek()) luck-=L[i]; else {luck-=heap.remove(); heap.add(L[i]);} } luck += heap.stream().mapToInt(i->i).sum();
Seems like cookies are disabled on this browser, please enable them to open this website
Luck Balance
You are viewing a single comment's thread. Return to all comments →
Java8 solution in O(N*log(K)) time and O(K) space: