https://school.programmers.co.kr/learn/courses/30/lessons/150369

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

2023 카카오 블라인드 2번 문제입니다. 

 

가장 먼 택배 업무의 위치가 n이라고 했을때 왕복으로 2 * n을 움직이면서 수거와 배달 두가지 일을 할 수 있다는 아이디어만 떠올리면 풀립니다.

 

다만 구현할때 실수하기 쉬운 부분이 꽤 있었습니다. (빈 집을 뛰어넘기, 초기 데이터 정리 등등)

 

def solution(cap, n, deliveries, pickups):
    answer = 0

    pi, di = n, n

    while not (pi == 0 and di == 0):
        while pi >= 1 and pickups[pi - 1] == 0:
            pi -= 1
        while di >= 1 and deliveries[di - 1] == 0:
            di -= 1

        answer += (max(pi, di)) * 2

        c = cap
        while c > 0 and pi >= 1:
            d = min(c, pickups[pi - 1])
            pickups[pi - 1] -= d
            c -= d
            if pickups[pi - 1] == 0:
                pi -= 1

        c = cap
        while c > 0 and di >= 1:
            d = min(c, deliveries[di - 1])
            deliveries[di - 1] -= d
            c -= d
            if deliveries[di - 1] == 0:
                di -= 1

    return answer

 

728x90

+ Recent posts