Skip to content

Commit a09c61e

Browse files
authored
Merge pull request #1076 from AlgorithmWithGod/0224LJH
[20251009] PGM / Lv2 / 택배 배달과 수거하기 / 이종환
2 parents 8c7bcca + 5c3f544 commit a09c61e

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
6+
class Solution {
7+
8+
static int limit,houseCnt,pIdx, dIdx;
9+
static long totalCost;
10+
static House[] houses;
11+
12+
static class House implements Comparable<House>{
13+
int idx;
14+
int delivery;
15+
int pickup;
16+
17+
public House(int idx, int delivery, int pickup ){
18+
this.idx = idx;
19+
this.delivery = delivery;
20+
this.pickup = pickup;
21+
}
22+
23+
@Override
24+
public int compareTo(House h){
25+
return Integer.compare(h.idx, this.idx); // 거리 높은순으로 나옴
26+
}
27+
}
28+
29+
public long solution(int cap, int n, int[] deliveries, int[] pickups) {
30+
limit = cap;
31+
houseCnt = n;
32+
totalCost = 0;
33+
houses = new House[houseCnt+1];
34+
for (int i = 1; i<= houseCnt; i++){
35+
houses[i] = new House(i, deliveries[i-1], pickups[i-1]);
36+
}
37+
38+
process();
39+
40+
41+
long answer = totalCost;
42+
return answer;
43+
}
44+
45+
private void process(){
46+
dIdx = houseCnt;
47+
pIdx = houseCnt;
48+
49+
while (dIdx != 0 || pIdx != 0){
50+
processEach();
51+
}
52+
}
53+
54+
55+
56+
private void processEach(){
57+
int dLeft = limit;
58+
int pLeft = limit;
59+
60+
while(dIdx != 0 && houses[dIdx].delivery == 0){
61+
dIdx--;
62+
}
63+
while(pIdx != 0 && houses[pIdx].pickup == 0){
64+
pIdx--;
65+
}
66+
67+
int curCost = Math.max(dIdx, pIdx) * 2;
68+
// System.out.println(curCost + " ->" + dIdx + " "+ pIdx);
69+
totalCost += curCost;
70+
71+
while(dIdx != 0 && dLeft != 0){
72+
House h = houses[dIdx];
73+
if (h.delivery > dLeft){
74+
h.delivery -= dLeft;
75+
dLeft = 0;
76+
break;
77+
}
78+
79+
dLeft -= h.delivery;
80+
h.delivery = 0;
81+
dIdx--;
82+
}
83+
84+
while(pIdx != 0 && pLeft != 0){
85+
House h = houses[pIdx];
86+
if (h.pickup > pLeft){
87+
h.pickup -= pLeft;
88+
pLeft = 0;
89+
break;
90+
}
91+
92+
pLeft -= h.pickup;
93+
h.pickup = 0;
94+
pIdx--;
95+
}
96+
// System.out.println(curCost + " ->" + dIdx + " "+ pIdx);
97+
98+
}
99+
}
100+
```

0 commit comments

Comments
 (0)