Hackerland Radio Transmitters

  • + 0 comments

    Java. Not the best solution, I think, but it works.

    public static int hackerlandRadioTransmitters(List<Integer> x, int k) {
        x.sort(Integer::compareTo);
        int count = 1;
        int currentBranchStart = x.get(0);
        boolean isLeftBranch = true;
    
        for (int i = 1; i < x.size(); i++) {
            int currentLocation = x.get(i);
            int prevLocation = x.get(i - 1);
    
            if (currentLocation - prevLocation > k) {
                count++;
                currentBranchStart = currentLocation;
                isLeftBranch = true;
                continue;
            }
    
            if (currentLocation - currentBranchStart > k) {
                if (isLeftBranch) {
                    currentBranchStart = prevLocation;
                } else {
                    currentBranchStart = currentLocation;
                    count++;
                }
                isLeftBranch = !isLeftBranch;
            }
        }
    
        return count;
    }