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++的更多相关文章

  1. 134. Gas Station leetcode

    134. Gas Station 不会做. 1. 朴素的想法,就是针对每个位置判断一下,然后返回合法的位置,复杂度O(n^2),显然会超时. 把这道题转化一下吧,求哪些加油站不能走完一圈回到自己,要求 ...

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

  3. Gas Station——LeetCode

    There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...

  4. Gas Station leetcode java

    题目: There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. ...

  5. Gas Station|leetcode 贪心

    贪心:尽量将gas[i]-cost[i]>0的放在前面,gas[i]-cost[i]<0的放在后面.(路程的前面有汽油剩下,耗汽油的放在路程的后面). 能否全程通过的 条件 是:sum(g ...

  6. Gas Station [leetcode] 两个解决方案

    因为gas的总数大于cost总时间.你将能够圈住整个城市. 第一溶液: 如果一開始有足够的油.从位置i出发.到位置k时剩余的油量为L(i,k). 对随意的k.L(i,k)依据i的不同,仅仅相差常数. ...

  7. [LeetCode] Gas Station 加油站问题

    There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...

  8. [LeetCode] Gas Station

    Recording my thought on the go might be fun when I check back later, so this kinda blog has no inten ...

  9. leetcode@ [134] Gas station (Dynamic Programming)

    https://leetcode.com/problems/gas-station/ 题目: There are N gas stations along a circular route, wher ...

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

随机推荐

  1. 第二十次CSP考试有感

    这是第二次参加csp考试了,大二上学期参加了第17次csp,160分.刚刚下午结束了第20次csp,200分. 这次比赛规则和以往不同,以前可以携带纸质书籍和usb,提交上去的答案不能实时出成绩.现在 ...

  2. open failed: EACCES (Permission denied)

    出现背景:调用系统相册进行图片展示,但是没有成功,是空白的,且检查权限无问题 解决方法

  3. python matplotlib.pyplot 散点图详解(2)

    python matplotlib.pyplot 散点图详解(2) 上期资料 一.散点图叠加 可以用多个scatter函数叠加散点图 代码如下: import matplotlib.pyplot as ...

  4. git合并内容的1个方式

    诉求 把自己的改动的文件合并到目标分支: 不想改那些不属于自己开发时出现的冲突: 操作 开发分支dev,要合并至master分支 1 所有改动都已提交,保持分支的干净状态(working tree c ...

  5. TP5框架下实现数据库的备份功能-tp5er/tp5-databackup

    1.安装扩展 方法一: composer require tp5er/tp5-databackup dev-master 方法二 composer.json: "require": ...

  6. IDEA - 2019中文版安装教程

    前言 个人安装备忘录 软件简介 IDEA 全称IntelliJ IDEA,是java语言开发的集成环境,在业界被公认为最好的java开发工具之一,尤其在智能代码助手.代码自动提示.重构.J2EE支持. ...

  7. django 自定义auth中user登陆认证以及自写认证

    第一种: 重写自定义auth中user登陆认证模块, 引入MobelBackend from django.contrib.auth.backends import ModelBackend 重写验证 ...

  8. go 成长路上的坑(2)

    请看代码 代码一 package main import "fmt" func main() { var a Integer = 1 var b Integer = 2 var i ...

  9. 51nod1676-无向图同构【乱搞】

    正题 题目连接:http://www.51nod.com/Challenge/Problem.html#problemId=1676 题目大意 给出两张\(n\)个点\(m\)条边的无向图,求这两张图 ...

  10. Linux系统自我学习的一些笔记1

    远程连接: 1.查看IP地址  ip addr 2.远程登陆linux系统 ssh  主机名@IP地址 文件操作: 新建文件touch 例如:touch test.txt (创建单个文件) 例如:to ...