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. Windows Server 2022 OVF(SLIC 2.6)

    请访问原文链接:https://sysin.org/blog/windows-server-2022-ovf/,查看最新版.原创作品,转载请保留出处. 作者:gc(at)sysin.org,主页:ww ...

  2. [AtcoderABC200E]Patisserie

    [AtcoderABC200E]Patisserie 题面翻译 对于一个三元组\((i,j,k)\) 我们对它按如下要求进行升序排序: 第一关键词 \(i + j + k\) 即三者总和 第二关键词 ...

  3. jquery动态生成dom(比如append)导致js事件无效

    如果无效用这个方法: on() 方法在被选元素及子元素上添加一个或多个事件处理程序. <div id="zkdiv">  <input type="bu ...

  4. css定位:p:nth-child(n)

    p:nth-child(n) 定位p标签下的第一个元素,下标从1开始. 首先是一个标签下有多个相同的元素. 如index_service_cnt js_service_list下有多个class=&q ...

  5. javascript 一些函数的实现 Function.prototype.bind, Array.prototype.map

    * Function.prototype.bind Function.prototype.bind = function() { var self = this, context = [].shift ...

  6. C# 在PPT中添加数学公式

    本次内容介绍在C#程序中给PPT幻灯片添加Latex数学公式,添加公式前,首先需要在幻灯片中插入一个Shape形状,在形状的段落中通过方法Paragraphs.AddParagraphFromLate ...

  7. 华为云计算IE面试笔记-华为云计算解决方案业务迁移支持哪些迁移?有哪些特点?请描述基本的业务交付流程、业务迁移流程和原则。

    1. 迁移场景:华为云计算解决方案按照源端环境来说,支持P2V.V2V(P2V:物理设备(操作系统及其上的应用软件和数据)迁移到华为虚拟化平台.V2V:其他厂商的虚拟化平台迁移到华为虚拟化平台.)以及 ...

  8. CF848E-Days of Floral Colours【dp,分治NTT】

    正题 题目链接:https://www.luogu.com.cn/problem/CF848E 题目大意 \(2n\)个花排成一个圆环,\(n\)种颜色每种两个,要求两个相同颜色之间最小距离为\(1, ...

  9. IO流基本概念

    IO流主要分为两类 节点流:直接能够进行数据写入或读取的I0流.可以单独执行读写操作,但是功能比较单一,只能进行一些基本 的操作.例如:FileInputStream FileInputStream ...

  10. 1-SQL Server2019安装

    sql server2019安装 首先去官网下载(下载express版本): 打开安装程序 选择自定义 更改一下安装目录,点击安装 等待安装 等安装完成后,出现如下页面 选择SQL Server独立安 ...