Hackerland Radio Transmitters

  • + 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;
        }