Hackerland Radio Transmitters

Sort by

recency

|

41 Discussions

|

  • + 0 comments

    Java with single loop

    public static int hackerlandRadioTransmitters(List<Integer> x, int k) {
            int counter = 0;
            int trans = 0;
            int minInRange = 0;
            
            x = x.stream().distinct().sorted().collect(toList());
            for(int n:x){
                if(trans == 0){
                    trans = n;
                    counter++;
                } else if(inRange(trans, n, k)){
                    if (minInRange == 0){
                        minInRange = trans;
                        trans = n;
                    } else if (inRange(minInRange, n, k)){
                            trans = n;
                    }
                } else {
                    trans = n;
                    minInRange = 0;
                    counter++;
                }
            }
            
            return counter;
        }
    
  • + 0 comments

    C# O(n)

    public static int hackerlandRadioTransmitters(List<int> houses, int range)
    {
        houses.Sort();
        
        int transmittersCount = 0;
        int i = 0;
        
        while (i < houses.Count)
        {
            transmittersCount++;
            
            // Take the first house. 
            int left = houses[i];
            int mid = left + range;
            
            // Find the first house out of range, put a transmitter 1 house before there.
            while (i < houses.Count && houses[i] <= mid)
            {
                i++;
            }
            
            mid = houses[i-1];
            int right = mid + range;
            
            // Find the first house out of range. Repeat.
            while (i < houses.Count && houses[i] <= right)
            {
                i++;
            }
        }
        
        return transmittersCount;
    }
    
  • + 0 comments

    Here is the approch for O(n) time complexity and O(n) space complexity.

    public static int hackerlandRadioTransmitters(List<Integer> x, int k) {
        // Write your code here
            if(x.size() < 1)
                return 0;
                
            int maxInd = x.stream().max(Comparator.comparingInt(Integer::intValue)).get();
            int minInd = x.stream().min(Comparator.comparingInt(Integer::intValue)).get();
            
            Set<Integer> houseIndex = new HashSet<>(x);
            int count = 0;
            int currentInd = minInd;
            while(currentInd <= maxInd) {
                if(houseIndex.contains(currentInd)) {
                    count++;
                    for(int i = k; i >= 0; i--) { // look for farthest house in range for radio installation 
                        if(houseIndex.contains(currentInd + i)) {
                            currentInd += i; //here radio installed
                            break;
                        }
                    }
                    currentInd += k;// till this range coverage is available
                }
                currentInd++; // move to find next house for coverage
            }
            return count;
        }
    
  • + 0 comments

    Here is HackerRland Radio transmitter problem solution in Python Java, c++ c and javascript

  • + 0 comments
    def hackerlandRadioTransmitters(x, k):
        size = len(x)
        if size == 1:
            return 1
            
        x.sort()
        num = 0
        i = 0
        while i < size:
            # mark the furthest location within the coverage at current i
            loc = x[i] + k
       
            # move to the furthest house within the range and set transmitter
            while i < size and x[i] <= loc:
                i += 1
            i -= 1
            num += 1
            
            # make the furthest location within the coverage at current i
            loc = x[i] + k
            
            # move to the first house outside the coverage
            while i < size and x[i] <= loc:
                i += 1
            
        return num