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

posted @   jianchao-li  阅读(355)  评论(0编辑  收藏  举报
编辑推荐:
· 理解Rust引用及其生命周期标识(下)
· 从二进制到误差:逐行拆解C语言浮点运算中的4008175468544之谜
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
阅读排行:
· 2025成都.NET开发者Connect圆满结束
· 后端思维之高并发处理方案
· 在 VS Code 中,一键安装 MCP Server!
· 千万级大表的优化技巧
· langchain0.3教程:从0到1打造一个智能聊天机器人
点击右上角即可分享
微信分享提示