이분탐색 left, right, 조건 정하기
while (left <= right)
- 조건 따져가며
left=target+1
orright=target-1
으로 업데이트
return left
def binary(num):
left = 0
right = len(LIS)-1
mid = (left + right) // 2
while(left <= right):
if LIS[mid] == num:
return mid
elif LIS[mid] > num:
right = mid - 1
elif LIS[mid] < num:
left = mid + 1
return left
while (left + 1 < right)
- 조건 따져가며
left=target
orright=target
으로 업데이트
def solution(n, times):
answer = 10e14
times.sort()
left,right=-1,times[-1]*n
while(left+1<right):
total=0
target=(left+right)//2
for time in times:
total+=(target//time)
if total>=n:
answer=min(answer,target)
right=target
else:
left=target
return answer
def can_move(stones,target,k):
stack=0
for stone in stones:
if stone<target:
stack+=1
if stack==k: return False
else:
stack=0
return True
def solution(stones, k):
answer = 0
max,min=0,200000
for stone in stones:
if stone>max: max=stone
if stone<min: min=stone
while(min<=max):
target=(min+max)//2
if can_move(stones,target,k):
if target>answer: answer=target
min=target+1
else:
max=target-1
return answer
Share article