[LeetCode] Gas Station

本文介绍了一种解决特定问题的方法:如何找到一条从加油站出发能够绕一圈返回起点的路径。通过观察得出结论,如果总的油量大于等于总的成本,则一定存在解决方案,并给出了两种高效的算法实现。

To solve this problem, some observations have to be made first.

Let's first see two relatively easy observations.

  1. To maximize the probability that we can complete the circuit, at each gas station, we will add all the available gas to the tank.
  2. 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.

 1 class Solution {
 2 public:
 3     int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
 4         int start = 0, total = 0, tank = 0;
 5         for (int i = 0; i < (int)gas.size(); i++) {
 6             tank += gas[i] - cost[i];
 7             if (tank < 0) {
 8                 start = i + 1;
 9                 total += tank;
10                 tank = 0;
11             }
12         }
13         return total + tank >= 0 ? start : -1;
14     }
15 };

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.

 1 class Solution {
 2 public:
 3     int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
 4         int net = 0, start = 0, min_net = 0;
 5         for (int i = 0; i < (int)gas.size(); i++) {
 6             net += gas[i] - cost[i];
 7             if (net < min_net) {
 8                 start = i + 1;
 9                 min_net = net;
10             }
11         }
12         return net >= 0 ? start : -1;
13     }
14 };

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.

转载于:https://www.cnblogs.com/jcliBlogger/p/4596666.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值