Skip to content

Instantly share code, notes, and snippets.

@Shaddyjr
Created March 21, 2021 21:20
Show Gist options
  • Save Shaddyjr/83bbcca1346c4eeef1509c24b518cbe3 to your computer and use it in GitHub Desktop.
Save Shaddyjr/83bbcca1346c4eeef1509c24b518cbe3 to your computer and use it in GitHub Desktop.
# source: https://www.hackerrank.com/challenges/hackerland-radio-transmitters
def hackerlandRadioTransmitters(houses, transmitter):
houses.sort() # requires sorting => O(nlogn)
transmitter_count = 0
i = 0
while i < len(houses): # O(n)
transmitter_count += 1
max_transmitting_house = houses[i] + transmitter
# position after transmitting house
while i < len(houses) and houses[i] <= max_transmitting_house:
i += 1
last_house = houses[i - 1] + transmitter
while i < len(houses) and houses[i] <= last_house:
i += 1
return transmitter_count # Time complexity: O(nlogn + n) => O(nlogn)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment