diff --git a/LeetCode/Gas_Station.cpp b/LeetCode/Gas_Station.cpp new file mode 100644 index 0000000..c925a1d --- /dev/null +++ b/LeetCode/Gas_Station.cpp @@ -0,0 +1,21 @@ +class Solution { +public: + int canCompleteCircuit(vector& gas, vector& cost) { + int totalTank = 0; + int currentTank = 0; + int startStation = 0; + + for (int i = 0; i < gas.size(); i++) { + int netGas = gas[i] - cost[i]; + totalTank += netGas; + currentTank += netGas; + + if (currentTank < 0) { + startStation = i + 1; + currentTank = 0; + } + } + + return (totalTank >= 0) ? startStation : -1; + } +}; \ No newline at end of file