Skip to content

Instantly share code, notes, and snippets.

@nderkach
Created December 11, 2018 13:34
Show Gist options
  • Save nderkach/47d8f6a718e46d9f8d9f0567d077caca to your computer and use it in GitHub Desktop.
Save nderkach/47d8f6a718e46d9f8d9f0567d077caca to your computer and use it in GitHub Desktop.
# def consecutive(arr):
# arr = sorted(arr)
# max_so_far_start = 0
# max_so_far_end = 1
# max_range = (0, 1)
# cur_i = 1
# while cur_i <= len(arr):
# if cur_i != len(arr) and arr[cur_i] == arr[cur_i-1]+1:
# max_so_far_end += 1
# else:
# if cur_i - max_so_far_start > max_range[1] - max_range[0]:
# max_range = (max_so_far_start, cur_i)
# max_so_far_start = cur_i
# cur_i += 1
# return max_range[1] - max_range[0], arr[max_range[0]:max_range[1]]
def consecutive(arr):
values = set(arr)
max_total = 0
start_i = 0
for val in values:
if val-1 in values:
continue
start_i = val
max_so_far = 0
while val in values:
val += 1
max_so_far += 1
max_total = max(max_total, max_so_far)
# return the length of max consecutive range and the range itself
return max_total, [_ for _ in range(start_i, start_i+max_total)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment