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.
Python 3 with greedy algorithm: O(n) time and space complexity.
fromcollectionsimportdefaultdictdefhackerlandRadioTransmitters(x,k):# Write your code hereis_house=defaultdict(bool)last_house=0first_house=float('inf')forhouseinx:is_house[house]=Truelast_house=max(last_house,house)first_house=min(first_house,house)curr=first_houseans=0whilecurr<=last_house:transmitter_here=currforcandinrange(curr,curr+k+1):ifis_house[cand]:transmitter_here=candans+=1curr=transmitter_here+k+1whilecurr<=last_houseandnotis_house[curr]:curr+=1returnans
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 →
Python 3 with greedy algorithm: O(n) time and space complexity.