Hackerland Radio Transmitters

Sort by

recency

|

420 Discussions

|

  • + 0 comments

    def hackerlandRadioTransmitters(x, k): x = sorted(set(x)) count = 0 next_start = -float('inf')

    for pos, val in enumerate(x):
    
        if val < next_start:
            continue
    
        for rg in range(k, -1, -1):
            if pos + rg < len(x) and val + k >= x[pos+rg]:
                next_start = x[pos+rg] + k + 1
                count += 1
                break
    
    return count
    
  • + 0 comments

    In Python, don't forget to run set on the house locations ("x") first. We only need to order the unique house locations.

  • + 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;
    }
    
  • + 0 comments
    def hackerlandRadioTransmitters(x, k):
        locations = sorted(x)
        station_location = first_house_outside_coverage = one_side_covered = two_side_coverage_counter = 0
    
        while station_location < len(locations):
            station_location_0 = station_location
    
            while first_house_outside_coverage < len(locations) and locations[first_house_outside_coverage] <= locations[station_location] + k:
                first_house_outside_coverage += 1
    
            if first_house_outside_coverage == station_location_0 + 1 or one_side_covered:
                station_location = first_house_outside_coverage
                two_side_coverage_counter += 1
                one_side_covered = 0
            else:
                station_location = first_house_outside_coverage - 1
                one_side_covered = 1
    
        return two_side_coverage_counter
    
  • + 1 comment

    Test case #2: 7 2 9 5 4 2 6 15 12

    Expected output: 4

    I think the result should be 3. When sorting: [2, 4, 5, 6, 9, 12, 15] Result is: [2, 4, 5, 6] -> 1 transmitter [9, 12] -> 1 transmitter [15] -> 1 transmitter Total 3 transmitters. Any help? Thank you!