LeetCode-Solutions-in-Cpp17

Solutions to high-frequency interview questions of LeetCode in C++17, taking into account both efficiency and comprehensibility.


Project maintained by downdemo Hosted on GitHub Pages — Theme by mattgraham
class Solution {
 public:
  int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
    int res = 0;
    int mn = INT_MAX;
    int x = 0;
    for (int i = 0; i < size(gas); ++i) {
      x += gas[i] - cost[i];
      if (x < mn) {
        mn = x;
        res = i;
      }
    }
    if (x < 0) {
      return -1;
    }
    if (res == size(gas) - 1) {
      return 0;
    }
    return res + 1;
  }
};