롤케이크 자르기
🤖문제 : 롤케이크 자르기 Lv2
😎풀이
from collections import Counter
def solution(topping) :
answer = 0
left_dict, right_dict = dict(Counter(topping[:1])), dict(Counter(topping[1:]))
left_cnt, right_cnt = len(left_dict), len(right_dict)
for i in range(1, len(topping)) :
if topping[i] in left_dict :
left_dict[topping[i]] += 1
else :
left_dict[topping[i]] = 1
left_cnt += 1
if right_dict[topping[i]] == 1 :
del right_dict[topping[i]]
right_cnt -= 1
else :
right_dict[topping[i]] -= 1
if left_cnt == right_cnt :
answer += 1
return answer
👩🏫접근 방식
- 누적합 시도 -> 시간 초과
- 오른쪽에서 왼쪽으로 하나씩 넘겨주며 변경된 부분을 고려해서 연산량을 줄임.