Gas Station [leetcode] 两个解决方案

因为gas的总数大于cost总时间。你将能够圈住整个城市。

第一溶液:

如果一開始有足够的油。从位置i出发。到位置k时剩余的油量为L(i,k)。

对随意的k。L(i,k)依据i的不同,仅仅相差常数。

我们仅仅须要找到最小的L(0, k)相应的k,k+1为所求。

代码例如以下:

    int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
        int start = 0;
        int curGas = 0, minGas = 0, totalGas = 0;
        for (int i = 0; i < gas.size(); i++)
        {
            int temp = gas[i] - cost[i];
            curGas += temp;
            totalGas += temp;
            if (minGas > curGas)
            {
                start = i + 1;
                minGas = curGas;
            }
        }
        if (totalGas >= 0) return start % gas.size();
        else return -1;
    }

另外一种解法:

假设L(i,k) < 0,则从i和k之间全部的位置都不能到k

所以从k+1的位置从0開始找

    int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
        int start = 0;
        int curGas = 0, totalGas = 0;
        for (int i = 0; i < gas.size(); i++)
        {
            int temp = gas[i] - cost[i];
            curGas += temp;
            totalGas += temp;
            if (curGas < 0)
            {
                start = i + 1;
                curGas = 0;
            }
        }
        if (totalGas >= 0) return start % gas.size();
        else return -1;
    }

版权声明:本文博客原创文章,博客,未经同意,不得转载。

时间: 2024-10-31 16:17:49

Gas Station [leetcode] 两个解决方案的相关文章

Gas Station leetcode java

题目: 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

Gas Station——LeetCode

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 e

Gas Station Leetcode Python

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 e

[Lintcode]187. Gas Station/[Leetcode]134. Gas Station

187. Gas Station/134. Gas Station 本题难度: Medium Topic: Greedy Description 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

Gas Station [leetcode] 的两种解法

由于gas总量大于cost总量时,一定可以绕所有城市一圈. 第一种解法: 假设一开始有足够的油,从位置i出发,到位置k时剩余的油量为L(i,k). 对任意的k,L(i,k)根据i的不同,只相差常数. 我们只需要找到最小的L(0, k)对应的k,k+1为所求. 代码如下: int canCompleteCircuit(vector<int> &gas, vector<int> &cost) { int start = 0; int curGas = 0, minGas

[C++]LeetCode: 119 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

Leetcode:Gas Station 加油站

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 journ

LeetCode: Gas Station [134]

[题目] 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

LeetCode: Gas Station 解题报告

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 journ