Skip to content

Instantly share code, notes, and snippets.

@MartinThoma
Created October 3, 2021 06:21
Show Gist options
  • Save MartinThoma/72a101f67b58dbfa35bc552e47913190 to your computer and use it in GitHub Desktop.
Save MartinThoma/72a101f67b58dbfa35bc552e47913190 to your computer and use it in GitHub Desktop.
from typing import Tuple, List
def solution(l: List[int], t: int) -> Tuple[int, int]:
"""
>>> solution([1, 2, 3, 4, 5], 3)
(0, 1)
>>> solution([1, 2, 3, 4, 5], 4)
(3, 3)
>>> solution([1, 2, 3, 4, 5], 9)
(1, 3)
>>> solution ([2, 4, 8, 16], 3)
(-1, -1)
"""
start, end, s = 0, -1, 0
while end < len(l) and start < len(l):
if s < t:
end += 1
if end > len(l):
break
s += l[end]
elif s > t:
s -= l[start]
start += 1
if start > len(l):
break
else:
return (start, end)
return (-1, -1)
from doctest import testmod
testmod()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment