Skip to content

Commit 282b6fc

Browse files
committed
[#15] Feat: Add 가장 긴 증가하는 부분 수열
DP 로 문제 해결
1 parent e5a0ff0 commit 282b6fc

File tree

1 file changed

+6
-35
lines changed

1 file changed

+6
-35
lines changed

11053.py

+6-35
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,14 @@
22

33
# Dynamic Programming
44

5-
# N = int(input())
6-
# A = list(map(int, input().split()))
7-
8-
# dp = [1 for _ in range(N)]
9-
10-
# for i in range(N):
11-
# for j in range(i):
12-
# if A[i] > A[j]:
13-
# dp[i] = max(dp[i], dp[j]+1)
14-
15-
# print(dp)
16-
17-
# Binary Search, Stack
18-
195
N = int(input())
206
A = list(map(int, input().split()))
217

22-
stack = []
23-
24-
25-
def binarySearch(arr, start, end, target):
26-
if (start >= end):
27-
return start
28-
mid = (start + end) // 2
29-
30-
if (arr[mid] == target):
31-
return mid
32-
if (arr[mid] < target):
33-
return binarySearch(arr, mid+1, end, target)
34-
return binarySearch(arr, start, mid, target)
35-
8+
dp = [1 for _ in range(N)]
369

37-
for a in A:
38-
if not stack or (stack[-1] < a):
39-
stack.append(a)
40-
else:
41-
i = binarySearch(stack, 0, len(stack), a)
42-
stack[i] = a
10+
for i in range(N):
11+
for j in range(i):
12+
if A[i] > A[j]:
13+
dp[i] = max(dp[i], dp[j]+1)
4314

44-
print(len(stack))
15+
print(max(dp))

0 commit comments

Comments
 (0)