IT/coding study

[acmicpc] 20922. 겹치는 건 싫어(python)

seyeonHello 2022. 9. 18. 22:14

이중 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)

https://www.acmicpc.net/problem/20922

 

20922번: 겹치는 건 싫어

홍대병에 걸린 도현이는 겹치는 것을 매우 싫어한다. 특히 수열에서 같은 원소가 여러 개 들어 있는 수열을 싫어한다. 도현이를 위해 같은 원소가 $K$개 이하로 들어 있는 최장 연속 부분 수열

www.acmicpc.net

'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