Skip to content

Commit 3fc4941

Browse files
committed
연속 펄스 부분 수열의 합 풀이
1 parent dded972 commit 3fc4941

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# 연습문제
2+
# https://school.programmers.co.kr/learn/courses/30/lessons/161988
3+
4+
5+
def solution(sequence):
6+
n = len(sequence)
7+
arr1, arr2 = [], []
8+
for idx, num in enumerate(sequence):
9+
if idx % 2 == 0:
10+
arr1.append(num)
11+
arr2.append(-num)
12+
else:
13+
arr1.append(-num)
14+
arr2.append(num)
15+
16+
dp1, dp2 = [arr1[0]], [arr2[0]]
17+
for idx in range(1, n):
18+
dp1.append(max(dp1[idx-1] + arr1[idx], arr1[idx]))
19+
dp2.append(max(dp2[idx-1] + arr2[idx], arr2[idx]))
20+
21+
return max(max(dp1), max(dp2))
22+
23+
sequence = [[2, 3, -6, 1, 3, -1, 2, 4]]
24+
result = [10]
25+
26+
for q in [0]:
27+
qid = solution(sequence[q])
28+
if qid == result[q]:
29+
print(f'correct {qid}')
30+
else:
31+
print(f'incorrect {qid}')

0 commit comments

Comments
 (0)