이중 for문으로 구현하면 터져서 O(n)으로 구현했다
import sys
input=sys.stdin.readline
N,K=map(int,input().split())
arr=list(map(int,input().split()))
left,right=0,0
check=[0]*100001
answer=0
cnt=0
while right<N:
if check[arr[right]]<K:
check[arr[right]]+=1
cnt+=1
right += 1
else:
answer=max(answer,cnt)
cnt-=1
check[arr[left]]-=1
left+=1
answer=max(answer,cnt)
print(answer)
'IT > coding study' 카테고리의 다른 글
[acmicpc] 1987. 알파벳(python) (0) | 2022.09.19 |
---|---|
[acmicpc] 2467. 용액(python) (0) | 2022.09.19 |
[acmicpc] 3758. KCPC(python) (0) | 2022.09.18 |
[SWEA] 2383. 점심 식사시간(python) (0) | 2022.09.16 |
[SWEA] 2115. 벌꿀채취(python) (0) | 2022.09.05 |