-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathP87_2.java
More file actions
26 lines (23 loc) · 923 Bytes
/
P87_2.java
File metadata and controls
26 lines (23 loc) · 923 Bytes
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
package ch21;
import java.util.Arrays;
public class P87_2 {
public int canCompleteCircuit(int[] gas, int[] cost) {
// 방문 가능한 입력값인지 필터링
if (Arrays.stream(gas).sum() < Arrays.stream(cost).sum())
return -1;
int start = 0, fuel = 0;
// 전체 주유소를 순회하면서 성립되지 않는 위치를 찾는다.
for (int i = 0; i < gas.length; i++) {
// 남은 기름으로 출발점이 안 되는 주유소가 있다면 이미 지나친 지점도 전부 출발점이 될 수 없으므로
// 출발점을 다음 지점으로 밀어낸다.
if (fuel + gas[i] - cost[i] < 0) {
start = i + 1;
fuel = 0;
} else {
// 남은 기름을 계속 누적한다.
fuel += gas[i] - cost[i];
}
}
return start;
}
}