-
택배상자코딩 테스트/Level 2 2022. 11. 9. 18:27반응형
https://school.programmers.co.kr/learn/courses/30/lessons/131704
간단하게 스택으로 처리하기 위해 order의 방향을 바꿈...
변화를 체크하기 위해 change 변수를 사용..
문제를 그대로 코드로...
def solution(order): belt = list(range(len(order), 0, -1)) order.reverse() sub_belt = [] truck = [] change = True while change: change = False if belt and order and order[-1] == belt[-1]: belt.pop() truck.append(order.pop()) change = True elif sub_belt and order and order[-1] == sub_belt[-1]: sub_belt.pop() truck.append(order.pop()) change = True elif belt: sub_belt.append(belt.pop()) change = True return len(truck)
truck을 변수로...
change 변수 제거...def solution(order): order.reverse() belt = list(range(len(order), 0, -1)) sub_belt = [] truck = 0 while True: if belt and order and order[-1] == belt[-1]: belt.pop() order.pop() truck += 1 elif sub_belt and order and order[-1] == sub_belt[-1]: sub_belt.pop() order.pop() truck += 1 elif belt: sub_belt.append(belt.pop()) else: break return truck
반응형