zoukankan      html  css  js  c++  java
  • [LeetCode] 134. Gas Station

    There are N gas stations along a circular route, where the amount of gas at station i is gas[i].

    You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journey with an empty tank at one of the gas stations.

    Return the starting gas station's index if you can travel around the circuit once in the clockwise direction, otherwise return -1.

    Note:

    • If there exists a solution, it is guaranteed to be unique.
    • Both input arrays are non-empty and have the same length.
    • Each element in the input arrays is a non-negative integer.

    Example 1:

    Input: 
    gas  = [1,2,3,4,5]
    cost = [3,4,5,1,2]
    
    Output: 3
    
    Explanation:
    Start at station 3 (index 3) and fill up with 4 unit of gas. 
    Your tank = 0 + 4 = 4 Travel to station 4. Your tank = 4 - 1 + 5 = 8 Travel to station 0. Your tank = 8 - 2 + 1 = 7 Travel to station 1. Your tank = 7 - 3 + 2 = 6 Travel to station 2. Your tank = 6 - 4 + 3 = 5 Travel to station 3. The cost is 5. Your gas is just enough to
    travel back to station 3. Therefore, return 3 as the starting index.

    Example 2:

    Input: 
    gas  = [2,3,4]
    cost = [3,4,3]
    
    Output: -1
    
    Explanation:
    You can't start at station 0 or 1, as there is not enough gas 
    to travel to the next station. Let's start at station 2 and fill up with 4 unit of gas.
    Your tank = 0 + 4 = 4 Travel to station 0. Your tank = 4 - 3 + 2 = 3 Travel to station 1. Your tank = 3 - 3 + 3 = 3 You cannot travel back to station 2, as it requires 4 unit of
    gas but you only have 3. Therefore, you can't travel around the circuit once no matter
    where you start.

    加油站。

    题意是给一个长度为N的环,用数组表示。环上的每一个点都有两个值,一个是当前加油站的油量,一个是开到下一个加油站的耗油量。请找到一个起点能够保证车开完一圈,返回起点的坐标;若找不到这个起点则返回-1。这是一个数学题。这个题涉及一个定理(此处不证明了),如果一个数组的总和非负,那么一定可以找到一个起始位置,从它开始绕数组一圈,累加和一直都是非负的。有了这个定理之后,可以对input做如下判断。

    1. 从位置 i 开始直到绕完一圈,如果油箱没空,说明从 i 开始所有的累积都是正的(总的加油量大于耗油量);

    2. 如果在位置 j 的时候油箱空了,说明从位置 i 开始是走不完全程的,此时可以从 j + 1 的位置重新开始计算。为什么不考虑从 i + 1 的位置开始重新计算是因为i处的油量是正的情况下已经走不完全程了,如果从 i + 1 开始,更走不完全程了。

    时间O(n)

    空间O(1)

    JavaScript实现

     1 /**
     2  * @param {number[]} gas
     3  * @param {number[]} cost
     4  * @return {number}
     5  */
     6 var canCompleteCircuit = function (gas, cost) {
     7     // corner case
     8     if (gas.length === 0 || cost.length === 0) return -1;
     9 
    10     // normal case
    11     let total = 0;
    12     let sum = 0;
    13     let start = 0;
    14     for (let i = 0; i < gas.length; i++) {
    15         total += (gas[i] - cost[i]);
    16         if (sum < 0) {
    17             sum = gas[i] - cost[i];
    18             start = i;
    19         } else {
    20             sum += gas[i] - cost[i];
    21         }
    22     }
    23     return total < 0 ? -1 : start;
    24 };

    Java实现

     1 class Solution {
     2     public int canCompleteCircuit(int[] gas, int[] cost) {
     3         int total = 0;
     4         int sum = 0;
     5         int start = 0;
     6         for (int i = 0; i < gas.length; i++) {
     7             total += gas[i] - cost[i];
     8             if (sum < 0) {
     9                 sum = gas[i] - cost[i];
    10                 start = i;
    11             } else {
    12                 sum += gas[i] - cost[i];
    13             }
    14         }
    15         return total < 0 ? -1 : start;
    16     }
    17 }

    LeetCode 题目总结

  • 相关阅读:
    python学习笔记之--read、readline和readlines
    目录操作习题
    递归习题
    文件操作练习题
    HandleBase句柄的5种写法
    ContextBase
    BasegoSort
    PrototypePra原型_设计订单保存
    DesignPattenTemplate模板模式
    DesignPattenStrategy策略模式
  • 原文地址:https://www.cnblogs.com/cnoodle/p/12302111.html
Copyright © 2011-2022 走看看