-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPractice4.java
More file actions
49 lines (39 loc) · 1.31 KB
/
Copy pathPractice4.java
File metadata and controls
49 lines (39 loc) · 1.31 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package algorithm.greedy;
// Practice
// 원형 루트 상에 n 개의 주유소가 있다.
// 각 주유소의 가스 보유량은 gas 배열에 주어지고,
// 해당 주유소에서 다음 주유소로의 이동 비용은 cost 배열에 주어진다.
// 어떤 위치에서 차량이 가스를 채워 출발하면 모든 주유소를 방문하고 원래의 위치로 돌아올 수 있는지
// 계산하는 프로그램을 작성하세요.
// 입출력 예시
// gas: 1, 2, 3, 4, 5
// cost: 3, 4, 5, 1, 2
// 출력: 3
// gas: 4, 5, 1, 2, 3
// cost: 1, 2, 3, 4, 2
// 출력: -1
public class Practice4 {
public static int solution(int[] gas, int[] cost) {
int cur = 0;
int total = 0;
int startPos = 0;
for (int i = 0; i < gas.length; i++) {
cur += gas[i] - cost[i];
total += gas[i] - cost[i];
if (cur < 0) {
startPos = i + 1;
cur = 0;
}
}
return total >= 0 ? startPos : -1;
}
public static void main(String[] args) {
// Test code
int[] gas = {1, 2, 3, 4, 5};
int[] cost = {3, 4, 5, 1, 2};
System.out.println(solution(gas, cost));
gas = new int[]{2, 3, 4};
cost = new int[]{3, 4, 3};
System.out.println(solution(gas, cost));
}
}