문제풀이/이분탐색

https://leetcode.com/problems/median-of-two-sorted-arrays/description/코드 (이분탐색)class Solution: def findMedianSortedArrays(self, nums1, nums2) -> float: n = len(nums1) m = len(nums2) if n > m: return self.findMedianSortedArrays(nums2, nums1) left, right = 0, n while left min2: right = partition1-1 else: lef..
https://www.acmicpc.net/problem/13397코드import sysinput = sys.stdin.readlinen, m = map(int, input().split())arr = list(map(int, input().split()))def count_section(target): max_value = min_value = arr[0] cnt = 1 for i in range(1, n): if max_value arr[i]: min_value = arr[i] if max_value - min_value > target: cnt += 1 max_value = min_value = a..
https://www.acmicpc.net/problem/17393코드n = int(input())inks = list(map(int, input().split()))viscosities = list(map(int, input().split()))def binary_search(start, target): l, r = start, n-1 res = n-1 while l  칸이 최대 10^18개까지 존재하고, 점도지수가 오름차순으로 정렬된채로 주어지므로 이분탐색을 쓰라는 문제인 것을 알 수 있다. 현재 칸에서부터 오른쪽 끝칸 중에서 점도지수가 현재 칸의 잉크 지수(A_i) 이하인 칸을 찾으면 된다. 이때 예제 1의 10, 10, 10과 같이 점도지수가 같은 값이 연달아서 나오는 경우도 있으..
https://www.acmicpc.net/problem/16564 코드n, k = map(int, input().split())x = [int(input()) for _ in range(n)]s, e = 1, int(1e9)+1ans = 0while s  기본적인 매개 변수 탐색 방법으로 풀 수 있다.다만, 조심해야 할 것은 초기 s와 e 값이다. 처음에는 e의 초기값을 int(1e9)으로 두고 했더니 틀렸었다. 다시 보니 이렇게 하면 가능한 팀 최대 레벨이 1,000,000,000인 경우를 구할 수 없기 때문이었다. s = 1, e = 10이라고 두고 가능한 팀 최대 레벨이 10이 되는 것을 구하는 경우를 생각해보니 mid=10이 되기 전에 s=10, e=10이 되어서 while문을 빠져나오기 때문에..
https://www.acmicpc.net/problem/16401 코드m, n = map(int, input().split())l = list(map(int, input().split()))s, e = 1, max(l)ans = 0while s = m: s = mid+1 ans = mid else: e = mid-1print(ans)
딜레이레이
'문제풀이/이분탐색' 카테고리의 글 목록