Baekjoon/이분탐색

Baekjoon/이분탐색

[백준] 2805 나무 자르기 (실버2) / 이분탐색

문제요약 나의 코드 및 설명 n, m = map(int, input().split()) #n:나무의 수 m:목표나무의길이 tree = list(map(int, input().split())) tree.sort() # 10 15 17 20 result = [] ## 절단기의 높이를 0~나무의높이의 최대값으로 지정할 수 있음(즉, 0~20) start = 0 #절단기의 높이의 시작값:0 end = tree[-1] #절단기의 높이의 끝값: 나무의높이의 최대값 while start = mid: #설정한 절단기의 높이보다 나무가 크다면 자를 수 있으므로 cutting += (t-mid) #잘랐을때의 나무의 길이의 총합에 저장 if cutting >= m: #만약 나무의 길이의 총합이 목표값 이상이라면 result..

Baekjoon/이분탐색

[백준] 10816 숫자 카드 2 (실버4) / 이분탐색

문제요약 나의 코드 및 설명 n = int(input()) s = list(map(int, input().split())) s.sort() d = {} #개수 담을 딕셔너리 생성 for i in range(len(s)): if s[i] not in d: #만약 딕셔너리에 없다면 d[s[i]] = 1 #해당 값에 카운트 1 else: #만약 이미 딕셔너리에 있다면 d[s[i]] += 1 #해당 값에 카운트 += 1 m = int(input()) num_list = list(map(int, input().split())) result = [] #이분탐색 시작 for x in num_list: #두 번째 숫자리스트에 있는 값을 하니씩 확인 start = 0 #시작 인덱스 : 0 end = n-1 #끝 인덱스 ..

Baekjoon/이분탐색

[백준] 10815 숫자 카드 (실버5) / 이분탐색

문제요약 나의 코드 및 설명 n = int(input()) a = list(map(int, input().split())) a.sort() m = int(input()) b = list(map(int, input().split())) result = [] for x in b: start = 0 end = n-1 ans = False while start x: end = mid-1 elif a[mid] < x: start = mid+1 else: ans = True break if ans: result.append(1) else: result.append(0) print(" ".join(map(str, result))) 피드백 이전에 풀었던 이분탐색 문제와 크게 다르지 않은 문제여서 역시나 이분탐색 알고리..

Baekjoon/이분탐색

[백준] 1920 수 찾기 (실버4) / 이분탐색

문제요약 나의 코드 및 설명 n = int(input()) a = list(map(int, input().split())) a.sort() #비교 대상 리스트 정렬 m = int(input()) b = list(map(int, input().split())) for x in b: start = 0 #시작 : 인덱스 0 end = n-1 #끝 : 마지막 인덱스 ans = False while start end가 아닐 때까지 mid = (start + end) // 2 #중간값을 찾는다. if x > a[mid]: #찾는 값이 중간값보다 크다면, start = mid + 1 #다음 반복 시에 중간값 이후의 값 부터 찾는다. elif x < a[mid]: #찾는 값이 중간값보다 작다면, end = mid - ..

hellosonic
'Baekjoon/이분탐색' 카테고리의 글 목록