https://school.programmers.co.kr/learn/courses/30/lessons/178870
def solution(sequence, k):
n = len(sequence)
max_sum = 0
end = 0
interval = n
for start in range(n):
while max_sum < k and end < n:
max_sum += sequence[end]
end += 1
if max_sum == k and end-1-start < interval:
res = [start, end-1]
interval = end-1-start
max_sum -= sequence[start]
return res
def solution(sequence, k):
answer = []
start, end = 0, 0
temp = sequence[0]
min_len = 1000001
while start <= end < len(sequence):
if temp == k:
if end - start + 1 < min_len:
min_len = end - start + 1
answer = [start, end]
temp -= sequence[start]
start += 1
elif temp < k:
end += 1
if end < len(sequence):
temp += sequence[end]
elif temp > k:
temp -= sequence[start]
start += 1
return answer