Hackerland Radio Transmitters

Sort by

recency

|

419 Discussions

|

  • + 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!

  • + 0 comments
    def hackerlandRadioTransmitters(x, k):
        t=0 #number of transmitters
        ind=0 #our location in Hackerland
        while ind<max(x): #Loops through the cities in list x
            if ind+1 not in x:
                ind+=1 #This makes sure we dont place transmitters in places where there are no houses
            else:
                while ind+k+1 not in x:
                    ind-=1 #This makes sure that the center is one of the houses
                t+=1 #Adds a transmitter
                ind+=(2*k+1) #skips all the locations where the transmitter is reached
        return t #returns the amount of of transmitters