Skip to content

Commit e5a0ff0

Browse files
committed
[#15] Feat: Add 가장 긴 증가하는 부분 수열 2
1 parent 78d6982 commit e5a0ff0

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

12015.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# 가장 긴 증가하는 부분 수열 2
2+
# Binary Search, Stack
3+
4+
N = int(input())
5+
A = list(map(int, input().split()))
6+
7+
stack = []
8+
9+
10+
def binarySearch(arr, start, end, target):
11+
if (start >= end):
12+
return start
13+
mid = (start + end) // 2
14+
15+
if (arr[mid] == target):
16+
return mid
17+
if (arr[mid] < target):
18+
return binarySearch(arr, mid+1, end, target)
19+
return binarySearch(arr, start, mid, target)
20+
21+
22+
for a in A:
23+
if not stack or (stack[-1] < a):
24+
stack.append(a)
25+
else:
26+
i = binarySearch(stack, 0, len(stack), a)
27+
stack[i] = a
28+
29+
print(len(stack))

0 commit comments

Comments
 (0)