[LeetCode] Gas Station
2015-06-24 00:51
381 查看
To solve this problem, some observations have to be made first.
Let's first see two relatively easy observations.
To maximize the probability that we can complete the circuit, at each gas station, we will add all the available gas to the tank.
If sum_{i = 1, 2, ..., k}gas[i] < sum_{i = 1, 2, ..., k}cost[i], then we cannot reach k if we start from i.
Make sure you convince yourself of these two observations. They will serve as the foundation for the following trickier observations.
Starting from i, if the first unreachable position is k, then we cannot reach position k if we start from any position between i and k.
Let's do a quick proof. We use proof by contradiction. Suppose we can reach k if we start from the position j which is between i and k. Now we can reach k from j. Moreover, we can reach j from i (since k is the first unreachable position starting from i). Putting these together, we will first reach j from i, and then reach k from j. This contradicts the assumption that k is not reachable from i.
With this observation, each time we find the first point that the we cannot reach from the current starting position. We know that we do not need to check all the points between them and simply skip to the next point of the first unreachable point.
Now comes the last observation, which is the key to give a O(n) solution.
If the sum of gas is not less than the sum of cost, there must be a solution.
Well, the proof of this observation is not as easy as the above one. If there are only two gas stations, this observation is easy. When there are more than two gas stations, you may need to merge some of them to a single one and reduce the effective number of gas stations. You mar refer to this link for a proof.
Using these two observations, the idea to solve this problem is as follows.
Maintain a variable tank for the net gas in the tank and a variable total to accumulate the net difference between the sum of gas and the sum of cost. Then we initialize a variable start to be 0 for the final starting position. Each time we find an unreachable point, we update start to be the point after the current unreachable point. After we traverse all the points, we will have the final net difference between the sum of gas and the sum of cost. If it is not less than 0, we know there must be a solution (according to the last observation) and return start. Otherwise, return -1.
The code is as follows.
Well, let's do more with the last observation. In fact, if the sum of gas is not less than the sum of cost, there exists a position with the minimum net gas (sum_{i=0, 1, ..., k}gas[i] - sum_{i = 0, 1, ..., k}cost[i] is minimized) and the starting point is simply the point after it.
Using this idea, we will have a more succinct code.
One final note, all of the above solutions are from this link in the LeetCode dicussion forum. Thank you for the nice sharer. Please refer to it for more details.
Let's first see two relatively easy observations.
To maximize the probability that we can complete the circuit, at each gas station, we will add all the available gas to the tank.
If sum_{i = 1, 2, ..., k}gas[i] < sum_{i = 1, 2, ..., k}cost[i], then we cannot reach k if we start from i.
Make sure you convince yourself of these two observations. They will serve as the foundation for the following trickier observations.
Starting from i, if the first unreachable position is k, then we cannot reach position k if we start from any position between i and k.
Let's do a quick proof. We use proof by contradiction. Suppose we can reach k if we start from the position j which is between i and k. Now we can reach k from j. Moreover, we can reach j from i (since k is the first unreachable position starting from i). Putting these together, we will first reach j from i, and then reach k from j. This contradicts the assumption that k is not reachable from i.
With this observation, each time we find the first point that the we cannot reach from the current starting position. We know that we do not need to check all the points between them and simply skip to the next point of the first unreachable point.
Now comes the last observation, which is the key to give a O(n) solution.
If the sum of gas is not less than the sum of cost, there must be a solution.
Well, the proof of this observation is not as easy as the above one. If there are only two gas stations, this observation is easy. When there are more than two gas stations, you may need to merge some of them to a single one and reduce the effective number of gas stations. You mar refer to this link for a proof.
Using these two observations, the idea to solve this problem is as follows.
Maintain a variable tank for the net gas in the tank and a variable total to accumulate the net difference between the sum of gas and the sum of cost. Then we initialize a variable start to be 0 for the final starting position. Each time we find an unreachable point, we update start to be the point after the current unreachable point. After we traverse all the points, we will have the final net difference between the sum of gas and the sum of cost. If it is not less than 0, we know there must be a solution (according to the last observation) and return start. Otherwise, return -1.
The code is as follows.
class Solution { public: int canCompleteCircuit(vector<int>& gas, vector<int>& cost) { int start = 0, total = 0, tank = 0; for (int i = 0; i < (int)gas.size(); i++) { tank += gas[i] - cost[i]; if (tank < 0) { start = i + 1; total += tank; tank = 0; } } return total + tank >= 0 ? start : -1; } };
Well, let's do more with the last observation. In fact, if the sum of gas is not less than the sum of cost, there exists a position with the minimum net gas (sum_{i=0, 1, ..., k}gas[i] - sum_{i = 0, 1, ..., k}cost[i] is minimized) and the starting point is simply the point after it.
Using this idea, we will have a more succinct code.
class Solution { public: int canCompleteCircuit(vector<int>& gas, vector<int>& cost) { int net = 0, start = 0, min_net = 0; for (int i = 0; i < (int)gas.size(); i++) { net += gas[i] - cost[i]; if (net < min_net) { start = i + 1; min_net = net; } } return net >= 0 ? start : -1; } };
One final note, all of the above solutions are from this link in the LeetCode dicussion forum. Thank you for the nice sharer. Please refer to it for more details.
相关文章推荐
- LeetCode——Rectangle Area
- Window SDK 入门
- Leetcode#7 Reverse Integer !!!Update (2014-11-10)之后的版本!!!
- expdp时遇到ORA-31693&ORA-02354&ORA-01466
- anjularjs
- leetcode 35 -- Search Insert Position
- 读《怪诞行为学》感
- bcompare3+keygen
- 用自己制作的设备得到的3D自画像
- bcompare3+keygen
- 3S比赛预定
- UVa - 1611 - Crane
- UVa - 1611 - Crane
- eclipse debug不起作用了
- 打开网页不显示图片和视频
- 【莫比乌斯反演】BZOJ1101 [POI2007]zap
- 分布式系统设计理念
- leetcode 10 -- Regular Expression Matching
- 获取文件夹下得所有文件,并按文件夹先,文件后的顺序排
- LeetCode——Happy Number