Open
Conversation
Collaborator
Author
[이분탐색] 백준 12015 가장 긴 증가하는 부분 수열문제에서 구해야하는 값은 가장 긴 증가하는 부분 수열이 아니라 수열의 길이이다. 입력 배열이 가장 긴 증가하는 부분 수열의 길이를 구하는 과정은 아래와 같다.
def get_idx(arr, key): # key보다 크지만 가장 작은 값의 위치
min_v = 0
max_v = len(arr) - 1
while True:
if min_v > max_v:
return min_v
mid = (min_v + max_v) // 2
if arr[mid] < key:
min_v = mid + 1
else:
max_v = mid - 1
n = int(input()) # 10 20 30 15 20 25 50 45 55 60
array = list(map(int, input().split()))
result = [array[0]]
for i in array:
if i > result[-1]:
result.append(i)
else:
idx = get_idx(result, i)
result[idx] = i
print(len(result)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
[이분탐색] 백준 2805 나무자르기
입력이 위와 같이 주어졌을 때, 상근이가 가져가야할 나무 길이는 7이고, 나무 높이(arr)는 [20, 15, 10, 17]이다.
절단기 높이를 인덱싱하고, 나오는 나무 길이를 비교하여 이분탐색을 한다.
절단기 높이가 20일 때, 나무 길이는 0이고, 높이가 20-7=13일 때 나무 길이가 7+2+4=13이다.
즉, 절단기 높이의 최댓값은 max(arr)이고, 최솟값은 max(arr)-(필요한 길이)로 둘 수 있다.