Home [TIL] 99클럽 코테 스터디 6기 5일차 TIL - 수열(슬라이딩 윈도우)
Post
Cancel

[TIL] 99클럽 코테 스터디 6기 5일차 TIL - 수열(슬라이딩 윈도우)

오늘의 문제

2559. 수열(실버1)

키워드

누적 합, 두 포인터, 슬라이딩 윈도우

나의 풀이

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
## 수정 후 정답(슬라이딩 윈도우 사용)
N, K = map(int, input().split())
arr = list(map(int, input().split()))

# 초기 윈도우 합
window_sum = sum(arr[:K])
max_sum = window_sum

# 한 칸씩 이동하면서 합 갱신
for i in range(K, N):
    window_sum += arr[i] - arr[i - K]
    max_sum = max(max_sum, window_sum) ## 둘 중 더 큰 값을 저장

print(max_sum)



## [수정 전 오답] 시간초과

N, K = map(int, input().split())
arr = [*map(int, input().split())]

max = 0
for i in range(N-K+1) :
    sum_arr = sum(arr[i:i+K])
    if max < sum_arr :
        max = sum_arr
        
print(max)

## [수정 전 오답] 시간초과
print(max([sum(arr[i:i+K]) for i in range(N-K+1)]))

회고

This post is licensed under CC BY 4.0 by the author.

[TIL] 99클럽 코테 스터디 6기 4일차 TIL - [그래프] 안전 영역

[TIL] 99클럽 코테 스터디 6기 6일차 TIL - [BFS/DFS] 섬의 개수

Comments powered by Disqus.

Comments powered by Disqus.