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.
publicstaticintpylons(intk,List<Integer>arr){intans=0,n=arr.size();for(inti=0;i<n;i++){// Power plant should be built on buildIdxintbuildIdx=Math.min(i+(k-1),n-1);// Keep looking for a power plantwhile(buildIdx>=0&&buildIdx<n&&arr.get(buildIdx)==0)buildIdx--;if(buildIdx>=0&&buildIdx<n&&arr.get(buildIdx)==1&&Math.abs(buildIdx-i)<k){ans++;// Build herei=buildIdx+(k-1);}elsereturn-1;}returnans;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Goodland Electricity
You are viewing a single comment's thread. Return to all comments →
Java