Middle-题目82:134. Gas Station

原创 2016年05月31日 17:01:31

题目原文:
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.
题目大意:
有n个加油站围成一个圈,其中第i个加油站的油量是gas[i].
你有一个汽车,每从第i个加油站开到第i+1个,要消耗cost[i]的汽油。初始油箱为空,问这个汽车能否开完一圈,如果能则返回一个起点位置,否则返回-1.
题目分析:
首先朴素算法就是从每个节点开始开一圈,看看能不能走到头。这样提交会超时。
那么从discuss看到的一个贪心算法,从节点0开始向前开,如果开完一个站点还有油就向前开,如果油量不足,就把出发站点往回移动一站,并从出发点重新开始开,直到遍历完一圈,如果最后油箱里的油≥0,则返回最后的出发点,否则返回-1.
源码:(language:java)

public class Solution {
    public int canCompleteCircuit(int[] gas, int[] cost) {
            if (gas == null) {
                return -1;
            }
            // Note: The Solution object is instantiated only once and is reused by each test case.
            int count = gas.length;

            int n = 0;
            int gasInCar = 0;
            int begin = 0;
            int end = 0;
            int i = 0;
            while (n < count - 1) {
                gasInCar += gas[i] - cost[i];
                if (gasInCar >=0) {//forward
                    end++;
                    i=end;
                } else {
                    begin--;
                    if (begin < 0) {
                        begin = count - 1;
                    }
                    i = begin;
                }

                n++;
            }

            gasInCar += gas[i] - cost[i];

            if (gasInCar >= 0) {
                return begin;
            } else {
                return -1;
            }

        }

}

成绩:
1ms,beats 5.79%,众数1ms,82.52%
Cmershen的碎碎念:
这个算法的理解和实现都不算难,但暂时没想到如何严格证明其正确性。

版权声明:完整版Leetcode题解请出门左转https://github.com/cmershen1/leetcode/tree/master/docs

相关文章推荐

[leetcode] 134 Gas Station(经典dp || 贪心)

(一)最容易想到的是O(n2)的解法 预处理出gas[i] - cost[i] 的数组,从每个非负的位置开始尝试,只要能够完成一个循环,就可以输出结果; 对于返回-1的情况,我们经过思考和推论可以...
  • NK_test
  • NK_test
  • 2015年09月28日 14:31
  • 1633

LeetCodeP134 Gas Station

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

LeetCode 134 Gas Station 题解

题意简述:有n个加油站,呈环形分布在一条公路上。给出每个加油站所能提供的油量gas,和从一个加油站到达下一个加油站消耗的油量cost。假设车的油缸可以装无限的汽油,那么能否找到一个出发点,使车能够绕公...

算法分析与设计第七周:134. Gas Station

题目简介: There are N gas stations along a circular route, where the amount of gas at station i is gas[...

LeetCode-134. Gas Station(JAVA)加气站问题

LeetCode-134. Gas Station(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]. Yo...

LeetCode(134)Gas Station

题目如下: There are N gas stations along a circular route, where the amount of gas at station i is gas[...

[leetcode] 【数组】134. Gas Station

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

【LeetCode】134. Gas Station

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

[leetcode] 134 Gas Station

题目: There are N gas stations along a circular route, where the amount of gas at station i is gas[i]...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Middle-题目82:134. Gas Station
举报原因:
原因补充:

(最多只允许输入30个字)