Files
baekjoon-study/workbook_8708/silver/20922-s1.py
2026-02-23 11:14:50 +09:00

44 lines
1.0 KiB
Python

# 겹치는 건 싫어
import sys
from collections import deque
input = sys.stdin.readline
def solution():
n, k = map(int, input().rstrip().split())
s = list(map(int, input().rstrip().split()))
idx = 0
c = {}
d = deque([])
result = 0
while idx < len(s):
new = s[idx]
if not c.get(new, 0):
c[new] = 0
d.append(new)
c[new] += 1
if c[new] > k:
while d and c[new] > k:
now = d.popleft()
c[now] -= 1
result = max(result, len(d))
idx += 1
print(result)
return
solution()
"""
걸린 시간: 15분
시간 복잡도: 한 요소가 deque에 들어갔다가 나오기를 한번씩만 하므로 O(n)이다.
해설: deque에 숫자를 넣고, 숫자마다 개수를 dict로 센다.
숫자를 deque에 넣었을 때 그 숫자의 개수가 k개를 넘으면 그 숫자가 k개 이하가 될때까지 deque에서 popleft를 한다.
매번 현재 길이를 최대 길이와 비교해간다.
"""