[leetcode] 134. Gas Station
2015-12-14 22:02
381 查看
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 costscost[i] of gas to travel from stationi 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.
这道题给出一组加油站储存的油量和加油站之间的油耗,问能否从一个加油站出发依次走完所有加油站回到原点,题目难度为Medium。
对于任意加油站m,如果gas[m]<cost[m],则不能从m出发,直接查看下一个加油站。如果gas[m]>=cost[m],则可以从m出发,每经过一个加油站统计剩下的油量,如果剩下油量为负,表明从m出发已不能到达此加油站,将此加油站记为n。从m到n之间的任意加油站出发也不能到达n,为什么呢?取m和n之间的任一加油站p,从m到p剩下的油量肯定不为负,因而从p到n剩下的油量比从m到n剩下的油量小(或相等),也必定为负,所以从m和n之间任意加油站不能到n。这样我们接着检查n的下一个加油站直至找到答案。具体代码:
为什么遍历一次最后记录的起点就是满足条件的呢?这里基于两点:第一,题目限定结果唯一或无解;第二,如果gas[0]+gas[1]+...+gas[n-1] > cost[0]+cost[1]+...+cost[n-1],则必定存在一个加油站,从此加油站开始能够顺利走完一圈回到原点。
第一点是题目限定的,不再说明。第二点真的成立吗?试着举反例很久没有想明白,所以查看了别人的证明,具体如下:
只有一个加油站:结论成立,不再说明。
两个加油站a和b:如果gas[a]>=cost[a]、gas[b]>=cost[b],则从任意加油站开始都可以走完(题目限定结果唯一,不包括此情况也无所谓)。如果gas[a]<cost[a],则gas[b]>cost[b],这样能够从b出发顺利到达a,同时由于gas[a]+gas[b]>cost[a]+cost[b],所以到达a后仍能够顺利回到b;同理gas[b]<cost[b]结论一样成立。
三个加油站a、b和c:如果gas[a]>cost[a]、gas[b]>cost[b]、gas[c]>cost[c],和两个加油站的情况相同。如果gas[a]<cost[a],此时又分两种情况:
gas[b]>=cost[b],能够从b出发顺利到达c,这样可以把b和c合并为一个加油站b',就把问题化解为两个加油站a和b'的情况,已证。
gas[b]<cost[b],则gas[c]>cost[c],能够从c出发顺利到达a,这样把c和a合并为一个加油站c',同上把问题化解为两个加油站c'和b的情况,已证。
其他的情况也能够按照上面的方法化解为两个加油站的情况。
这样能够把任意加油站的情况按照上述方法逐次转化为两个加油站,结论成立。
由上面提到的两点能够确定:如果最后剩下的油量为负则无解,如果为正则最后记录的起点即是结果。
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 costscost[i] of gas to travel from stationi 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.
这道题给出一组加油站储存的油量和加油站之间的油耗,问能否从一个加油站出发依次走完所有加油站回到原点,题目难度为Medium。
对于任意加油站m,如果gas[m]<cost[m],则不能从m出发,直接查看下一个加油站。如果gas[m]>=cost[m],则可以从m出发,每经过一个加油站统计剩下的油量,如果剩下油量为负,表明从m出发已不能到达此加油站,将此加油站记为n。从m到n之间的任意加油站出发也不能到达n,为什么呢?取m和n之间的任一加油站p,从m到p剩下的油量肯定不为负,因而从p到n剩下的油量比从m到n剩下的油量小(或相等),也必定为负,所以从m和n之间任意加油站不能到n。这样我们接着检查n的下一个加油站直至找到答案。具体代码:
class Solution { public: int canCompleteCircuit(vector<int>& gas, vector<int>& cost) { if(gas.empty()) return -1; int sz = gas.size(); int idx = 0, pos = 0; int left = 0; while(idx < sz) { if(gas[idx] < cost[idx]) { idx++; } else { while(left >= 0) { if(pos == idx + sz) return idx; left += (gas[pos%sz]-cost[pos%sz]); pos++; } left = 0; idx = pos; } pos = idx; } return -1; } };这种方法时间复杂度为O(n),所有加油站遍历次数不超过两次。查看别人的代码有仅遍历一次的方法,先上代码:
class Solution { public: int canCompleteCircuit(vector<int>& gas, vector<int>& cost) { if(gas.empty()) return -1; int idx = 0; int left = 0, total = 0; for(int i=0; i<gas.size(); i++) { left += gas[i]-cost[i]; if(left < 0) { total += left; left = 0; idx = i + 1; } } total += left; return (total>=0) ? idx : -1; } };
为什么遍历一次最后记录的起点就是满足条件的呢?这里基于两点:第一,题目限定结果唯一或无解;第二,如果gas[0]+gas[1]+...+gas[n-1] > cost[0]+cost[1]+...+cost[n-1],则必定存在一个加油站,从此加油站开始能够顺利走完一圈回到原点。
第一点是题目限定的,不再说明。第二点真的成立吗?试着举反例很久没有想明白,所以查看了别人的证明,具体如下:
只有一个加油站:结论成立,不再说明。
两个加油站a和b:如果gas[a]>=cost[a]、gas[b]>=cost[b],则从任意加油站开始都可以走完(题目限定结果唯一,不包括此情况也无所谓)。如果gas[a]<cost[a],则gas[b]>cost[b],这样能够从b出发顺利到达a,同时由于gas[a]+gas[b]>cost[a]+cost[b],所以到达a后仍能够顺利回到b;同理gas[b]<cost[b]结论一样成立。
三个加油站a、b和c:如果gas[a]>cost[a]、gas[b]>cost[b]、gas[c]>cost[c],和两个加油站的情况相同。如果gas[a]<cost[a],此时又分两种情况:
gas[b]>=cost[b],能够从b出发顺利到达c,这样可以把b和c合并为一个加油站b',就把问题化解为两个加油站a和b'的情况,已证。
gas[b]<cost[b],则gas[c]>cost[c],能够从c出发顺利到达a,这样把c和a合并为一个加油站c',同上把问题化解为两个加油站c'和b的情况,已证。
其他的情况也能够按照上面的方法化解为两个加油站的情况。
这样能够把任意加油站的情况按照上述方法逐次转化为两个加油站,结论成立。
由上面提到的两点能够确定:如果最后剩下的油量为负则无解,如果为正则最后记录的起点即是结果。
相关文章推荐
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- leetcode----Longest Substring Without Repeating Characters
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- leetcode_linearList
- leetcode_linearList02
- 021-Merge Two Sorted Lists(合并两个排好序的单链表);leetcode
- LeetCode[Day 1] Two Sum 题解
- LeetCode[Day 2] Median of Two Sorted Arrays 题解
- LeetCode[Day 3] Longest Substring Without... 题解
- LeetCode [Day 4] Add Two Numbers 题解