Gas Station

原创 2015年07月07日 22:55:09

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.

看到题目第一反应是贪心算法;所以想也没想就直接枚举每一种情况,从0开始,看是不是可以走完,这样发现其实根本就是暴力枚举,没有任何的算法可言。然后仔细分析,发现其实很多地方是可以优化的,定义diff[i] = gas[i] - cost[i];如果diff数组里面的每个元素和小于0的话,那么必然是没有可以到达的方式,直接返回-1;然后是从0开始,依次计算是否可能,如果发现到达j位置走不下去了,那么可能的出发点start的范围应该是 j <= start <= gas.size(),所以下次需找的点应该是从j继续往后走,这样可以将算法马上从0(N^2)的复杂度降低到0(N)的复杂度,只要遍历一遍diff数据,就可以找到可能的起始出发位置。所以看到题目还是得注意多分析,尽可能的优化时间复杂度。

int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
        int lenG = gas.size();
        int lenC = cost.size();
        if(lenG == 0 || lenC == 0 || (lenG != lenC))
        {
            return -1;
        }
        
        int sum = 0;
        vector<int> diff;
        int tmp = 0;
        for(int i = 0; i < lenG; ++i)
        {
            tmp = gas[i] - cost[i];
            diff.push_back(tmp);
            sum += tmp;
        }
        
        if(sum < 0)
            return -1;
        int index = 0;
        for(int i = 0; i < lenG; ++i)
        {
            int tmpSum = diff[i];
            if(tmpSum < 0)
                continue;
            int flag = 1;
            for(int j = i + 1; j < lenG; ++j)
            {
                tmpSum += diff[j];
                if(tmpSum < 0)
                {
                    flag = 0;
                    i = j;
                    break;
                }
            }
            
            if(flag)
            {
                index = i;
                break;
            }
        }
        
        return index;
    }


LeetCode Gas Station 两个特性,两种方法完美解答-更新证明方法

我觉得做这道题的关键是要可以总结出来这道题目的属性,注意Note这个地方,其属性主要有两个: 1 如果总的gas - cost小于零的话,那么没有解返回-1 2 如果前面所有的gas - cost...
  • kenden23
  • kenden23
  • 2013年11月03日 17:07
  • 17907

[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 之 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]...

Leetcode——134. Gas Station && 435. Non-overlapping Intervals

Gas Station 1. 问题描述 有N个加油站围成一个圈,第i个加油站的油量是gas[i],一辆车有一个无限量的油箱,每次从第i个加油站开到第i+1个加油站需要消耗cost[i]的油量。车一开始...

[LeetCode] Gas Station,转化为求最大序列的解法,和更简单简单的Jump解法。

LeetCode上 Gas Station是比较经典的一题,它的魅力在于算法足够优秀的情况下,代码可以简化到非常简洁的程度。 原题如下 Gas Station There are N...

Gas Station leetcode

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

leetcode134. Gas Station

134. Gas StationThere are N gas stations along a circular route, where the amount of gas at station ...

1072. Gas Station (30)

1072. Gas Station (30) #include #include #include using namespace std; class CA { public: enum{...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Gas Station
举报原因:
原因补充:

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