diff --git a/problems/src/backtracking/GasStation.cpp b/problems/src/backtracking/GasStation.cpp new file mode 100644 index 00000000..892d8fe1 --- /dev/null +++ b/problems/src/backtracking/GasStation.cpp @@ -0,0 +1,19 @@ +class Solution { +public: + int canCompleteCircuit(vector& gas, vector& cost) { + int n = gas.size(); + for(int i=0;i<=n;i++){ + vector summ(n+1,0); + int cnt = 0; + bool flag = true; + for(int j=i+1;cnt<=n;j++,cnt++){ + summ[j%n] = (summ[(j-1)%n] + gas[(j-1)%n] - cost[(j-1)%n]); + if(summ[j%n] < 0){ + flag=false; break; + } + } + if(flag) return i; + } + return -1; + } +}; \ No newline at end of file