๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
4๏ธโƒฃ Python/Problem Solving

[Python Algorithm] ๋•…๋”ฐ๋จน๊ธฐ Programmers(Lv.2)

by seolhee2750 2022. 2. 4.
๋ฌธ์ œ

https://programmers.co.kr/learn/courses/30/lessons/12913?language=python3 

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๋•…๋”ฐ๋จน๊ธฐ

๋•…๋”ฐ๋จน๊ธฐ ๊ฒŒ์ž„์„ ํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋•…๋”ฐ๋จน๊ธฐ ๊ฒŒ์ž„์˜ ๋•…(land)์€ ์ด Nํ–‰ 4์—ด๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๊ณ , ๋ชจ๋“  ์นธ์—๋Š” ์ ์ˆ˜๊ฐ€ ์“ฐ์—ฌ ์žˆ์Šต๋‹ˆ๋‹ค. 1ํ–‰๋ถ€ํ„ฐ ๋•…์„ ๋ฐŸ์œผ๋ฉฐ ํ•œ ํ–‰์”ฉ ๋‚ด๋ ค์˜ฌ ๋•Œ, ๊ฐ ํ–‰์˜ 4์นธ ์ค‘ ํ•œ ์นธ๋งŒ ๋ฐŸ

programmers.co.kr

 

๋‚ด ๋ฌธ์ œ ํ’€์ด
def solution(land):
    if len(land) == 1:
        return max(land[0])
    else:
        for i in range(1, len(land)):
            land[i][0] += max(land[i - 1][1], land[i - 1][2], land[i - 1][3])
            land[i][1] += max(land[i - 1][0], land[i - 1][2], land[i - 1][3])
            land[i][2] += max(land[i - 1][0], land[i - 1][1], land[i - 1][3])
            land[i][3] += max(land[i - 1][0], land[i - 1][1], land[i - 1][2])

        return max(land[-1])

๐Ÿ‘‰ DP ๋ฌธ์ œ๋กœ, ํ•œ ํ–‰์”ฉ ๋‚ด๋ ค์˜ค๋ฉด์„œ ์ง์ „ ์—ด์˜ ํ˜„์žฌ ํ–‰์„ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€ ์ค‘ ์ตœ๋Œ“๊ฐ’์„ ๊ณ ๋ฅด๋Š” ๊ฒƒ์„ ๋ฐ˜๋ณตํ–ˆ๋‹ค.

 

๐Ÿ’ก ํ”ผ๋“œ๋ฐฑ
  • DP ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ˆ™์ง€ํ•˜๊ณ  ์žˆ๋‹ค๋ฉด ๊ธˆ๋ฐฉ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์˜€๋‹ค.

๋Œ“๊ธ€