diff --git a/workbook_8708/21921-s3.py b/workbook_8708/21921-s3.py new file mode 100644 index 0000000..4067f1d --- /dev/null +++ b/workbook_8708/21921-s3.py @@ -0,0 +1,46 @@ +# 블로그 + +import sys + +from collections import deque, Counter + +input = sys.stdin.readline + +def solution(): + n, x = map(int, input().rstrip().split()) + + one_days = list(map(int, input().rstrip().split())) + duration = deque([one_days[i] for i in range(x)]) + + duration_result = [0] * n + duration_result[x-1] = sum(duration) + + for i in range(x, n): + before = duration.popleft() + duration.append(one_days[i]) + after = duration[-1] + duration_result[i] = duration_result[i-1] - before + after + + c = Counter(duration_result) + max_vistor = max(c) + if max_vistor == 0: + print('SAD') + else: + print(max_vistor) + print(c.get(max_vistor)) + + return + + +solution() + +""" +걸린 시간: 19분 + +시간 복잡도: 전체 요소를 한, 두바퀴 정도 돌기 때문에 O(n)이다. + +해설: 그냥 인덱스로 앞 뒤로 줄이고, 늘이면 되는데 슬라이딩 윈도우는 deque를 쓰는게 좀 직관적이라서 deque를 사용했다. +최대값과 count를 계속 업데이트 하는 것이 코드가 가독성이 떨어지는 것 같아서 어차피 그렇게 연산하는 것과 +마지막에 결과 리스트 한 바퀴 돌면서 개수 세는 연산이 비슷하기 때문에 그냥 결과 리스트에 쭉 저장하고 Counter로 개수를 셌다. +0이 최대일 경우 SAD 출력(처음에 이거 안해서 틀림), 아니면 최대값과 그 개수를 출력한다. +""" \ No newline at end of file