加油站回到起点 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, otherwise return -1.

Note:
The solution is guaranteed to be unique.

解决:

【题意】环形路线上有N个加油站,每个加油站有汽油gas[i],从每个加油站到下一站消耗汽油cost[i],问从哪个加油站出发能够回到起始点,如果都不能则返回-1(注意,解是唯一的)。

① 直接解决,O(n^2)。

class Solution { //超时
    public int canCompleteCircuit(int[] gas, int[] cost) {
        for (int i = 0;i < gas.length ;i ++ ) {//依次从每个加油站出发
            int j = i;
            int curgas = gas[j];
            while(curgas >= cost[j]){//如果当前的汽油量能够到达下一站
                curgas -= cost[j];//减去每次小号
                j = (j + 1) % gas.length;//到达下一站
                if(j == i){//如果回到了起始站,成功
                    return i;
                }
                curgas += gas[j];//到下一站后重新加油
            }
        }
        return -1;
    }
}

② 参考 http://www.cnblogs.com/felixfang/p/3814463.html的思路

如果你是司机,你肯定会选这么样的一个站点:从这个站点出发,先遇到的站点都是给车加油大于耗油的,从而让车攒够足够的余油,来撑过剩下的那些耗油大于加油的站点。

对于站点 i,我们把 gas[i] - cost[i] 当作整体考虑,我们用diff[i] = gas[i] - cost[i] 表示从当前站点出发,到下一个站点后剩余的油量。

因此结合司机的经验,题目就是需要找出这么一个点,从这个点出发,diff的累加值能达到峰值

想起了什么吗?这道题就是求diff[]数组上的 和最大连续子序列这个最大子序列的起始点就是车的出发点!如果车从这儿开都不能做到油箱不空,那么这个题目就该返回-1了

因此,这道题转化成了 求循环数组的最大子序列问题

利用动态规划,求出diff[0] 到 diff[size-1]这个非循环数组上的最大子序列MAX和最小子序列MIN。然后比较MAX和 Total - MIN,选出较大值(Total为 diff数组所有元素和)。当然,我们要求的不是最大子序列的和,而是最大子序列的起始位置。因此,如果MAX 较大,返回MAX所代表的最大子序列的起始元素;否则,返回MIN所代表的最小子序列末尾的下一个元素。时间复杂度为O(n)。

class Solution { //1ms
    public int canCompleteCircuit(int[] gas, int[] cost) {
        if (gas.length == 0 || gas.length != cost.length) {
            return -1;
        }
        int MAX = gas[0] - cost[0];
        int MIN = gas[0] - cost[0];
        int max = gas[0] - cost[0];
        int min = gas[0] - cost[0];
        int smax = 0;
        int sMAX = 0;
        int eMIN = 0;
        int total = 0;
        int diff = 0;
        for (int i = 0;i < gas.length ;i ++ ) {
            diff = gas[i] - cost[i];
            total += diff;
            if (max < 0) {
                max = diff;
                smax = i;//不能省略,因为后面要赋值给sMAX,需要记录前一次最大值的起始位置
            }else{
                max += diff;
            }
            if (max > MAX) {
                MAX = max;
                sMAX = smax;
            }
            if (min > 0) {
                min = diff;
            }else{
                min += diff;
            }
            if (min < MIN) {
                MIN = min;
                eMIN = i;
            }
        }
        return total < 0 ? -1 : (MAX >= (total - MIN) ? sMAX : (eMIN + 1) % gas.length );
    }
}

【注】

在末尾可以发现,最后我们在通过比较 MAX 和 (total - MIN) 得出结果后,并没有模拟让车从所得站点跑一圈,来验证是否正确,而是直接通过比较 total 和 0的关系来确定是否有解。

如果Total >= 0,那么直接返回站点,如果Total < 0,返回-1。

也就是说,直接通过Total的值就可以来确定该题是否有解

【注】

[2,0,1,2,3,4,0] [0,1,0,0,0,0,11] ---》5 正确输出应该是0.

③ 

我们模拟一下过程:

a. 最开始,站点0是始发站,假设车开出站点p后,油箱空了,假设sum1 = diff[0] +diff[1] + ... + diff[p],可知sum1 < 0;

b. 根据上面的论述,我们将p+1作为始发站,开出q站后,油箱又空了,设sum2 = diff[p+1] +diff[p+2] + ... + diff[q],可知sum2 < 0。

c. 将q+1作为始发站,假设一直开到了未循环的最末站,油箱没见底儿,设sum3 = diff[q+1] +diff[q+2] + ... + diff[size-1],可知sum3 >= 0。

要想知道车能否开回 q 站,其实就是在sum3 的基础上,依次加上 diff[0] 到 diff[q],看看sum3在这个过程中是否会小于0。但是我们之前已经知道 diff[0] 到 diff[p-1] 这段路,油箱能一直保持非负,因此我们只要算算sum3 + sum1是否 <0,就知道能不能开到 p+1站了。如果能从p+1站开出,只要算算sum3 + sum1 + sum2 是否 < 0,就知都能不能开回q站了。

因为 sum1, sum2 都 < 0,因此如果 sum3 + sum1 + sum2 >=0 那么 sum3 + sum1 必然 >= 0,也就是说,只要sum3 + sum1 + sum2 >=0,车必然能开回q站。而sum3 + sum1 + sum2 其实就是 diff数组的总和 Total,遍历完所有元素已经算出来了。因此 Total 能否 >= 0,就是是否存在这样的站点的 充分必要条件。时间复杂度 O(n)。

class Solution { //1ms
    public int canCompleteCircuit(int[] gas, int[] cost) {
        if (gas.length == 0 || cost.length == 0 || gas.length != cost.length) {
            return -1;
        }
        int total = 0;
        int sum = 0;
        int start = 0;
        for (int i = 0;i < gas.length ;i ++ ) {
            total += gas[i] - cost[i];
            if (sum < 0) {//油箱空了,从下一个站点出发
                sum = gas[i] - cost[i];
                start = i;
            }else{
                sum += gas[i] - cost[i];
            }
        }
        return total < 0 ? -1 : start;//用total判断start 是否是满足要求的解
    }
}

这种解法其实依托于一个数学命题:

对于一个循环数组,如果这个数组整体和 SUM >= 0,那么必然可以在数组中找到这么一个元素:从这个数组元素出发,绕数组一圈,能保证累加和一直是出于非负状态

转载于:https://my.oschina.net/liyurong/blog/1544920

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值