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

[Python Algorithm] ๊ฐ€์žฅ ๊ธด ๋ฐ”์ดํ† ๋‹‰ ๋ถ€๋ถ„ ์ˆ˜์—ด BOJ #11054

by seolhee2750 2022. 6. 27.
๋ฌธ์ œ

https://www.acmicpc.net/problem/11054

 

11054๋ฒˆ: ๊ฐ€์žฅ ๊ธด ๋ฐ”์ดํ† ๋‹‰ ๋ถ€๋ถ„ ์ˆ˜์—ด

์ฒซ์งธ ์ค„์— ์ˆ˜์—ด A์˜ ํฌ๊ธฐ N์ด ์ฃผ์–ด์ง€๊ณ , ๋‘˜์งธ ์ค„์—๋Š” ์ˆ˜์—ด A๋ฅผ ์ด๋ฃจ๊ณ  ์žˆ๋Š” Ai๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (1 ≤ N ≤ 1,000, 1 ≤ Ai ≤ 1,000)

www.acmicpc.net

 

๋‚ด ๋ฌธ์ œ ํ’€์ด
import sys

n = int(sys.stdin.readline().strip())
nums = list(map(int, sys.stdin.readline().strip().split()))
r_nums = list(reversed(nums))
memory1 = [0 for _ in range(n)]
memory2 = [0 for _ in range(n)]
answer = 0

memory1[0] = 1
for i in range(1, n):
    maxi = 0
    for j in range(i):
        if nums[j] < nums[i]:
            maxi = max(maxi, memory1[j])
    memory1[i] = maxi + 1

memory2[0] = 1
for i in range(1, n):
    maxi = 0
    for j in range(i):
        if r_nums[j] < r_nums[i]:
            maxi = max(maxi, memory2[j])
    memory2[i] = maxi + 1
memory2 = list(reversed(memory2))

for i in range(n):
    answer = max(answer, memory1[i] + memory2[i])

print(answer - 1)

๐Ÿ‘‰ DP ๋ฌธ์ œ

memory1, memory2๋ฅผ ๋งŒ๋“ค๊ณ ,

๊ฐ๊ฐ ์•ž์—์„œ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋Š” ๊ฐ€์žฅ ๊ธด ๋ถ€๋ถ„ ์ˆ˜์—ด, ๋’ค์—์„œ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋Š” ๊ฐ€์žฅ ๊ธด ๋ถ€๋ถ„ ์ˆ˜์—ด์„ ๊ตฌํ–ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  memory1๊ณผ memory2๋ฅผ ๋น„๊ตํ•˜๋ฉฐ ๊ฐ™์€ ์ธ๋ฑ์Šค์˜ ์š”์†Œ ํ•ฉ์ด ๊ฐ€์žฅ ํฐ ๊ฒƒ์„

๊ฐ€์žฅ ๊ธด ๋ฐ”์ดํ† ๋‹‰ ๋ถ€๋ถ„ ์ˆ˜์—ด์˜ ๊ผญ์ง€์ ์ด๋ผ๊ณ  ํŒ๋‹จํ•ด์ฃผ์—ˆ๋‹ค.

 

๐Ÿ’ก ํ”ผ๋“œ๋ฐฑ
  • ๋ถ€๋ถ„ ์ˆ˜์—ด์„ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•๋งŒ ์•Œ๊ณ  ์žˆ๋‹ค๋ฉด ๊ต‰์žฅํžˆ ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์˜€๋‹ค.

๋Œ“๊ธ€