Loading [MathJax]/jax/output/CommonHTML/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >LeetCode 0134 - Gas Station

LeetCode 0134 - Gas Station

作者头像
Reck Zhang
发布于 2021-08-11 06:35:53
发布于 2021-08-11 06:35:53
31800
代码可运行
举报
文章被收录于专栏:Reck ZhangReck Zhang
运行总次数:0
代码可运行

Gas Station

Desicription

There are N gas stations along a circular route, where the amount of gas at station i is 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 in the clockwise direction, otherwise return -1.

Note:

  • If there exists a solution, it is guaranteed to be unique.
  • Both input arrays are non-empty and have the same length.
  • Each element in the input arrays is a non-negative integer.

Example 1:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Input: 
gas  = [1,2,3,4,5]
cost = [3,4,5,1,2]

Output: 3

Explanation:
Start at station 3 (index 3) and fill up with 4 unit of gas. Your tank = 0 + 4 = 4
Travel to station 4. Your tank = 4 - 1 + 5 = 8
Travel to station 0. Your tank = 8 - 2 + 1 = 7
Travel to station 1. Your tank = 7 - 3 + 2 = 6
Travel to station 2. Your tank = 6 - 4 + 3 = 5
Travel to station 3. The cost is 5. Your gas is just enough to travel back to station 3.
Therefore, return 3 as the starting index.

Example 2:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Input: 
gas  = [2,3,4]
cost = [3,4,3]

Output: -1

Explanation:
You can't start at station 0 or 1, as there is not enough gas to travel to the next station.
Let's start at station 2 and fill up with 4 unit of gas. Your tank = 0 + 4 = 4
Travel to station 0. Your tank = 4 - 3 + 2 = 3
Travel to station 1. Your tank = 3 - 3 + 3 = 3
You cannot travel back to station 2, as it requires 4 unit of gas but you only have 3.

Solution

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
class Solution {
public:
    int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
        int index = 0;
        int tank = 0;
        int total = 0;
        for(int i = 0; i < gas.size(); i++) {
            if((tank = tank + gas[i] - cost[i]) < 0) {
                total += tank;
                index = i + 1;
                tank = 0;
            }
        }
        return total + tank >= 0?index:-1;
    }
};
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-05-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
Leetcode 134 Gas Station
There are N gas stations along a circular route, where the amount of gas at station i is 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 empt
triplebee
2018/01/12
5870
回溯/贪心高频题
"有关递归的算法,都离不开“树”的遍历这一抽象模型。只不过对于不同的算法,在前(中)后序遍历的时候,所做的事不同而已。 "
王脸小
2019/10/28
1.4K0
【LEETCODE】模拟面试-134-Gas Station
新生 题目: https://leetcode.com/problems/gas-station/ There are N gas stations along a circular route, where the amount of gas at station i is 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
杨熹
2018/04/03
7740
【LEETCODE】模拟面试-134-Gas Station
LeetCode 134. Gas Station题目分析
There are N gas stations along a circular route, where the amount of gas at station i is 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. Subscribe to see which companies asked this question. 在一条环路上有 N 个加油站,其中第 i 个加油站有汽油gas[i],并且从第i个加油站前往第i+1个加油站需要消耗汽油cost[i]。 你有一辆油箱容量无限大的汽车,现在要从某一个加油站出发绕环路一周,一开始油箱为空。 求可环绕环路一周时出发的加油站的编号,若不存在环绕一周的方案,则返回-1。 注意事项 数据保证答案唯一。 样例 现在有4个加油站,汽油量gas[i]=[1, 1, 3, 1],环路旅行时消耗的汽油量cost[i]=[2, 2, 1, 1]。则出发的加油站的编号为2。
desperate633
2018/08/22
7360
​LeetCode刷题实战134:加油站
算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试。所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 !
程序员小猿
2021/02/01
3710
​LeetCode刷题实战134:加油站
[LeetCode] 134. Gas Station
本文讨论了一种算法题的解题思路,通过计算每个站点的差和,从第一个差和不为负数的站点开始环游,并返回起始站点。
用户1148830
2018/01/03
6230
LeetCode 134 Gas Station
LeetCode 134 Gas Station 水题,暴力一下就ok class Solution { public: int tag[100005]; int sum[100005]; int canCompleteCircuit(vector<int>& gas, vector<int>& cost) { int l = gas.size(); for(int i=0;i<l;i++) { ta
ShenduCC
2018/12/12
4180
加油站,能怎么贪心?
力扣题目链接:https://leetcode-cn.com/problems/gas-station
代码随想录
2021/11/23
4340
LeetCode 134. 加油站(贪心)
你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] 升。你从其中的一个加油站出发,开始时油箱为空。
Michael阿明
2020/07/13
7230
LeetCode 134. 加油站(贪心)
134. 加油站(前缀和+单调队列|贪心)「建议收藏」
你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] 升。你从其中的一个加油站出发,开始时油箱为空。
全栈程序员站长
2022/09/22
2280
[Leetcode][python]Gas Station/加油站
贪心法。但其实需要证明,证明详见: http://bookshadow.com/weblog/2015/08/06/leetcode-gas-station/ 看懂证明,才能看懂代码
蛮三刀酱
2019/03/26
6790
贪心算法-LeetCode 134、111(递归算法,异或性质,贪心)
对于swap函数使用中间变量的形式大家都不陌生了,但是对于面试官来说这种方法不出奇,并不是面试官想要的!有没有不使用中间变量的呢? 在swap2函数中,我们可以使用这样的方式来交换a和b的值,但是swap2函数有一个缺陷就是,a+b有可能会造成数据溢出!!! 在swap3函数中,使用异或来进行a和b的交换,关于异或的性质如下: 假设有A, B, C三个数,C 相等于 A ^ B, 则必定 A ^ C 相等于 B, B ^ C 相等于 A
算法工程师之路
2019/10/24
7240
【leetcode刷题】T174-加油站
你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] 升。你从其中的一个加油站出发,开始时油箱为空。
木又AI帮
2019/10/03
3860
leetcode-134-加油站
你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] 升。你从其中的一个加油站出发,开始时油箱为空。
chenjx85
2018/09/29
4500
Leetcode No.134 加油站
在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升。 你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] 升。你从其中的一个加油站出发,开始时油箱为空。 如果你可以绕环路行驶一周,则返回出发时加油站的编号,否则返回 -1。
week
2022/01/06
2770
Leetcode No.134 加油站
leetcode每日一题:134 加油站
题目地址: https://leetcode-cn.com/problems/gas-station/
用户3578099
2020/11/30
8690
leetcode每日一题:134 加油站
LeetCode 题目解答——Medium 部分(上)
[Updated on 9/22/2017] 如今回头看来,里面很多做法都不是最佳的,有的从复杂度上根本就不是最优解,有的写的太啰嗦,有的则用了一些过于 tricky 的方法。我没有为了这个再更新,就让它们去吧。
四火
2022/07/19
6480
LeetCode 题目解答——Medium 部分(上)
PAT 1018 Public Bike Management(Dijkstra 最短路)
1018. Public Bike Management (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. One may rent
ShenduCC
2018/04/26
7710
PAT 1018 Public Bike Management(Dijkstra 最短路)
[Java·算法·中等] LeetCode274. H指数 详细解读
你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] 升。你从其中的一个加油站出发,开始时油箱为空。
人不走空
2024/02/20
1720
力扣每日一刷(2023.9.5)
重复这个过程恰好 k 次。可以多次选择同一个下标 i 。 以这种方式修改数组后,返回数组 可能的最大和 。 示例 1: 输入:nums = [4,2,3], k = 1 输出:5 解释:选择下标 1 ,nums 变为 [4,-2,3] 。 示例 2: 输入:nums = [3,-1,0,2], k = 3 输出:6 解释:选择下标 (1, 2, 2) ,nums 变为 [3,1,0,2] 。 示例 3: 输入:nums = [2,-3,-1,5,-4], k = 2 输出:13 解释:选择下标 (1, 4) ,nums 变为 [2,3,-1,5,4] 。 提示:
用户11097514
2024/05/31
1390
力扣每日一刷(2023.9.5)
相关推荐
Leetcode 134 Gas Station
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验