Skip to content

Commit 27bad34

Browse files
authored
134. Gas Station (Imp. Queue qs.)
One of Important question from Queue
1 parent 46ce1a9 commit 27bad34

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

134. Gas Station

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//T.C - o(n)
2+
//S.C - o(1)
3+
4+
class Solution {
5+
public:
6+
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
7+
//petrol ki kitni kami hai yani Ghat ti
8+
int deficit =0;
9+
//petrol kitna bacha hua hai
10+
int balance =0;
11+
///circuit kha se suru kre
12+
int start =0;
13+
for(int i =0;i<gas.size();i++){
14+
balance += gas[i] - cost[i];
15+
if(balance < 0){
16+
deficit += balance; // yahi par galti hoti hai...deficit ko increment krna padega // or deficit += abs(balance);
17+
start = i+1;
18+
balance =0;
19+
}
20+
}
21+
if(deficit + balance >=0){ // or if(balance >= deficit)
22+
return start;
23+
}
24+
else{
25+
return -1;
26+
}
27+
}
28+
};
29+
30+

0 commit comments

Comments
 (0)