# 가장 긴 증가하는 부분 수열 3
import sys
import bisect
input = sys.stdin.readline
N = int(input())
arr = list(map(int, input().split()))
result = [arr[0]]
for i in range(1, N):
if arr[i] > result[-1]:
result.append(arr[i])
else:
index = bisect.bisect_left(result, arr[i])
result[index] = arr[i]
print(len(result))
이진탐색, bisect
1. 전체 리스트를 돌면서
- result의 마지막 원소보다 크면 append를 해준다
- 작으면 bisect_left를 이용하여 해당 인덱스의 값을 바꿔준다
2. result배열의 길이를 출력
가장 긴 증가하는 부분 수열 2와 똑같은 풀이로 풀었다.
가장 긴 증가하는 부분 수열 2: https://2hs-rti.tistory.com/38
[백준] 12015번 - 가장 긴 증가하는 부분 수열 2 (파이썬)
# 가장 긴 증가하는 부분 수열 2 import sys import bisect input = sys.stdin.readline N = int(input()) arr = list(map(int, input().split())) result = [] result.append(arr[0]) for i in range(1, N): if..
2hs-rti.tistory.com
가장 긴 증가하는 부분 수열 (DP풀이) : https://2hs-rti.tistory.com/37
[백준] 11053번 - 가장 긴 증가하는 부분 수열 (파이썬)
# 가장 긴 증가하는 부분 수열 import sys N = int(input()) arr = list(map(int, sys.stdin.readline().split())) dp = [1] * N for i in range(1, N): for j in range(i): if arr[j] < arr[i]: dp[i] = max(dp[..
2hs-rti.tistory.com
'Algorithm > BOJ' 카테고리의 다른 글
[백준] 4386번 - 별자리 만들기 (파이썬) (0) | 2022.02.09 |
---|---|
[백준] 17136번 - 색종이 붙이기 (파이썬) (0) | 2022.02.07 |
[백준] 11437번 - LCA (파이썬) (0) | 2022.02.07 |
[프로그래머스] - 자동완성 (0) | 2022.02.07 |
[백준] 17471번 - 게리맨더링 (파이썬) (0) | 2022.02.05 |