방울

Mkim4·2023년 6월 23일
2

from itertools import accumulate

def solution(bell):
    coors_start = {}
    coors_end = {}
    
    for i, x in enumerate(accumulate([0] + [-1 if b == 1 else 1 for b in bell])):
    #bell에서 b를 하나씩 꺼내는데 만약에 b가 1이면 -1을 반환하고
    #1이 아니라면 1을 반환해라
   
        if x not in coors_start:
            coors_start[x] = i
        coors_end[x] = i
    return max(coors_end[x] - coors_start[x] for x in coors_end)

accumulate 함수(누적합계)

from itertools import accumulate
사실 for문 만으로도 값을 누적한 리스트를 뽑을 수 있지만 값이 커지면 커질수록 속도면에서 큰 차이가 난다.

profile
귀요미 개발자

0개의 댓글