프로그래머스-택배 배달과 수거하기

S_H_H·2023년 6월 12일
0

프로그래머스

목록 보기
2/15

프로그래머스 로고

프로그래머스 - 택배 배달과 수거하기


문제 설명

문제 풀이

풀이 설명

트럭이 한번 갔다가 돌아올때 최대한 물건을 배달하고 가져오면 되는 문제

코드 작성

        public long solution(int cap, int n, int[] deliveries, int[] pickups) {
            long answer = 0;
            int haveDeliverCount = 0;
            int canPickupCount = 0;

            for (int i = n - 1; i >= 0; i--) {
                if (deliveries[i] != 0 || pickups[i] != 0) {
                    int numberOfVisit = 0;
                    while (haveDeliverCount < deliveries[i] || canPickupCount < pickups[i]) {
                        numberOfVisit++;
                        haveDeliverCount += cap;
                        canPickupCount += cap;
                    }
                    haveDeliverCount -= deliveries[i];
                    canPickupCount -= pickups[i];
                    answer += (i + 1) * numberOfVisit * 2;
                }
            }

            return answer;
        }
profile
LEVEL UP

0개의 댓글