gas-station leetcode C++
There are N gas stations along a circular route, where the amount of gas at station i isgas[i].
You have a car with an unlimited gas tank and it costscost[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.
C++
class Solution {
public:
int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
int total = 0, sum = 0;
int index = -1;
for (int i = 0; i < gas.size(); i++) {
sum += gas[i] - cost[i];
total += gas[i] - cost[i];
if (sum < 0) {
sum = 0;
index = i;
}
}
return total >= 0 ? index + 1 : -1;
}
};
gas-station leetcode C++的更多相关文章
- 134. Gas Station leetcode
134. Gas Station 不会做. 1. 朴素的想法,就是针对每个位置判断一下,然后返回合法的位置,复杂度O(n^2),显然会超时. 把这道题转化一下吧,求哪些加油站不能走完一圈回到自己,要求 ...
- Gas Station [LeetCode]
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- Gas Station——LeetCode
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- Gas Station leetcode java
题目: There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. ...
- Gas Station|leetcode 贪心
贪心:尽量将gas[i]-cost[i]>0的放在前面,gas[i]-cost[i]<0的放在后面.(路程的前面有汽油剩下,耗汽油的放在路程的后面). 能否全程通过的 条件 是:sum(g ...
- Gas Station [leetcode] 两个解决方案
因为gas的总数大于cost总时间.你将能够圈住整个城市. 第一溶液: 如果一開始有足够的油.从位置i出发.到位置k时剩余的油量为L(i,k). 对随意的k.L(i,k)依据i的不同,仅仅相差常数. ...
- [LeetCode] Gas Station 加油站问题
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- [LeetCode] Gas Station
Recording my thought on the go might be fun when I check back later, so this kinda blog has no inten ...
- leetcode@ [134] Gas station (Dynamic Programming)
https://leetcode.com/problems/gas-station/ 题目: There are N gas stations along a circular route, wher ...
- [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 ...
随机推荐
- Hadoop 2.x安装
1.关闭防火墙 systemctl stop firewalld.service #停止firewallsystemctl disable firewalld.service #禁止firewall开 ...
- 【MySQL】MySQL基础(SQL语句、约束、数据类型)
数据库的基本概念 什么是数据库? 用于存储和管理数据的仓库 英文单词为:DataBase,简称DB 数据库的好处? 可以持久化存储数据 方便存储和管理数据 使用了统一的方式操作数据库 -- SQL 常 ...
- HDU2063 过山车(二分匹配)
过山车 HDU - 2063 RPG girls今天和大家一起去游乐场玩,终于可以坐上梦寐以求的过山车了.可是,过山车的每一排只有两个座位,而且还有条不成文的规矩,就是每个女生必须找个个男生做part ...
- CodeForce-785B Anton and Classes(简单贪心)
Anton and Classes Anton likes to play chess. Also he likes to do programming. No wonder that he deci ...
- STM32CbueIDE 体验
STM32CbueIDE 体验 下载安装 官网下载链接:https://www.st.com/zh/development-tools/stm32cubeide.html. 软件启动时会令设置工作目录 ...
- 【PHP数据结构】图的遍历:深度优先与广度优先
在上一篇文章中,我们学习完了图的相关的存储结构,也就是 邻接矩阵 和 邻接表 .它们分别就代表了最典型的 顺序存储 和 链式存储 两种类型.既然数据结构有了,那么我们接下来当然就是学习对这些数据结构的 ...
- Shell系列(6)- 管道符
多命令顺序执行 多命令执行符 格式 作用 ; 命令1 ; 命令2 连接命令:多个命令顺序执行,命令之间没有任何逻辑联系:前面命令报错,后面命令照常执行 && 命令1 && ...
- 低代码+RPA+AI,能否让ERP焕发下一春?
从2004年开始,国内ERP项目的实施便在各大企业热火朝天地展开,2014年,国内大中型企业已经基本完成了ERP系统的普及.ERP已经在大中型企业中成为不可或缺的关键信息系统.企业核心业务的流转与管控 ...
- 深度理解JVM
1. 环境搭建 安装jdk 2. 内存溢出场景模拟 public class Test01 { public static void main(String[] args) { //测试内存溢出 ...
- 『GoLang』数组与切片
数组 数组是具有相同唯一类型的一组已编号且长度固定的数据项序列(这是一种同构的数据结构):这种类型可以是任意的原始类型例如整型.字符串或者自定义类型. 数组长度必须是一个常量表达式,并且必须是一个非负 ...