# 二分查找模板

def binarySearch(nums, target):
left = 0
right = len(nums) - 1
while left <= right:
mid = (right + left) // 2
if nums[mid] == target:
return True
elif nums[mid] < target:
left = mid + 1
else:
right = mid - 1
return False

nums = [1,2,3,4,5,7]
res = binarySearch(nums, 7)
print(res)


# 寻找左侧边界的二分查找

def binarySearch(nums, target):
left = 0
right = len(nums)
while left < right:
mid = (right + left) // 2
if nums[mid] == target:
right = mid
elif nums[mid] < target:
left = mid + 1
else:
right = mid
if left == len(nums):
return -1
if left == 0 and nums[left] != target:
return -1
return left

nums = [1,1,2,2,2,3]
res = binarySearch(nums, 2)
print(res)


# 寻找右侧边界的二分查找

def binarySearch(nums, target):
left = 0
right = len(nums)
while left < right:
mid = (right + left) // 2
if nums[mid] == target:
left = mid + 1
elif nums[mid] < target:
left = mid + 1
else:
right = mid
if right >= len(nums) and nums[right - 1] != target:
return -1
return right - 1

nums = [1,2,2,2,2,3,3,4]
res = binarySearch(nums, -2)
print(res)



# 包裹运输问题

def shipWithinDays(weights, d):
left = max(weights)
right = sum(weights) + 1 # 船的载重区间[left,right)
while left < right:
mid = (right + left) // 2
if canFinish(weights, d, mid):
right = mid
else:
left = mid + 1
return left

def canFinish(w, d, cap):
i = 0
for day in range(d):
maxCap = cap
while maxCap - w[i] >= 0:
i += 1
if i == len(w):
return True
maxCap = maxCap - w[i]
return False


for i in range(n):
if isOK(i):