Post

๐ŸฆŠ ๊ฐ€์žฅ ๊ธด ๋ฐ”์ดํ† ๋‹‰ ๋ถ€๋ถ„ ์ˆ˜์—ด

1. ๋ฌธ์ œ ๋งํฌ

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


2. ์ฝ”๋“œ

Python3 31120KB 284ms

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
N  = int(input())

A = list(map(int, input().split()))

dp_up = [1] * N
dp_down = [1] * N

# ์˜ค๋ฆ„์ฐจ์ˆœ DP
for i in range(N):
    for j in range(i):
        if A[j] < A[i]:
            dp_up[i] = max(dp_up[j] + 1, dp_up[i])

# ๋‚ด๋ฆผ์ฐจ์ˆœ DP
for i in range(N - 1, -1, -1):
    for j in range(N - 1, i, -1):
        if A[j] < A[i]:
            dp_down[i] = max(dp_down[j] + 1, dp_down[i])
result = 0
for i in range(N):
    result = max(dp_up[i] + dp_down[i] - 1, result);
print(result)


3. ํ•ด์„ค

์‹œ์ž‘๋ถ€ํ„ฐ, ๋งˆ์ง€๋ง‰๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋Š” ์˜ค๋ฆ„์ฐจ์ˆœ DP๋ฅผ ๋‘๊ฐœ ๋งŒ๋“ฌ

๋‘ DP์˜ MAX ๊ฐ’์ด ์ฆ๊ฐ€ํ–ˆ๋‹ค๊ฐ€ ๊ฐ์†Œํ•˜๋Š” ๋ถ€๋ถ„ ์ˆ˜์—ด์ด ๋œ๋‹ค

This post is licensed under CC BY 4.0 by the author.