๋ฌธ์
https://www.acmicpc.net/problem/11054
๋ด ๋ฌธ์ ํ์ด
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๋ฅผ ๋น๊ตํ๋ฉฐ ๊ฐ์ ์ธ๋ฑ์ค์ ์์ ํฉ์ด ๊ฐ์ฅ ํฐ ๊ฒ์
๊ฐ์ฅ ๊ธด ๋ฐ์ดํ ๋ ๋ถ๋ถ ์์ด์ ๊ผญ์ง์ ์ด๋ผ๊ณ ํ๋จํด์ฃผ์๋ค.
๐ก ํผ๋๋ฐฑ
- ๋ถ๋ถ ์์ด์ ๊ตฌํ๋ ๋ฐฉ๋ฒ๋ง ์๊ณ ์๋ค๋ฉด ๊ต์ฅํ ์ฝ๊ฒ ํ ์ ์๋ ๋ฌธ์ ์๋ค.
'4๏ธโฃ Python > Problem Solving' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Python Algorithm] ๋ฏธ๋ก1 SWEA #1226 (0) | 2022.07.05 |
---|---|
[Python Algorithm] ๋ณด๊ธ๋ก SWEA #1249 (0) | 2022.07.05 |
[Python Algorithm] ๋ ์ด์ ํต์ BOJ #6087 (0) | 2022.06.24 |
[Python Algorithm] ํ๋ก์ด๋ BOJ #11404 (0) | 2022.06.23 |
[Python Algorithm] ๊ฒฝ๋ก ์ฐพ๊ธฐ BOJ #11403 (feat. BFS, ํ๋ก์ด๋ ์์ ) (0) | 2022.06.23 |
๋๊ธ