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<int>arr){intp=-1;intcount=-1;p=StartingKIndex(p,arr,k);if(p!=-1){p=MiddleIndexes(p,k,arr,outcount);}if(count!=-1&&(p+k-1)<arr.Count-1){p=arr.Count-1;count=LastIndex(p,k,arr,count);}returncount;}publicstaticintStartingKIndex(intp,List<int>arr,intk){// Check the first city where Power plant can be built within the distribution rangefor(inti=k-1;i>=0;i--){if(arr[i]==1){p=i;break;}}returnp;}publicstaticintMiddleIndexes(intp,intk,List<int>arr,outintcount){// StartIndex function will return a city index so make the count as 1 initially.count=1;// Check for the next cities where Power plant can be built and increment the countwhile((p+(2*k)-1)<arr.Count){boolflag=false;p=p+(2*k)-1;for(inti=p;i>p-(2*k)+1;i--){if(arr[i]==1){p=i;count=count+1;flag=true;break;}}// if a city cannot be covered withing the distribution range make the count as -1if(!flag){count=-1;break;}}returnp;}publicstaticintLastIndex(intp,intk,List<int>arr,intcount){// find the last city where the power plant can be built within the distribution range. boolflag=false;for(inti=p;i>arr.Count-k+1;i--){if(arr[i]==1){count+=1;flag=true;break;}}// if city is not found make the count as -1if(!flag){count=-1;}returncount;}
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 →
C#: