Post

[BOJ] 9465. ์Šคํ‹ฐ์ปค(python)

๐Ÿ“Œ๋ฌธ์ œ

Alt text

๐Ÿ’ช์•„์ด๋””์–ด

  1. ํ•˜๋‚˜์˜ ์Šคํ‹ฐ์ปค๋ฅผ ์„ ํƒํ•˜๋ฉด ์ƒํ•˜์ขŒ์šฐ์— ์žˆ๋Š” ์Šคํ‹ฐ์ปค๋Š” ์„ ํƒํ•  ์ˆ˜ ์—†๋‹ค. ๊ทธ๋ž˜์„œ ํ˜„์žฌ ์Šคํ‹ฐ์ปค์—์„œ ๋ถ์„œ์ชฝ์ด๋‚˜ ๋‚จ์„œ์ชฝ์— ์žˆ๋Š” ์Šคํ‹ฐ์ปค๋ฅผ ๊ณ ๋ฅด๊ฑฐ๋‚˜ ํ•œ์นธ ๋’ค์ชฝ ๋ถ์„œ๋‚˜ ๋‚จ์„œ์ชฝ ์Šคํ‹ฐ์ปค๋ฅผ ๊ณ ๋ฅผ ์ˆ˜ ์žˆ๋‹ค.
    Alt text

  2. ์„ธ ๋ฒˆ์งธ ์Šคํ‹ฐ์ปค๋ถ€ํ„ฐ ๋‘ ๊ฐ€์ง€ ์„ ํƒ์ง€๊ฐ€ ์žˆ๋‹ค.
    Alt text

  3. 2๋ฒˆ์„ ์„ ํƒํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ์ด๋ฏธ 1.์—์„œ ๊ณ ๋ฅธ ์Šคํ‹ฐ์ปค์˜ ์ ์ˆ˜๋ฅผ ๋”ํ•œ ๊ฐ’์ด๊ธฐ ๋•Œ๋ฌธ์— ํ•ด๋‹น ์Šคํ‹ฐ์ปค ์ ์ˆ˜์™€ 2๋ฒˆ ์Šคํ‹ฐ์ปค์ ์ˆ˜๋ฅผ ๋”ํ•˜๋ฉด ๋œ๋‹ค.
    Alt text Alt text

  4. ์ตœ๋Œ€ ์Šคํ‹ฐ์ปค ์ ์ˆ˜ ๊ณ ๋ฅด๊ธฐ
    1์—ด์— ์žˆ๋Š” ์Šคํ‹ฐ์ปค์™€ 2์—ด์— ์žˆ๋Š” ์Šคํ‹ฐ์ปค๋ฅผ ๊ณ ๋ฅด๋Š” ๊ฒฝ์šฐ๊ฐ€ ์„œ๋กœ ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์— 1์—ด๊ณผ 2์—ด์˜ ๋งˆ์ง€๋ง‰๋ฒˆ์งธ์— ์žˆ๋Š” ์Šคํ‹ฐ์ปค์˜ ์ดํ•ฉ์„ ๋น„๊ตํ•œ๋‹ค.

    ๐Ÿฅ‚์ฝ”๋“œ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import sys; input = sys.stdin.readline

T = int(input())
for _ in range(T):

    n = int(input())
    stickers = []

    for i in range(2):
        stickers.append(list(map(int,input().split())))

    stickers[0].insert(0,0)
    stickers[1].insert(0,0)

    for i in range(2,n+1):
        stickers[0][i] += max(stickers[1][i-1],stickers[1][i-2])
        stickers[1][i] += max(stickers[0][i-1],stickers[0][i-2])

    print(max(stickers[0][n],stickers[1][n]))
This post is licensed under CC BY 4.0 by the author.

Comments powered by Disqus.