๋ฌธ์ ์ค๋ช
์ด๋ค ์์ด์ ์ฐ์ ๋ถ๋ถ ์์ด์ ๊ฐ์ ๊ธธ์ด์ ํ์ค ์์ด์ ๊ฐ ์์๋ผ๋ฆฌ ๊ณฑํ์ฌ ์ฐ์ ํ์ค ๋ถ๋ถ ์์ด์ ๋ง๋ค๋ ค ํฉ๋๋ค. ํ์ค ์์ด์ด๋ [1, -1, 1, -1 …] ๋๋ [-1, 1, -1, 1 …] ๊ณผ ๊ฐ์ด 1 ๋๋ -1๋ก ์์ํ๋ฉด์ 1๊ณผ -1์ด ๋ฒ๊ฐ์ ๋์ค๋ ์์ด์ ๋๋ค. ์๋ฅผ ๋ค์ด ์์ด [2, 3, -6, 1, 3, -1, 2, 4]์ ์ฐ์ ๋ถ๋ถ ์์ด [3, -6, 1]์ ํ์ค ์์ด [1, -1, 1]์ ๊ณฑํ๋ฉด ์ฐ์ ํ์ค ๋ถ๋ถ์์ด์ [3, 6, 1]์ด ๋ฉ๋๋ค. ๋ ๋ค๋ฅธ ์์๋ก ์ฐ์ ๋ถ๋ถ ์์ด [3, -1, 2, 4]์ ํ์ค ์์ด [-1, 1, -1, 1]์ ๊ณฑํ๋ฉด ์ฐ์ ํ์ค ๋ถ๋ถ์์ด์ [-3, -1, -2, 4]์ด ๋ฉ๋๋ค. ์ ์ ์์ด sequence๊ฐ ๋งค๊ฐ๋ณ์๋ก ์ฃผ์ด์ง ๋, ์ฐ์ ํ์ค ๋ถ๋ถ ์์ด์ ํฉ ์ค ๊ฐ์ฅ ํฐ ๊ฒ์ return ํ๋๋ก solution ํจ์๋ฅผ ์์ฑํด์ฃผ์ธ์.
์ ํ ์ฌํญ
- 1 ≤ sequence์ ๊ธธ์ด ≤ 500,000
- -100,000 ≤ sequence์ ์์ ≤ 100,000
- sequence์ ์์๋ ์ ์์ ๋๋ค.
์ ์ถ๋ ฅ ์
sequence | result |
[2, 3, -6, 1, 3, -1, 2, 4] | 10 |
์ ์ถ๋ ฅ ์ ์ค๋ช
์ฃผ์ด์ง ์์ด์ ์ฐ์ ๋ถ๋ถ ์์ด [3, -6, 1]์ ํ์ค ์์ด [1, -1, 1]์ ๊ณฑํ์ฌ ์ฐ์ ํ์ค ๋ถ๋ถ ์์ด [3, 6, 1]์ ์ป์ ์ ์๊ณ ๊ทธ ํฉ์ 10์ผ๋ก์ ๊ฐ์ฅ ํฝ๋๋ค.
ํ์ด
- ์ด๊ฑฐ ์ค๋ ํ์๋ ์คํฐ์ปค ๋ชจ์ผ๊ธฐ (2) ๋ฌธ์ ๋ ๋น์ทํ๋ค. ์์ ์ด์ผ๋ก ํ์๋ค ใ ใ ใ
def solution(sequence):
answer = 0
n = len(sequence)
dp = [0] * n
dp[0] = sequence[0]
if n == 1:
return max(dp[0]*-1, dp[0])
else:
for i in range(1, n, 2):
sequence[i] *= -1
dp[0] = sequence[0]
for i in range(1, n):
dp[i] = max(sequence[i], sequence[i] + dp[i-1])
answer = max(dp)
for i in range(n):
sequence[i] *= -1
dp[0] = sequence[0]
for i in range(1, n):
dp[i] = max(sequence[i], sequence[i] + dp[i-1])
answer = max(answer, max(dp))
return answer
์ ํ์ฑ ํ
์คํธ
ํ
์คํธ 1 ใ ํต๊ณผ (0.01ms, 10.3MB)
ํ
์คํธ 2 ใ ํต๊ณผ (0.00ms, 10.2MB)
ํ
์คํธ 3 ใ ํต๊ณผ (0.02ms, 10.2MB)
ํ
์คํธ 4 ใ ํต๊ณผ (0.03ms, 10.2MB)
ํ
์คํธ 5 ใ ํต๊ณผ (0.02ms, 10.2MB)
ํ
์คํธ 6 ใ ํต๊ณผ (0.03ms, 10.2MB)
ํ
์คํธ 7 ใ ํต๊ณผ (0.06ms, 10.1MB)
ํ
์คํธ 8 ใ ํต๊ณผ (0.49ms, 10.3MB)
ํ
์คํธ 9 ใ ํต๊ณผ (0.62ms, 10.2MB)
ํ
์คํธ 10 ใ ํต๊ณผ (5.94ms, 10.4MB)
ํ
์คํธ 11 ใ ํต๊ณผ (7.31ms, 10.7MB)
ํ
์คํธ 12 ใ ํต๊ณผ (74.73ms, 17.7MB)
ํ
์คํธ 13 ใ ํต๊ณผ (93.50ms, 17.7MB)
ํ
์คํธ 14 ใ ํต๊ณผ (79.86ms, 17.4MB)
ํ
์คํธ 15 ใ ํต๊ณผ (68.00ms, 17.7MB)
ํ
์คํธ 16 ใ ํต๊ณผ (64.65ms, 18.9MB)
ํ
์คํธ 17 ใ ํต๊ณผ (404.85ms, 48.2MB)
ํ
์คํธ 18 ใ ํต๊ณผ (389.38ms, 48.6MB)
ํ
์คํธ 19 ใ ํต๊ณผ (412.56ms, 56.1MB)
ํ
์คํธ 20 ใ ํต๊ณผ (344.72ms, 56MB)
- ์ด... ์ฐ์?
def solution(sequence):
a1, a2 = -111111, -111111
s1, s2 = 0, 0
sign = [-1,1]
for i, s in enumerate(sequence):
s1 += s * sign[i%2]
s2 += s * sign[(i+1)%2]
a1 = max(s1,a1)
a2 = max(s2,a2)
s1 = max(0,s1)
s2 = max(0,s2)
return max(a1,a2)
์ ํ์ฑ ํ
์คํธ
ํ
์คํธ 1 ใ ํต๊ณผ (0.01ms, 10.2MB)
ํ
์คํธ 2 ใ ํต๊ณผ (0.01ms, 10.1MB)
ํ
์คํธ 3 ใ ํต๊ณผ (0.01ms, 10.3MB)
ํ
์คํธ 4 ใ ํต๊ณผ (0.02ms, 10MB)
ํ
์คํธ 5 ใ ํต๊ณผ (0.02ms, 10.3MB)
ํ
์คํธ 6 ใ ํต๊ณผ (0.06ms, 10.3MB)
ํ
์คํธ 7 ใ ํต๊ณผ (0.04ms, 10.3MB)
ํ
์คํธ 8 ใ ํต๊ณผ (0.35ms, 10.3MB)
ํ
์คํธ 9 ใ ํต๊ณผ (0.70ms, 10.1MB)
ํ
์คํธ 10 ใ ํต๊ณผ (3.64ms, 10.3MB)
ํ
์คํธ 11 ใ ํต๊ณผ (6.96ms, 10.4MB)
ํ
์คํธ 12 ใ ํต๊ณผ (70.10ms, 13.9MB)
ํ
์คํธ 13 ใ ํต๊ณผ (71.70ms, 14.1MB)
ํ
์คํธ 14 ใ ํต๊ณผ (70.91ms, 14.2MB)
ํ
์คํธ 15 ใ ํต๊ณผ (72.48ms, 14.1MB)
ํ
์คํธ 16 ใ ํต๊ณผ (71.04ms, 14.1MB)
ํ
์คํธ 17 ใ ํต๊ณผ (352.71ms, 31.3MB)
ํ
์คํธ 18 ใ ํต๊ณผ (348.84ms, 31.2MB)
ํ
์คํธ 19 ใ ํต๊ณผ (344.66ms, 31.6MB)
ํ
์คํธ 20 ใ ํต๊ณผ (341.89ms, 31.8MB)
- ๋น ๋ฆ
def solution(sequence):
# 1 ๋ถํฐ ์ฐ์ํ์ค ๋ถ๋ถ ์์ด์ ๊ณฑํ๊ฐ ์ฐพ๊ธฐ prefix sum ๋ง๋ค๊ธฐ
# maxV - minV
# ๊ฐ๊ฐ์ ๋ฆฌ์คํธ์์ max()
answer = 0
prefixS = [0]
for i in range(len(sequence)):
pulse = 1 if i%2 ==0 else -1
prefixS.append(prefixS[-1]+pulse*sequence[i])
return abs(max(prefixS) - min(prefixS))
์ ํ์ฑ ํ
์คํธ
ํ
์คํธ 1 ใ ํต๊ณผ (0.01ms, 10.3MB)
ํ
์คํธ 2 ใ ํต๊ณผ (0.01ms, 10.1MB)
ํ
์คํธ 3 ใ ํต๊ณผ (0.01ms, 10.2MB)
ํ
์คํธ 4 ใ ํต๊ณผ (0.01ms, 10.3MB)
ํ
์คํธ 5 ใ ํต๊ณผ (0.01ms, 10.3MB)
ํ
์คํธ 6 ใ ํต๊ณผ (0.01ms, 10.2MB)
ํ
์คํธ 7 ใ ํต๊ณผ (0.02ms, 10.1MB)
ํ
์คํธ 8 ใ ํต๊ณผ (0.10ms, 10.1MB)
ํ
์คํธ 9 ใ ํต๊ณผ (0.39ms, 10MB)
ํ
์คํธ 10 ใ ํต๊ณผ (1.07ms, 10.5MB)
ํ
์คํธ 11 ใ ํต๊ณผ (2.20ms, 10.6MB)
ํ
์คํธ 12 ใ ํต๊ณผ (23.09ms, 17.8MB)
ํ
์คํธ 13 ใ ํต๊ณผ (22.49ms, 17.8MB)
ํ
์คํธ 14 ใ ํต๊ณผ (24.46ms, 17.8MB)
ํ
์คํธ 15 ใ ํต๊ณผ (24.59ms, 17.7MB)
ํ
์คํธ 16 ใ ํต๊ณผ (22.96ms, 19.1MB)
ํ
์คํธ 17 ใ ํต๊ณผ (112.46ms, 50.3MB)
ํ
์คํธ 18 ใ ํต๊ณผ (116.15ms, 52.2MB)
ํ
์คํธ 19 ใ ํต๊ณผ (118.42ms, 58.6MB)
ํ
์คํธ 20 ใ ํต๊ณผ (134.35ms, 58.7MB)
- ์ค์... ์ด๋ ๊ฒ๋ ํธ๋๊ตฌ๋?
def solution(sequence):
acc_list=[0]
for idx in range(len(sequence)):
if idx%2==0:
acc_list.append(acc_list[-1]+sequence[idx])
else:
acc_list.append(acc_list[-1]-sequence[idx])
return max(acc_list)-min(acc_list)
์ ํ์ฑ ํ
์คํธ
ํ
์คํธ 1 ใ ํต๊ณผ (0.01ms, 10.3MB)
ํ
์คํธ 2 ใ ํต๊ณผ (0.01ms, 10.3MB)
ํ
์คํธ 3 ใ ํต๊ณผ (0.01ms, 10.2MB)
ํ
์คํธ 4 ใ ํต๊ณผ (0.01ms, 10.1MB)
ํ
์คํธ 5 ใ ํต๊ณผ (0.01ms, 10.3MB)
ํ
์คํธ 6 ใ ํต๊ณผ (0.01ms, 10.2MB)
ํ
์คํธ 7 ใ ํต๊ณผ (0.02ms, 10.2MB)
ํ
์คํธ 8 ใ ํต๊ณผ (0.09ms, 10.3MB)
ํ
์คํธ 9 ใ ํต๊ณผ (0.19ms, 10.2MB)
ํ
์คํธ 10 ใ ํต๊ณผ (1.86ms, 10.3MB)
ํ
์คํธ 11 ใ ํต๊ณผ (3.71ms, 10.9MB)
ํ
์คํธ 12 ใ ํต๊ณผ (20.46ms, 17.8MB)
ํ
์คํธ 13 ใ ํต๊ณผ (20.96ms, 17.9MB)
ํ
์คํธ 14 ใ ํต๊ณผ (21.82ms, 17.8MB)
ํ
์คํธ 15 ใ ํต๊ณผ (26.54ms, 17.8MB)
ํ
์คํธ 16 ใ ํต๊ณผ (19.79ms, 19MB)
ํ
์คํธ 17 ใ ํต๊ณผ (107.60ms, 50.4MB)
ํ
์คํธ 18 ใ ํต๊ณผ (104.11ms, 52.3MB)
ํ
์คํธ 19 ใ ํต๊ณผ (112.87ms, 58.8MB)
ํ
์คํธ 20 ใ ํต๊ณผ (111.49ms, 58.8MB)
- ๊ณ ์๋ค์ ๊ธฐ์์ฒ์ธํ ๋ฐฉ๋ฒ์ ๋ณด๋ฉด์ ๋๋ฌด ์ ๊ธฐํด์ ์๊ฐ ๊ฐ๋ ์ค ๋ชจ๋ฅด๊ฒ ๋ค.