이진 검색 binary search의 여러 가지 형태(파이썬 코드)
1. 다음은 가장 잘 알려진 이진 검색 코드다. 찾으려는 값을 target으로 넣어주면 그 값에 해당하는 인덱스를 빠르게 찾아 리턴한다. a= [1,4,5,8,12] def binary_search(target): left = 0 right = len(a)-1 while left target: right = mid -1 else: left = mid + 1 return mid print('target:',4, 'find:',a[binary_search(4)]) print('target:',5, 'find:', a[binary_search(5)]) print('target:',8, 'find:', a[binary_search(8)]) print('target:',12, 'find:', a[binary_se..
2021. 6. 11.
[정렬 문제] 백준 2170번: 선 긋기 / 골드 5
https://www.acmicpc.net/problem/2170 2170번: 선 긋기 첫째 줄에 선을 그은 횟수 N(1 ≤ N ≤ 1,000,000)이 주어진다. 다음 N개의 줄에는 선을 그을 때 선택한 두 점의 위치 x, y(-1,000,000,000 ≤ x < y ≤ 1,000,000,000)가 주어진다. www.acmicpc.net import heapq n = int(input()) q = [] for i in range(n): n1, n2 = map(int, input().split()) heapq.heappush(q, (n1, n2)) start, end = heapq.heappop(q) answer = 0 while q: n1, n2 = heapq.heappop(q) if n1
2021. 6. 9.