KEEP GOING
[python] 백준 1654번 : 랜선 자르기 (이진탐색) 본문
반응형
https://www.acmicpc.net/problem/1654
1. 정답
# 랜선 자르기
k, n = map(int, input().split())
lines = [int(input()) for _ in range(k)]
first = 1
last = max(lines)
res=0
while(first<=last):
mid = (first+last)//2
target = 0
for line in lines:
target += line//mid
if target>=n:
first = mid+1
res=mid
else:
last = mid-1
print(res)
2. 강사님 풀이
# 랜선 자르기
def Count(len):
cnt = 0
for line in Line:
cnt+=line//len
return cnt
k, n = map(int, input().split())
Line=[]
res=0
largest = 0
for _ in range(k):
tmp = int(input())
Line.append(tmp)
#max함수 사용
largest = max(largest, tmp)
lt=1
rt=largest
while(lt<=rt):
mid = (lt+rt)//2
if Count(mid)>=n:
lt = mid+1
res=mid
else:
rt = mid-1
print(res)
반응형
'code review > binary search' 카테고리의 다른 글
[python] SWEA : 이진 탐색 (0) | 2022.02.24 |
---|---|
[python] 프로그래머스 60060번 : 가사검색 (bisect) (0) | 2022.01.14 |
[python] 이것이코딩테스트다 : 정렬된 배열에서 특정 수의 개수 구하기 (bisect) (0) | 2022.01.13 |
Comments