Last active
January 16, 2017 04:13
-
-
Save liavkoren/0566ace1e7a3b40597c8b7160a9e45b3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fileinput | |
import sys | |
sys.setrecursionlimit(10000) | |
def solve(houses, transmitter_range): | |
houses.sort(reverse=True) | |
if len(houses) == 0: | |
return 0 | |
if len(houses) == 1: | |
return 1 | |
first_house = site = houses[-1] | |
# Find the site for the transmitter | |
try: | |
while houses[-1] <= first_house + transmitter_range: | |
site = houses.pop() | |
except IndexError: | |
# We've run out of houses | |
return 1 | |
# move us to the first house outside the transmitter's range: | |
try: | |
while houses[-1] <= site + transmitter_range: | |
houses.pop() | |
else: | |
# If there's no houses within range of the transmitter on the right, | |
# we need to advance to the next house: | |
if site == houses[-1]: | |
houses.pop() | |
except IndexError: | |
# out of houses | |
return 1 | |
return 1 + solve(houses, transmitter_range) | |
def data_stream(): | |
for line in fileinput.input(): | |
line = line.strip().split(' ') | |
yield map(int, line) | |
stream = data_stream() | |
n, transmitter_range = next(stream) | |
houses = next(stream) | |
print(solve(houses, transmitter_range)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment