-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_150369.java
More file actions
33 lines (29 loc) · 1.04 KB
/
_150369.java
File metadata and controls
33 lines (29 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Solution {
public long solution(int cap, int n, int[] deliveries, int[] pickups) {
long answer = 0;
int dIndex, pIndex;
dIndex = pIndex = n - 1;
while(dIndex >= 0 || pIndex >= 0){
// 배달, 수거 시작 인덱스 탐색
for(; (dIndex >= 0 && deliveries[dIndex]==0); dIndex--);
for(; (pIndex >= 0 && pickups[pIndex]==0); pIndex--);
// 결과값 추가
answer += (long)(Math.max(dIndex, pIndex)+1) * 2;
// 배달
dIndex = doDeliveryOrPickup(cap, deliveries, dIndex);
// 수거
pIndex = doDeliveryOrPickup(cap, pickups, pIndex);
}
return answer;
}
private int doDeliveryOrPickup(int cap, int[] arr, int index){
int Cap = cap;
while(index >= 0 && Cap > 0){
int sub = Math.min(Cap, arr[index]);
Cap -= sub;
arr[index] -= sub;
if(arr[index]==0) index--;
}
return index;
}
}