프로그래머스
C# - 택배 배달과 수거하기
ybbro
2023. 4. 3. 20:31
https://school.programmers.co.kr/learn/courses/30/lessons/150369
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
using System;
public class Solution {
public long solution(int cap, int n, int[] deliveries, int[] pickups)
{
long answer = 0;
int deliver = 0;
int pickup = 0;
// 멀리 있는 배송지부터
for (int i = n - 1; i >= 0; --i)
{
// 해당 배송지에 배송할 or 수거할 상자가 있다면
if (deliveries[i] != 0 || pickups[i] != 0)
{
// 해당 배송지까지 왕복한 횟수 초기화
int repeat = 0;
// 해당 배송지에 배송, 수거할 상자의 수 이상이 되기 전까지
while (deliver < deliveries[i] || pickup < pickups[i])
{
// 왕복 횟수를 1회 더하고
++repeat;
// 1번 왕복할 때 cap만큼 배송, 회수할 수 있음
deliver += cap;
pickup += cap;
}
// 해당 배송지에 배송, 수거한 상자의 수를 빼줌
// (남은 배송, 수거한 상자는 다음 배송지로)
deliver -= deliveries[i];
pickup -= pickups[i];
// 0번째 집까지 편도로 1번 -> i+1이 편도에 걸리는 횟수
// 해당 배송지까지 편도 거리 * 2 * 왕복 횟수
answer += (long)((i + 1) * 2 * repeat);
}
}
return answer;
}
}