- Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSolution134.java
More file actions
Latest commit
23 lines (22 loc) · 687 Bytes
/
Copy pathSolution134.java
File metadata and controls
23 lines (22 loc) · 687 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
publicclassSolution134 {
publicintcanCompleteCircuit(int[] gas, int[] cost) {
intmaxProfit = -1;
inttotalProfit = 0;
intseat = -1;
for (inti = gas.length - 1; i > -1; i--) {
totalProfit += gas[i] - cost[i];
if (totalProfit > maxProfit) {
maxProfit = totalProfit;
seat = i;
}
}
/*保证至少满足gas>cost*/
if (totalProfit < 0) {
return -1;
}
returnseat;
}
publicstaticvoidmain(String[] args) {
System.out.println(newSolution134().canCompleteCircuit(newint[]{3, 3, 4}, newint[]{3, 4, 4}));
}
}