본문 바로가기
Beakjoon&프로그래머스/파이썬

[백준/파이썬] 20922번 겹치는 건 싫어

by 현장 2022. 5. 7.

-Code

n, k = map(int, input().split())
nums = list(map(int, input().split()))
count = [0] * (max(nums) + 1)
left, right, result = 0, 0, 0
while right < n:
    if count[nums[right]] < k:
        count[nums[right]] += 1
        right += 1
    else:
        count[nums[left]] -= 1
        left += 1
    result = max(right - left, result)
print(result)

처음에 cnt < n 일경 우의 lcs인 줄 알고 했다가 틀려서 문제 이해를 못 한 건가 찾아보니 cnt > k이면 진행을 멈추고 다른 포인터를 진행시키면서 모든 수가 k개 이하일 경우 최장 길이를 구하는 문제였습니다. 하지만 처음에 count의 길이와 위치 잘못 지정이나 result 수정을 잘못해서 시간이 좀 걸렸습니다.