다리를 지나는 트럭 문제 바로가기
나의 풀이 from collections import deque def solution(bridge_length, weight, truck_weights): time = 0 bridge = [0] * bridge_length bridge_sum = 0 bridge = deque(bridge) truck_weights = deque(truck_weights) while truck_weights: bridge_sum -= bridge[-1] bridge.pop() time += 1 if bridge_sum + truck_weights[0] <= weight: bridge.insert(0, truck_weights.popleft()) else: bridge.insert(0, 0) bridge_sum += bridge[0] time += bridge_length return time 고수의 풀이 import collections DUMMY_TRUCK = 0 class Bridge(object): def __init__(self, length, weight): self._max_length = length self._max_weight = weight self._queue = collections.deque() self._current_weight = 0 def push(self, truck): next_weight = self._current_weight + truck if next_weight <= self._max_weight and len(self._queue) < self._max_length: self._queue.append(truck) self._current_weight = next_weight return True else: return False def pop(self): item = self._queue.popleft() self._current_weight -= item return item def __len__(self): return len(self._queue) def __repr__(self): return 'Bridge({}/{} : [{}])'.format(self._current_weight, self._max_weight, list(self._queue)) def solution(bridge_length, weight, truck_weights): bridge = Bridge(bridge_length, weight) trucks = collections.deque(w for w in truck_weights) for _ in range(bridge_length): bridge.push(DUMMY_TRUCK) count = 0 while trucks: bridge.pop() if bridge.push(trucks[0]): trucks.popleft() else: bridge.push(DUMMY_TRUCK) count += 1 while bridge: bridge.pop() count += 1 return count def main(): print(solution(2, 10, [7, 4, 5, 6]), 8) print(solution(100, 100, [10]), 101) print(solution(100, 100, [10, 10, 10, 10, 10, 10, 10, 10, 10, 10]), 110) if __name__ == '__main__': main() 출처
...