You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// class Solution {
// public:
// int canCompleteCircuit(vector& gas, vector& cost) {
// int f = 0;
// int n = gas.size();
// int s = 0;
// int t = 0;
// int d = 0;
// for(int i = 0;i<n;i++){
// d = gas[i]-cost[i];
// t += d;
// f += d;
// if(f<0){
// s = i+1;
// f = 0;
// }
// }
// return t>=0?s:-1;
// }
// };
// Purpose of fuel (f):
// fuel keeps track of whether you have enough gas to travel continuously from the current starting point (s) to the next stations.
// If at any point fuel < 0, it means you cannot reach the next station starting from the current starting station (s).
// Therefore, reset the starting point to the next station (s = i + 1) and start over from there.
// Resetting fuel = 0 means you are starting fresh from the new station, ignoring the previous failed portion.
// Purpose of total (t):
// t is the total difference between the gas you earn (gas[i]) and the gas you spend (cost[i]) for the entire circular route.
// After completing the loop:
// If t >= 0, it means there is enough gas overall to complete the circuit, starting from the valid candidate station s.
// If t < 0, it means there isn't enough gas overall, so it's impossible to complete the circuit, and the answer is -1.
// we need both fuel and total?
// fuel is local and tracks whether you can travel continuously from the current candidate station (s). If fuel drops below 0, it tells you that your local starting point has failed.
// total is global and ensures that even if you find a candidate station s (based on fuel), there is enough gas overall to complete the entire circuit.
// //
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
leetcode/en/lc/134/
LeetCode solutions in any programming language
https://doocs.github.io/leetcode/en/lc/134/
Beta Was this translation helpful? Give feedback.
All reactions