Post

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

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

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


2. ์ฝ”๋“œ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import sys

input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
incresed_dp = [0] * n
decresed_dp = [0] * n

# ์ขŒ์ธก์—์„œ๋ถ€ํ„ฐ ์ฆ๊ฐ€ํ•˜๋Š” ๋ถ€๋ถ„ ์ˆ˜์—ด ํƒ์ƒ‰
for i in range(n):
    flag = 0
    for j in range(i - 1, -1, -1):
        if a[j] < a[i]:
            incresed_dp[i] = incresed_dp[j] + 1
            flag = max(flag, incresed_dp[j] + 1)
    if flag:
        incresed_dp[i] = flag
    else:
        incresed_dp[i] = 1

# ์šฐ์ธก์—์„œ๋ถ€ํ„ฐ ์ฆ๊ฐ€ํ•˜๋Š” ๋ถ€๋ถ„ ์ˆ˜์—ด ํƒ์ƒ‰
for i in range(n - 1, -1, -1):
    flag = 0
    for j in range(i + 1, n):
        if a[j] < a[i]:
            flag = max(flag, decresed_dp[j] + 1)
    if flag:
        decresed_dp[i] = flag
    else:
        decresed_dp[i] = 1


ans = list(map(sum, list(zip(incresed_dp, decresed_dp))))
print(max(ans) - 1)


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