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.
publicstaticList<Integer>solve(List<Integer>arr,List<Integer>queries){List<Integer>results=newArrayList<>(queries.size());for(Integerquery:queries){if(query==1){// Special case. Just need to find the min value.results.add(arr.stream().mapToInt(Integer::intValue).min().getAsInt());}else{intmaxIdx=-1;intminOfMaxs=Integer.MAX_VALUE;for(inti=0;i<=arr.size()-query;++i){if(maxIdx<i)// The max value dropped out of the query windowsmaxIdx=windowMaxIdx(arr,i,query);elseif(arr.get(i+query-1)>arr.get(maxIdx))// New max value entered the query windowmaxIdx=i+query-1;minOfMaxs=Math.min(arr.get(maxIdx),minOfMaxs);}results.add(minOfMaxs);}}returnresults;}/** * Calulates the index of the max value within the window */privatestaticintwindowMaxIdx(List<Integer>arr,intidx,intwindow){intmaxIdx=idx;for(inti=idx+1;i<idx+window;++i){if(arr.get(i)>arr.get(maxIdx))maxIdx=i;}returnmaxIdx;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
An unexpected error occurred. Please try reloading the page. If problem persists, please contact support@hackerrank.com
Queries with Fixed Length
You are viewing a single comment's thread. Return to all comments →
Java 8