Hackerland Radio Transmitters

  • + 0 comments

    C# O(n)

    public static int hackerlandRadioTransmitters(List<int> houses, int range)
    {
        houses.Sort();
        
        int transmittersCount = 0;
        int i = 0;
        
        while (i < houses.Count)
        {
            transmittersCount++;
            
            // Take the first house. 
            int left = houses[i];
            int mid = left + range;
            
            // Find the first house out of range, put a transmitter 1 house before there.
            while (i < houses.Count && houses[i] <= mid)
            {
                i++;
            }
            
            mid = houses[i-1];
            int right = mid + range;
            
            // Find the first house out of range. Repeat.
            while (i < houses.Count && houses[i] <= right)
            {
                i++;
            }
        }
        
        return transmittersCount;
    }