๐Ÿค– ์•Œ๊ณ ๋ฆฌ์ฆ˜

baekjoon. 15486 ํ‡ด์‚ฌ 2 [Gold V][python]

ttoance 2023. 7. 6. 14:16

 

https://ddoance.tistory.com/108

 

โšก๏ธ baekjoon. 14501 ํ‡ด์‚ฌ [Silver III][python]

๊ด€๋ จ ํ’€์ด๋ฅผ ๋ดค์„๋•Œ ํƒ์š•๋ฒ•์œผ๋กœ ์ ‘๊ทผํ•ด์•ผ ํ•˜๋‚˜ ํ•˜๋‹ค๊ฐ€, ์ฃผ์–ด์ง„ ์˜ˆ์‹œ ์ผ€์ด์Šค๋กœ ๋„์ €ํžˆ ์ ‘๊ทผํ•  ์ˆ˜๊ฐ€ ์—†์–ด์„œ, dfs๋กœ ์ ‘๊ทผํ•ด์„œ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜์— ๋Œ€ํ•ด ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๊ฐ€ ์ตœ๋Œ“๊ฐ’์„ ๋ณด์—ฌ์ฃผ๋Š” ์ ‘๊ทผ ๋ฐฉ์‹์„

ddoance.tistory.com

 

์ด ๋ฌธ์ œ์˜ ์–ด๋ ค์šด ๋ฒ„์ „์œผ๋กœ, Gold V ๋ ˆ๋ฒจ์ด ์žˆ๋‹ค. 

์ด ํ’€์ด๋กœ ์ž‘๋™ํ•˜์ง€ ์•Š์•˜๊ณ , ๊ฒฐ๊ตญ dp๋กœ ๋‹ค์‹œ ํ’€์–ด์•ผ ํ–ˆ๋‹ค.

 

์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•  ๊ฒฝ์šฐ, input์„ ๋ฐ›๋Š” ํ˜•์‹์„ ๋ฐ”๊ฟ”์ค˜์•ผ ํ•œ๋‹ค. 

https://animoto1.tistory.com/entry/%ED%8C%8C%EC%9D%B4%EC%8D%ACPython-%EC%8B%9C%EA%B0%84-%EC%B4%88%EA%B3%BC-%ED%95%B4%EA%B2%B0%EB%B0%A9%EB%B2%95

 

ํŒŒ์ด์ฌ(Python) ์‹œ๊ฐ„ ์ดˆ๊ณผ ํ•ด๊ฒฐ๋ฐฉ๋ฒ•

ํŒŒ์ด์ฌ(Python) ์‹œ๊ฐ„ ์ดˆ๊ณผ ํ•ด๊ฒฐ๋ฐฉ๋ฒ• โ‘  input() ๋Œ€์‹  sys.stdin.readline() ์‚ฌ์šฉํ•˜๊ธฐ input() ๋‚ด์žฅํ•จ์ˆ˜๋Š” sys.stdin.readline()๊ณผ ๋‹ฌ๋ฆฌ prompt message๋ฅผ ์ถœ๋ ฅํ•˜๊ณ , ๊ฐœํ–‰ ๋ฌธ์ž๋ฅผ ์‚ญ์ œํ•œ ๊ฒƒ์„ ๋ฆฌํ„ดํ•˜๊ธฐ์—(์ฆ‰, rstrip()๋ฅผ ์ 

animoto1.tistory.com

 

 

 

์ตœ์ข… ํ’€์ด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. 

import sys
input = sys.stdin.readline 

N = int(input())


meetingList = list()
for _ in range(N):
    meetingList.append(list(map(int, input().split())))


dp = [0] * (N + 1)

for i in range(1, N + 1):
    time = meetingList[i - 1][0]
    price = meetingList[i - 1][1]

    # ํšŒ์˜ ์ฐธ์„ํ•  ๊ฒฝ์šฐ 
    if (i + time - 1) <= len(meetingList):
        dp[i + time - 1] = max(dp[i + time - 1], dp[i - 1] + price)

    ### ํšŒ์˜ ์ฐธ์„ ์•ˆํ•  ๊ฒฝ์šฐ, ๊ทธ ์ „๋‚  ๊ฐ’์„ ๊ฐ€์ ธ์˜ด 
    if dp[i - 1] > dp[i]:
        dp[i] = dp[i - 1]
    ###
    # print(i, ' >> ', dp)

print(max(dp))
๋ฐ˜์‘ํ˜•