

나의 코드
def solution(bridge_length, weight, truck_weights):
answer = 0
torock_list = []
torock_time_list = []
while True:
# 다리를 건너고 있는 트럭이 없고 대기 트럭이 없으면 멈춰!!!!!
if len(torock_list) == 0 and len(truck_weights) == 0:
break
# 초는 계속 흘러가야 되니까 증감
answer += 1
# 트런 시간 리스트가 존재하면 (즉, 건너고 있는 트럭 리스트가 있으면)
index = 0
while index < len(torock_time_list):
torock_time_list[index] += 1
if torock_time_list[index] > bridge_length:
torock_time_list.pop(0)
torock_list.pop(0)
else:
index += 1
# 대기 트럭이 있고 건너고 있는 트럭 무게합과
# 대기 트럭 인덱스 0을 더한 값이 제한 무게를 넘지 않으면
# 건너고 있는 트럭 리스트에 삽입
# 트런 시간 리스트에도 삽입 (1초로)
if len(truck_weights) > 0 and sum(torock_list) + truck_weights[0] <= weight:
torock_list.append(truck_weights.pop(0))
torock_time_list.append(1)
print(answer)
return answer
solution(2, 10, [7, 4, 5, 6])
'코딩테스트 파이썬 > 파이썬 프로그래머스 2단계' 카테고리의 다른 글
| 소수 찾기 (0) | 2023.05.22 |
|---|---|
| 롤케이크 자르기 (0) | 2023.05.20 |
| 숫자 변환하기 (1) | 2023.05.17 |
| 2개 이하로 다른 비트 (0) | 2023.05.16 |
| 2 x n 타일링 (0) | 2023.05.15 |