Hackerland Radio Transmitters

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