[오답1] 문제를 잘못읽었다! - 트럭이 순서대로 건너가는 것이지 가장 빠르게 건너가는 경우가 아니다.
def solution(bridge_length, weight, truck_weights):
answer = 1
bridge=[]
#max값을 먼저 가게한다.
#min값을 더해 리미트에 안걸리면 min값을 다음에 출발시킨다.
while len(truck_weights)>0 or len(bridge)>0:
answer+=1
bridge_sum=0
for i in range(len(bridge)):
bridge_sum+=bridge[i][0]
if len(truck_weights)==0:
delnum=-1
elif bridge_sum+max(truck_weights)<=weight:
bridge_sum+=max(truck_weights)
tmplist=[max(truck_weights),bridge_length]
bridge.append(tmplist)
truck_weights.remove(max(truck_weights))
elif bridge_sum+min(truck_weights)<=weight:
bridge_sum+=min(truck_weights)
tmplist=[min(truck_weights),bridge_length]
bridge.append(tmplist)
truck_weights.remove(min(truck_weights))
delnum=-1
for i in range(len(bridge)):
bridge[i][1]-=1
if bridge[i][1] == 0:
delnum+=1
if delnum != -1:
del bridge[0]
return answer
[정답]
def solution(bridge_length, weight, truck_weights):
answer = 1
bridge=[]
#max값을 먼저 가게한다.
#min값을 더해 리미트에 안걸리면 min값을 다음에 출발시킨다.
while len(truck_weights)>0 or len(bridge)>0:
answer+=1
bridge_sum=0
for i in range(len(bridge)):
bridge_sum+=bridge[i][0]
if len(truck_weights)==0:
delnum=-1
elif bridge_sum+truck_weights[0]<=weight:
bridge_sum+=truck_weights[0]
tmplist=[truck_weights[0],bridge_length]
bridge.append(tmplist)
truck_weights.remove(truck_weights[0])
delnum=-1
for i in range(len(bridge)):
bridge[i][1]-=1
if bridge[i][1] == 0:
delnum+=1
if delnum != -1:
del bridge[0]
return answer
'COMPUTER > 프로그래머스' 카테고리의 다른 글
[Python] 피로도 (0) | 2021.12.08 |
---|---|
[Python] 조이스틱 (0) | 2021.12.02 |
[Python]구명보트 (0) | 2021.11.22 |
[Python] 주식가격 (0) | 2021.11.16 |
[Oracle]입양 시각 구하기(2) (0) | 2021.11.15 |
댓글