728x90
시간초과 및 일부 테스트 케이스를 실패했다. 반복문을 돌면서 음수면 reset 하는 방식으로 풀 수 도 있다.
import copy
def solution(s):
answer = 0
temp1 = copy.deepcopy(s)
temp2 = copy.deepcopy(s)
# 1 로 시작
for i in range(0,len(s)):
if i%2==0:
temp1[i] = -temp1[i]
else:
temp2[i] = -temp2[i]
dp1 = [0] * len(s)
dp2 = [0] * len(s)
dp1[0] = temp1[0]
dp2[0] = temp2[0]
# 부분 수열 구하기
for i in range(1,len(dp2)):
dp1[i] = dp1[i-1] + temp1[i]
dp2[i] = dp2[i-1] + temp2[i]
answer1 = 0
answer2 = 0
for i in range(0,len(dp1)):
for j in range(i,len(dp1)):
if -(dp1[i]-dp1[j]) > answer1:
answer1 = -(dp1[i]-dp1[j])
for i in range(0,len(dp2)):
for j in range(i,len(dp2)):
if -(dp2[i]-dp2[j]) > answer2:
answer2 = -(dp2[i]-dp2[j])
return max(answer1,answer2)728x90
'🟢 알고리즘 문제 풀이 > Programmers' 카테고리의 다른 글
| [파이썬] 프로그래머스 크레인 인형뽑기 게임 (0) | 2023.09.13 |
|---|---|
| [파이썬] 프로그래머스 프로세스 (0) | 2023.08.21 |
| [파이썬] 프로그래머스 무인도 연습 (0) | 2023.05.24 |
| [파이썬] 백준 10159 저울 (0) | 2023.05.08 |
| [파이썬] 프로그래머스 연속된 부분 수열의 합 (0) | 2023.04.26 |