forked from SjxSubham/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path134. Gas Station.cpp
More file actions
30 lines (27 loc) · 791 Bytes
/
134. Gas Station.cpp
File metadata and controls
30 lines (27 loc) · 791 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
//T.C - o(n)
//S.C - o(1)
class Solution {
public:
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
//petrol ki kitni kami hai yani Ghat ti
int deficit =0;
//petrol kitna bacha hua hai
int balance =0;
///circuit kha se suru kre
int start =0;
for(int i =0;i<gas.size();i++){
balance += gas[i] - cost[i];
if(balance < 0){
deficit += balance; // yahi par galti hoti hai...deficit ko increment krna padega // or deficit += abs(balance);
start = i+1;
balance =0;
}
}
if(deficit + balance >=0){ // or if(balance >= deficit)
return start;
}
else{
return -1;
}
}
};