Leetcode 134. Gas Station 解题报告

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, otherwise return -1.

Note:
The solution is guaranteed to be unique.


分析

最简单的做法就是遍历一次每个作为起点,复杂度是O(n^2).

现在考虑只需要遍历一次的方法
经过每个油站对车来讲就是2中情况加了油或者耗了油,这可以用gas[i]-cost[i]来反映.于是我们构造数组detaGas[i] = gas[i] - cost[i].表示经过一个油站对油量的影响.
现在考虑从油站i出发一直到油站p-1油箱的油一直是>=0, 到了p油箱油<0,
即:sum = detaGas[i]+detaGas[i+1]+detaGas[i+2]…+detaGas[p-1]>=0
sum = detaGas[i]+detaGas[i+1]+detaGas[i+2]…+detaGas[p]<0

那么可以知道detaGas[i]>=0, 如果从i+1到p-1之间出发更不可能到p,因为少加了i的油.那么我们只需要考虑从p出发就好了,这样只需要遍历一次数组就可以得到答案.

假设数组daraGas被分为3段
sum1 = detaGas[0]+detaGas[1]+…..+detaGas[i] < 0;
sum2 = detaGas[i+1]+detaGas[i+2]+…..+detaGas[k] < 0;
sum3 = detaGas[k+1]+detaGas[1]+…..+detaGas[l-1] >= 0;
(一定最后一段的前面的和都是小于零的)

从k+1能回来的条件是 要能从0走到k,此时可以借助从k+1到l-1攒下的油,即满足 sum3+sum1+sum2 >= 0就可以回来.


AC代码

class Solution {
public:
    int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
        int l = gas.size();
        if (l <= 0) return -1;

        int s = 0;
        int curSum = 0;
        int b = 0 ,total = 0;
        for (int i = 0; i < l; ++i) {
            curSum += (gas[i] - cost[i]);
            total += (gas[i] - cost[i]);

            if (curSum >= 0) {

            } else {
                b = i+1;
                curSum = 0;
            }
        }

        if (total >= 0) return b;
        else return -1;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值