nikunjsoni

134

Jun 29th, 2021
103
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. };
RAW Paste Data