-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGasStation.java
More file actions
47 lines (47 loc) · 1.35 KB
/
GasStation.java
File metadata and controls
47 lines (47 loc) · 1.35 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
package medium;
/**
*
* ClassName: GasStation
* @author chenyiAlone
* Create Time: 2019/04/24 17:50:24
* Description: No.134
* 思路:
* 1. 环形路径=> %
* 2. gas_total 车上的 gas 余量
* 3. 判断是能都能否到达下一个位置,只有当 j > i 时才更新 i 为 j
*/
public class GasStation {
public int canCompleteCircuit(int[] gas, int[] cost) {
int len = gas.length;
for (int i = 0; i < len; i++) {
int gas_total = 0;
int j = i;
while (j < len) {
if (gas_total + gas[j] >= cost[j]) {
gas_total = gas_total - cost[j] + gas[j];
} else {
break;
}
if ((j + 1) % len == i) {
return i;
}
j = (j + 1) % len;
}
if (i < j)
i = j;
/*
for (int j = i; j < len; j = (j + 1) % len) {
if (gas_total + gas[j] >= cost[j]) {
gas_total = gas_total - cost[j] + gas[j];
} else {
break;
}
if ((j + 1) % len == i) {
return i;
}
}
*/
}
return -1;
}
}