zentan66 / daily-coding

日常手写算法,编程题
0 stars 0 forks source link

【⭐️⭐️】LeetCode-加油站 #32

Open zentan66 opened 3 years ago

zentan66 commented 3 years ago

在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升。

你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] 升。你从其中的一个加油站出发,开始时油箱为空。

如果你可以绕环路行驶一周,则返回出发时加油站的编号,否则返回 -1。

说明: 

如果题目有解,该答案即为唯一答案。 输入数组均为非空数组,且长度相同。 输入数组中的元素均为非负数。

zentan66 commented 3 years ago

编码

var canCompleteCircuit = function(gas, cost) {
  const n = gas.length;
  let i = 0;
  while (i < n) {
      let sumOfGas = 0, sumOfCost = 0;
      let cnt = 0;
      while (cnt < n) {
          const j = (i + cnt) % n;
          sumOfGas += gas[j];
          sumOfCost += cost[j];
          if (sumOfCost > sumOfGas) {
              break;
          }
          cnt++;
      }
      if (cnt === n) {
          return i;
      } else {
          i = i + cnt + 1;
      }
  }
  return -1;
};