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.
Here is the approch for O(n) time complexity and O(n) space complexity.
publicstaticinthackerlandRadioTransmitters(List<Integer>x,intk){// Write your code hereif(x.size()<1)return0;intmaxInd=x.stream().max(Comparator.comparingInt(Integer::intValue)).get();intminInd=x.stream().min(Comparator.comparingInt(Integer::intValue)).get();Set<Integer>houseIndex=newHashSet<>(x);intcount=0;intcurrentInd=minInd;while(currentInd<=maxInd){if(houseIndex.contains(currentInd)){count++;for(inti=k;i>=0;i--){// look for farthest house in range for radio installation if(houseIndex.contains(currentInd+i)){currentInd+=i;//here radio installedbreak;}}currentInd+=k;// till this range coverage is available}currentInd++;// move to find next house for coverage}returncount;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Hackerland Radio Transmitters
You are viewing a single comment's thread. Return to all comments →
Here is the approch for O(n) time complexity and O(n) space complexity.