[파이썬] bisect_left, bisect_right 직접 구현해보기


[파이썬] bisect_left, bisect_right 직접 구현해보기

bisect_left, bisect_right 직접 구현해보기 이분탐색을 이용하여 상한과 하한을 구해봅시다. biset_left, biset_right의 차이점은 if문 속 등호의 유무 뿐입니다. right에서 arr[m]이랑 val이랑 같아도 s를 올려야 상한이 구해지니까용.~ def bisect_left(arr, val): s, e = 0, len(arr) - 1 while s <= e: m = (s + e) // 2 if arr[m] < val: s = m + 1 else: e = m - 1 return s def bisect_right(arr, val): s, e = 0, len(arr) - 1 while s <= e: m = (s + e) // 2 if arr[m] <= val: s = m + 1 else: e = m - 1 return s...


#bisect_left #bisect_right #상한 #하한

원문링크 : [파이썬] bisect_left, bisect_right 직접 구현해보기