Hackerland Radio Transmitters

Sort by

recency

|

19 Discussions

|

  • + 0 comments

    Python:

    def hackerlandRadioTransmitters(x, k):
        x.sort()
        res = 1
        mid = x[0] + k
        for n in x:
            if n <= mid:
                end = n + k
            elif n > end:
                res += 1
                mid = n + k
        return res
    
  • + 0 comments

    Python, typical greedy algorithm

    # two modes: 
    # a) looking for farthest house to provide covarege for current uncovered house (radio search)
    # b) looking for first house out of coverage from current house with radio
    
    def hackerlandRadioTransmitters(locs, coverage):
        locs.sort()
        if len(locs) == 1:
            return 1
    
        radiosearch = True
        curindex = 0
        numradios = 0
        
        while True:
            otherindex = curindex + 1
            while otherindex != len(locs) and locs[otherindex] - locs[curindex] <= coverage:
                otherindex += 1
                
            if radiosearch:
                numradios += 1
                curindex = otherindex - 1 # radio search ends one house too far
            else:
                curindex = otherindex
                
            if otherindex == len(locs):
                return numradios
            
            radiosearch = not radiosearch
    
  • + 0 comments

    Java O(n log n)

    public static int hackerlandRadioTransmitters(List<Integer> x, int k) {
            Collections.sort(x);
    
            int transmitters = 0;
            int i = 0;
    
            while (i < x.size()) {
                int location = x.get(i);
                transmitters++;
                int nextCoverage = location + k;
    
                while (i < x.size() && x.get(i) <= nextCoverage) {
                    i++;
                }
    
                int lastHouseInRange = x.get(i - 1);
    
                while (i < x.size() && x.get(i) <= lastHouseInRange + k) {
                    i++;
                }
            }
    
            return transmitters;
        }
    
  • + 0 comments

    Python 3 with greedy algorithm: O(n) time and space complexity.

    from collections import defaultdict
    def hackerlandRadioTransmitters(x, k):
        # Write your code here
        is_house = defaultdict(bool)
        last_house = 0
        first_house = float('inf')
        for house in x:
            is_house[house] = True
            last_house = max(last_house, house)
            first_house = min(first_house, house)
        curr = first_house
        ans = 0
        while curr <= last_house:
            transmitter_here = curr
            for cand in range(curr, curr+k+1):
                if is_house[cand]:
                    transmitter_here = cand
            ans += 1
            curr = transmitter_here + k + 1
            while curr <= last_house and not is_house[curr]:
                curr += 1
        return ans
    
  • + 0 comments
        
        x.sort()
        count=0
        while x:
            
            leftHouse=x[0]#this provides the latest left house that does not have connection
            #print('leftHouse',leftHouse)
            while x and leftHouse+k >=x[0]:#this part ensures that the value added covers all left houses using loop ,when the value goes out of transmitters reach loop breaks
               # print('leftHouse+k',leftHouse+k,'>=x[0]',x[0],'x=',x)
                transmitter =x.pop(0)#this iteration places transmitter
               # print('new value of x after pop',x)
               ## print('transmitter',transmitter)
            while x and transmitter+k>=x[0]:#this side handles right side of the transmission
                print('transmitter  loop pop',x.pop(0))#this is more or less doing an increment
                #print('new value of s after pop',x)
            count+=1
        return count