본문 바로가기

알고리즘

프로그래머스 - 다리를 지나가는 트럭 (파이썬)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
def solution(bridge_length, weight, truck_weights):
    stack = []
    success = []
    current_weight = 0
    cnt = 0
    while True:
        if success and not stack and not current_weight: break
        for i in range(len([j for j in stack])):
            stack[i][1+= 1
        for i in [j for j in stack if j[1> bridge_length]:
            current_weight -= i[0]
            success.append(i[0])
            stack.remove(i)
        try:
            if weight - current_weight >= truck_weights[0]:
                truck_weight = truck_weights.pop(0)
                current_weight += truck_weight
                stack.append([truck_weight, 1])
        exceptpass
        cnt += 1
    return cnt
cs