백준 9465 스티커

김민영·2023년 1월 10일
0

알고리즘

목록 보기
51/125

과정

  • dp
  • 대각선, 한칸 이전 대각선 중 큰 값을 더해 나가는 방식
import sys
input = sys.stdin.readline
tc = int(input())
for _ in range(tc):
    N = int(input())
    lst = [list(map(int, input().split())) for _ in range(2)]

    for i in range(1, N):
        if i == 1:
            lst[0][1] += lst[1][0]
            lst[1][1] += lst[0][0]
        else:
            lst[0][i] += max(lst[1][i-1], lst[1][i-2])
            lst[1][i] += max(lst[0][i-1], lst[0][i-2])

    print(max(lst[0][-1], lst[1][-1]))
  • 원래 dp문제는 1인 경우 예외처리를 하고 exit() 을 사용했었다.
  • 하지만 이번 문제에서는 계속 에러가 발생했다. 테스트케이스 여러 번을 돌려야 했기 때문이었다. ㅜㅜ
  • 예외 처리를 따로 해주지 않고, 반복문 안에서 다른 연산과 같이 돌아갈 수 있도록 해야겠다..
profile
노션에 1차 정리합니당 - https://cream-efraasia-f3c.notion.site/4fb02c0dc82e48358e67c61b7ce8ab36?v=

0개의 댓글