From ecb877621faea92330fabce57678e2786b7c9ccc Mon Sep 17 00:00:00 2001 From: nkey Date: Thu, 19 Feb 2026 17:16:41 +0900 Subject: [PATCH] =?UTF-8?q?2531-s1=20=EC=84=B1=EA=B3=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- workbook_8708/2531-s1.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 workbook_8708/2531-s1.py diff --git a/workbook_8708/2531-s1.py b/workbook_8708/2531-s1.py new file mode 100644 index 0000000..c9466b9 --- /dev/null +++ b/workbook_8708/2531-s1.py @@ -0,0 +1,40 @@ +# 회전 초밥 + +import sys +from collections import Counter + +input = sys.stdin.readline + +def solution(): + n, d, k, c = map(int, input().rstrip().split()) + + belt = [int(input().rstrip()) for _ in range(n)] + double_belt = belt + belt + + eat = Counter(belt[:k]) + eat.setdefault(c, 0) + eat[c] += 1 + result = len(eat) + for i in range(1, len(double_belt)-k+1): + eat[double_belt[i-1]] -= 1 + if eat[double_belt[i-1]] == 0: + del eat[double_belt[i-1]] + eat.setdefault(double_belt[i+k-1], 0) + eat[double_belt[i+k-1]] += 1 + result = max(result, len(eat)) + + print(result) + + return + + +solution() + +""" +걸린 시간: 24분 + +시간 복잡도: 2n 길이의 리스트를 1번 순회하고, 그 과정에서 처리는 dict로 O(1)이므로, 전체 시간복잡도는 O(n)이다. + +해설: 같은 두 리스트를 이어붙여서 원형을 만든 후 k 구간씩 슬라이딩하면서 다른 종류의 개수를 세면 된다. +각 음식마다 있는 개수는 dict에 저장하고, key의 개수 세기로 O(1)에 종류 개수를 세기 때문에 value가 0이면 del로 key까지 없앤다. +""" \ No newline at end of file