[LeetCode] Gas Station
2014-06-16 16:32
288 查看
There are N gas stations along a circular route, where the amount of gas at station i is
You have a car with an unlimited gas tank and it costs
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个加油站,每个加油站的油的数量是
问题是:该汽车汽油罐开始时是空的,如果该车能绕着环型线路走完,返回起始加油站的index;否则,返回-1。
注意:如果存在方案,该方案必定是唯一的。
解题思路:
1,存在一种暴力求解法。即从每个加油站开始都计算一次。看能不能到达终点,但是时间肯定超时。
2,较好的方法:任意选择一个加油站作为开始,一直往下走,如果到达某一站加的油减去耗费的油为负值,则选择截至的那个加油站的下一站作为起始站,继续计算 ; 直到计算完所有加油站为止。
最后需要检测一下选择的起始站,能否跑完全程。
有两个前提,令A[i] = gas[i]-cost[i]
1 若A[0] + A[1] + ... + A[n-1] >= 0, 必存在 K,满足提议要求,A[k] >=0, A[K]+ A[K+1] >=0,....., A[K]+A[K+1]+ ... A[K-1] >=0.
2 假设起点是 i, 走到 j 时, 发现剩下的油不够了. 暴力法 的做法是从 i+1 开始, 再走. 但是, A[i] >=0, 而A[i] + A[i+1] + ... A[j] < 0, 必有 A[i+1] + ... A[j] < 0,即从 从 i+1 ->j 之间的节点,肯定走不通。所以下一个起点应该是 j+1
Code 如下
再次精简:
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]。有一辆车,它有一个油箱可以装无限量的汽油,汽车从加油站i到下一个加油站i+1需要耗费
cost[i]的汽油。
问题是:该汽车汽油罐开始时是空的,如果该车能绕着环型线路走完,返回起始加油站的index;否则,返回-1。
注意:如果存在方案,该方案必定是唯一的。
解题思路:
1,存在一种暴力求解法。即从每个加油站开始都计算一次。看能不能到达终点,但是时间肯定超时。
2,较好的方法:任意选择一个加油站作为开始,一直往下走,如果到达某一站加的油减去耗费的油为负值,则选择截至的那个加油站的下一站作为起始站,继续计算 ; 直到计算完所有加油站为止。
最后需要检测一下选择的起始站,能否跑完全程。
有两个前提,令A[i] = gas[i]-cost[i]
1 若A[0] + A[1] + ... + A[n-1] >= 0, 必存在 K,满足提议要求,A[k] >=0, A[K]+ A[K+1] >=0,....., A[K]+A[K+1]+ ... A[K-1] >=0.
2 假设起点是 i, 走到 j 时, 发现剩下的油不够了. 暴力法 的做法是从 i+1 开始, 再走. 但是, A[i] >=0, 而A[i] + A[i+1] + ... A[j] < 0, 必有 A[i+1] + ... A[j] < 0,即从 从 i+1 ->j 之间的节点,肯定走不通。所以下一个起点应该是 j+1
Code 如下
class Solution { public: int canCompleteCircuit(vector<int> &gas, vector<int> &cost) { int n = gas.size(); int total = 0; for(int i =0; i< n; i++) { total += (gas[i] - cost[i]); } if(total < 0) return -1; int sum = 0; int j = 0; for(int i =0; i< n; i++) { sum += (gas[i] - cost[i]); if(sum < 0) { j=(i+1)%n; sum = 0; } } return j; } };
再次精简:
// 时间复杂度 O(n),空间复杂度 O(1) class Solution { public: int canCompleteCircuit(vector<int> &gas, vector<int> &cost) { int total = 0; int j = -1; for (int i = 0, sum = 0; i < gas.size(); ++i) { sum += gas[i] - cost[i]; total += gas[i] - cost[i]; if (sum < 0) { j = i; sum = 0; } } return total >= 0 ? j + 1 : -1; } };
相关文章推荐
- leetcode — gas-station
- Leetcode: Gas Station
- LeetCode :: Gas Station
- Gas Station [leetcode] 的两种解法
- LeetCode134——Gas Station
- LeetCode-Gas Station
- [Leetcode] Gas Station (Java)
- 【LeetCode with Python】 Gas Station
- LEETCODE: Gas Station
- LeetCode | Gas Station(加油站)
- LeetCode – Refresh – Gas Station
- [leetcode] Gas Station
- [Leetcode 134, Medium] Gas Station
- LeetCode 134 Gas Station
- LeetCode:Gas Station
- LeetCode——Gas Station
- LeetCode之Gas Station --环形加油站周游
- LeetCode Gas Station
- [Leetcode]Gas Station
- Gas Station - Leetcode