[LeetCode] Gas Station

简介: To solve this problem, some observations have to be made first. Let's first see two relatively easy observations.

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.

目录
相关文章
|
2月前
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
|
3月前
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
57 6
|
3月前
|
Python
【Leetcode刷题Python】剑指 Offer 26. 树的子结构
这篇文章提供了解决LeetCode上"剑指Offer 26. 树的子结构"问题的Python代码实现和解析,判断一棵树B是否是另一棵树A的子结构。
51 4
|
3月前
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
114 2
|
22天前
|
机器学习/深度学习 人工智能 自然语言处理
280页PDF,全方位评估OpenAI o1,Leetcode刷题准确率竟这么高
【10月更文挑战第24天】近年来,OpenAI的o1模型在大型语言模型(LLMs)中脱颖而出,展现出卓越的推理能力和知识整合能力。基于Transformer架构,o1模型采用了链式思维和强化学习等先进技术,显著提升了其在编程竞赛、医学影像报告生成、数学问题解决、自然语言推理和芯片设计等领域的表现。本文将全面评估o1模型的性能及其对AI研究和应用的潜在影响。
17 1
|
2月前
|
数据采集 负载均衡 安全
LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口
本文提供了多个多线程编程问题的解决方案,包括设计有限阻塞队列、多线程网页爬虫、红绿灯路口等,每个问题都给出了至少一种实现方法,涵盖了互斥锁、条件变量、信号量等线程同步机制的使用。
LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口
|
3月前
|
索引 Python
【Leetcode刷题Python】从列表list中创建一颗二叉树
本文介绍了如何使用Python递归函数从列表中创建二叉树,其中每个节点的左右子节点索引分别是当前节点索引的2倍加1和2倍加2。
58 7
|
3月前
|
Python
【Leetcode刷题Python】剑指 Offer 30. 包含min函数的栈
本文提供了实现一个包含min函数的栈的Python代码,确保min、push和pop操作的时间复杂度为O(1)。
28 4