题目描述

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:

Input:
gas  = [1,2,3,4,5]
cost = [3,4,5,1,2]Output: 3Explanation:
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:

Input:
gas  = [2,3,4]
cost = [3,4,3]Output: -1Explanation:
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.
Therefore, you can't travel around the circuit once no matter where you start.

思路

从左到右累积,如果当前位置汽油<0,说明当前位置无法继续,从下一位置继续。
最后所有的汽油量和大于所有消耗量时,才有唯一解。

代码

class Solution {public:int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {int owe = 0;int st = 0;int tot = 0;for (int i=0; i<gas.size(); ++i) {tot += gas[i] - cost[i];owe += gas[i] - cost[i];if (tot < 0) {st = i+1;tot = 0;}}if (owe >= 0) return st;return -1;}
};

【LeetCode 134】 Gas Station相关推荐

  1. leetcode 134. 加油站(Gas Station)

    目录 题目描述: 示例 1: 示例 2: 解法: 题目描述: 在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升. 你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+ ...

  2. 重复次数最多的 子串_每日算法系列【LeetCode 424】替换后的最长重复字符

    题目描述 给你一个仅由大写英文字母组成的字符串,你可以将任意位置上的字符替换成另外的字符,总共可最多替换 k 次.在执行上述操作后,找到包含重复字母的最长子串的长度. 示例1 输入: s = &quo ...

  3. 【LeetCode - 32】最长有效括号

    给你一个只包含 '(' 和 ')' 的字符串,找出最长有效(格式正确且连续)括号子串的长度. 示例 1: 输入:s = "(()" 输出:2 解释:最长有效括号子串是 " ...

  4. 【LeetCode题解】二叉树的遍历

    我准备开始一个新系列[LeetCode题解],用来记录刷题,顺便复习一下数据结构与算法. 1. 二叉树 二叉树(binary tree)是一种极为普遍的数据结构,树的每一个节点最多只有两个节点--左孩 ...

  5. 如何给柱状图柱子添加阴影_【LeetCode日记】84. 柱状图中最大的矩形

    题目描述 ` 给定 n 个非负整数,用来表示柱状图中各个柱子的高度.每个柱子彼此相邻,且宽度为 1 . 求在该柱状图中,能够勾勒出来的矩形的最大面积. 以上是柱状图的示例,其中每个柱子的宽度为 1,给 ...

  6. 【LeetCode笔记】253. 会议室 II(Java、偏数学)

    文章目录 题目描述 思路 && 代码 计划里 hot 100 + 剑指Offer 的题目中唯一一道会员题,同时也是最后一道没写的题,刚好今天 leetcode 发了一天会员可以写上-简 ...

  7. 【LeetCode笔记】301. 删除无效的括号(Java、DFS、字符串)

    文章目录 题目描述 思路 && 代码 二刷 题目描述 [所有可能结果]-> [暴力DFS] 思路 && 代码 代码比较长,但是总体思路很清晰. 剪枝:舍弃左括号. ...

  8. 【LeetCode 871】 Minimum Number of Refueling Stops

    题目描述 A car travels from a starting position to a destination which is target miles east of the start ...

  9. 【LeetCode 总结】Leetcode 题型分类总结、索引与常用接口函数

    文章目录 零. Java 常用接口函数 一. 动态规划 二. 链表 三. 哈希表 四. 滑动窗口 五. 字符串 六. DFS.BFS 七. 二分法 八. 二叉树 九. 偏数学.过目不忘 and 原地算 ...

  10. 【LeetCode笔记】剑指 Offer 03. 数组中重复的数字(Java、哈希表、原地算法)

    文章目录 题目描述 思路 & 代码 二刷 题目描述 倒是和leetcode 287 寻找重复数很像..但是不能使用那道题的快慢指针法(也可能是我太菜了) 重点在于题干的描述[长度为 n 的数组 ...

最新文章

  1. C语言优势大揭露,你还在等什么呢?
  2. 最小二乘法+牛顿法+拟牛顿法+梯度下降法+梯度上升法+共轭梯度法
  3. MySQL索引的查看创建和删除
  4. 代码坏味道之非必要的
  5. linux界面添加地址,Linux系统下图形界面更改IP地址
  6. Linux函数--inet_pton / inet_ntop
  7. matlab电类,985电气研二,有发过考研经验贴 电气电力类的有
  8. Ajax链接输出数据库
  9. Web.py Cookbook 简体中文版 - 如何使用web.background
  10. 【算法大赛直播周】如何让人人都可以使用AI技术?北大崔斌教授亲解自动化机器学习
  11. docker简单介绍----存储
  12. 应云而生,原力觉醒——解读云原生基础设施 | 凌云时刻
  13. 微信小程序盲盒系统源码 带教程
  14. selenium自动化学习--截取长图的方法
  15. adb 的批处理命令
  16. 微信 jssdk 看着文档简单总结
  17. opencv无法打开摄像头
  18. 一个用interproscan做基因注释的简易教程
  19. 花卉识别python_基于深度学习的花卉识别系统设计与实现
  20. ERP软件定制是把双刃剑

热门文章

  1. java 字符串MD5的加密
  2. PIEO目前学术界最先进的数据包调度器介绍。看不懂
  3. 【MyBatis】-03- 核心配置
  4. 关于CMOS和TTL的DC参数
  5. PDF转图片以及转html
  6. 鸿蒙 HAIWEI DevEco Studio 安装配置,运行Hello World!
  7. 20+个创意十足的斜线网站设计
  8. deeplearning.ai第四课第一周:卷积神经网络
  9. OSChina 周六乱弹 ——坐在美女同事旁边的理由。
  10. valse2016参会总结