알고리즘

프로그래머스 택배 배달과 수거하기(JAVA)

박카스마시며코딩 2023. 4. 1. 18:23

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

 

프로그래머스

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

programmers.co.kr

 

 

 

class Solution {
    public long solution(int cap, int n, int[] deliveries, int[] pickups) {
        long answer = 0;
        int delIndex = -1;
        int pickIndex = -1;
        for(int i = 0 ; i < n ; i++){
            if(deliveries[i] > 0) {
                delIndex = i;
            }
            if(pickups[i] > 0) {
                pickIndex = i;
            }
        }
        while(delIndex > -1 || pickIndex > -1){
            answer += 2 * Math.max(delIndex+1, pickIndex+1);
            int cnt = 0;
            while(delIndex > -1 && cnt < cap){
                deliveries[delIndex]--;
                while(delIndex >= 0 && deliveries[delIndex] <= 0){
                    delIndex--;
                }
                cnt++;
            }
            cnt = 0;
            while(pickIndex > -1 && cnt < cap){
                pickups[pickIndex]--;
                while(pickIndex >= 0 && pickups[pickIndex] <= 0){
                    pickIndex--;
                }
                cnt++;
            }
        }
        return answer;
    }
}