-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGasStation.java
More file actions
33 lines (29 loc) · 759 Bytes
/
GasStation.java
File metadata and controls
33 lines (29 loc) · 759 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
27
28
29
30
31
32
33
import java.util.*;
class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
//Checking if there exist a sol
int total= 0;
int n= gas.length;
for(int i=0; i<n; i++)
total += gas[i] - cost[i];
if(total < 0) return -1;
int tank=0;
int start=0;
for(int i=0; i< n; i++){
tank += gas[i] - cost[i];
if(tank < 0){
start =i +1;
tank =0;
}
}
return start;
}
}
class GasStation{
public static void main(String[] args) {
Solution obj= new Solution();
int gas[]={1,2,3,4,5};
int cost[]={3,4,5,1,2};
System.out.println(obj.canCompleteCircuit(gas, cost));
}
}