코딩테스트/프로그래머스

[프로그래머스/2단계/PYTHON] 연속된 부분 수열의 합

jinsang-2 2024. 5. 29. 16:17

https://school.programmers.co.kr/learn/courses/30/lessons/178870

def solution(sequence, k):
    n=len(sequence)
    end=0
    max_sum=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:
            answer = [start,end-1]
            interval=end-1-start
        max_sum -= sequence[start]
    
    return answer