<원리>
1. truck_weights의 요소를 bridge_on(다리 위의 요소)으로 옮길때 [truck_weights[i],1]의 형식으로 넣어준다.
1-1. current_weight(bridge_on의 현재무게)에 truck_weights[i]를 더해준다.
bridge_on의 [i][1]요소가 bridge_length와 같으면 리스트arrive로 내보낸다.
bridge_on에 새로들어오는 truck_weights[i] + bridge_on 이
weight보다 같거나 작으면 새로운 truck_weights[i]를 bridge_on에
넣는다.
len_truck_weights = len(truck_weights) current_weight = 0 arrive = [] bridge_on = [] / #처음 아무것도 없을때 입력 bridge_on.append([truck_weights[0],1]) current_weight += truck_weights[0] truck_weights.remove(truck_weights[0]) answer = 1 / #truck_weights요소가 다 없어질 때 까지 입출력 반복 while truck_weights: answer +=1 for i in bridge_on[::-1]: if i[1] == bridge_length: arrive.append(i[0]) bridge_on.remove(i) current_weight -= i[0] i[1] += 1 / if current_weight + truck_weights[0] <= weight: bridge_on.append([truck_weights[0],1]) current_weight += truck_weights[0] truck_weights.remove(truck_weights[0]) / #bridge_on에 남아있는 요소들이 처리될 때 까지 반복 while bridge_on: answer += 1 for i in bridge_on[::-1]: if i[1] == bridge_length: arrive.append(i[0]) bridge_on.remove(i) current_weight -= i[0] i[1] += 1 print(answer)
다소 복잡해 보이고 중복되는 부분이 많다...
<원리>
1. bridge_on = [0] * bridge_length를 해준다.
2. 1초마다 bridge_on에 하나의 값을 입력하고 하나의 값을 빼준다.
예) 1. bridge_on =[0,0,0,1] 2. bridge_on =[0,0,1,0] 3. bridge_on =[0,1,0,0] 4. bridge_on =[1,0,0,0]
조건
current_weight + truck_weights[0](새로 들어올 값) <= weight 일때만 bridge_on에 값을 추가한다.
def solution(bridge_length, weight, truck_weights): current_weight = 0 bridge_on = [0] * bridge_length sec = 0 while bridge_on: sec += 1 if bridge_on[0] > 0: current_weight -= bridge_on[0] bridge_on.pop(0) else: bridge_on.pop(0) / if truck_weights: if current_weight + truck_weights[0] <= weight: bridge_on.append(truck_weights.pop(0)) current_weight += bridge_on[-1] else: bridge_on.append(0) return sec