Advertisement
nikunjsoni

134

Jun 29th, 2021
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.40 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
  4.         int n=gas.size(), total=0, start=0, curr=0;
  5.         for(int i=0; i<n; i++){
  6.             total += gas[i]-cost[i];
  7.             curr += gas[i]-cost[i];
  8.             if(curr< 0){
  9.                 start=i+1;
  10.                 curr=0;
  11.             }
  12.         }
  13.         return total>=0 ? start:-1;
  14.     }
  15. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement