강의

멘토링

커뮤니티

Inflearn Community Q&A

jiyeong10069079's profile image
jiyeong10069079

asked

Introduction to Python Algorithm Problem Solving (Coding Test Preparation)

2. Cut the LAN cable (decision algorithm)

샘 코드가.. 제꺼는 뭐가 문제인건지요..

Written on

·

171

0

import sys

#sys.stdin=open('input.txt','rt')

k,n=map(int,input().split())

Line=[]

res=0

for i in range(k):

    tmp=int(input())

    Line.append(tmp)

min_val=min(Line)

result=[]

#for j in range(1,min_val+1):

#    total=0

#    for idx,i in enumerate(Line):

#        total+=i//j

#    if total==n:

#        result.append(j)

##         break

#print(result[-1])

for j in range(1,min_val+1):

    total=0

    for idx,i in enumerate(Line):

        total+=i//(min_val-j)

    if total==n:

        result=total

        break

print(min_val-j)

보니까 다 타임 리밋 걸리는데 함수로 for 문돌리는것도 똑같은데 결과는 주피터 노트북서 똑같은데 왜이러는지 모르겠네요 ㅠㅠ

python코테 준비 같이 해요!

Answer 1

0

codingcamp님의 프로필 이미지
codingcamp
Instructor

자르는 랜선의 길이를 정할 때는 이분검색을 통해 정해야 타임리밋이 나지 않습니다. 그리고 위 코드는 에러가 나는데 j값이 min_val값이 되면 (min_val-j)값이 0이 됩니다. 0으로 수를 나눌수는 없습니다. 

min_val=min(Line) -가장 짧은 랜선보다 길게 해서 n개의 랜선을 만들 수 있습니다.

if total==n: --> if total>=n: 자른 랜선의 개수가 n보다 커도 답이 될 수 있습니다.

입력되는 각 랜선의 길이는 2^31-1크기의 자연수로 입력된다고 문제를 수정해 놓겠습니다.

jiyeong10069079's profile image
jiyeong10069079

asked

Ask a question