-
Notifications
You must be signed in to change notification settings - Fork 79
/
Solution.java
103 lines (84 loc) · 2.97 KB
/
Solution.java
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package seventeen;
/**
* @author dmrfcoder
* @date 2019/4/10
*/
public class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
//remaining为二维数组,remaing[index][0]代表车从第index出发到达index+1站后里面可以剩余的油,
// remaing[index][1]=index,因为后面要对remaing按照remaing[index][0]即剩余的油量排序,
// 所以要把原始的索引记录下来,排完序后直接从remaing[index][0]最大的值对应的remain[index][1]处开始开车即可
int[][] remaining = new int[gas.length][2];
//gas[i]---->gas[i+1] cost[i],计算remaining
for (int index = 0; index < gas.length; index++) {
remaining[index][0] = gas[index] - cost[index];
remaining[index][1] = index;
}
//按照remaining[index][0]对remaing进行排序
QuickSort(remaining, gas.length);
//由于上面排完序是降序,所以这里从后往前遍历
for (int index = gas.length - 1; index >= 0; index--) {
//如果剩余的油大于等于0,说明油足够到达下一站
if (remaining[index][0] >= 0) {
if (helper(gas, cost, remaining[index][1])) {
return remaining[index][1];
}
} else {
return -1;
}
}
return -1;
}
//模拟从第startIndex站出发,看能不能走完全程
boolean helper(int[] gas, int[] cost, int startIndex) {
int count = 0;
int curGas = 0;
for (int index = startIndex; count <= gas.length; count++, index = (index + 1) % gas.length) {
curGas += gas[index];
if (curGas >= cost[index]) {
curGas -= cost[index];
} else {
return false;
}
}
return true;
}
void QuickSort(int[][] v, int length) {
QSort(v, 0, length - 1);
}
void QSort(int[][] v, int low, int high) {
if (low >= high) {
return;
}
int t = Partition(v, low, high);
QSort(v, low, t - 1);
QSort(v, t + 1, high);
}
int Partition(int[][] v, int low, int high) {
int pivotkey;
pivotkey = v[low][0];
while (low < high) {
while (low < high && v[high][0] >= pivotkey) {
--high;
}
int[] t;
t = v[low];
v[low] = v[high];
v[high] = t;
while (low < high && v[low][0] <= pivotkey) {
++low;
}
t = v[low];
v[low] = v[high];
v[high] = t;
}
return low;
}
public static void main(String[] args) {
Solution solution = new Solution();
int[] gas = {3, 2, 5, 3, 4, 3};
int[] cost = {2, 3, 4, 4, 3, 4};
int index = solution.canCompleteCircuit(gas, cost);
System.out.println("startIndex is: " + index);
}
}