[Leetcode] gas station 气站
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.
题意:N个气站围成一圈,在气站i有气gas[i],从i到i+1要用掉气cost[i],问能否环绕一圈
思路:走完整圈的前提条件是gas的总量要大于cost的总量。能从一个气站到下一个气站的条件是,之前多余的总气sum加上当前气站的总量要不小于当前的cost。这两点是要意识到的,其次,我们在想问题时,要将圆当做数组来想。
最关键的是,若从一个气站到下一个 气站不满足条件了,起始点的下标应该怎么计算?
若当前气站i不满足条件,起始点start的下标应从i+1开始,为什么?如:从下标为0开始,到下标为5不满足,为什么起始点下标不是从1开始,而是从6开始?因为,在当前下标之前每过一个气站之后sum都是大于等于0的(不然早就不满足了,为什么?sum小于0说明之前的消耗大于提供的),即气有剩余,当前站点之前的任意一个小的区间的sum都是小于当前这整段区间的sum,就这样到下标为5时,还是不满足,所以当前之前的任意下标都是不满足条件的。代码如下:
class Solution {
public:
int canCompleteCircuit(vector<int> &gas, vector<int> &cost)
{
int sum=,start=,total=;
for(int i=;i<gas.size();++i)
{
sum+=gas[i]-cost[i];
total+=gas[i]-cost[i]; if(sum+gas[i]<cost[i]) //这里直接写sum<0也可以
{
start=i+;
sum=;
}
}
if(total<)
return -; return start;
}
};
参考了牛客网友的回答。
[Leetcode] gas station 气站的更多相关文章
- [leetcode]Gas Station @ Python
原题地址:https://oj.leetcode.com/problems/gas-station/ 题意: There are N gas stations along a circular rou ...
- LeetCode: Gas Station 解题报告
Gas Station There are N gas stations along a circular route, where the amount of gas at station i is ...
- [LeetCode] Gas Station,转化为求最大序列的解法,和更简单简单的Jump解法。
LeetCode上 Gas Station是比较经典的一题,它的魅力在于算法足够优秀的情况下,代码可以简化到非常简洁的程度. 原题如下 Gas Station There are N gas stat ...
- [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——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 贪心
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- [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 ...
- [LeetCode] Minimize Max Distance to Gas Station 最小化去加油站的最大距离
On a horizontal number line, we have gas stations at positions stations[0], stations[1], ..., statio ...
随机推荐
- 使用union 外加count
explain extended and name='aaa')) t; +----+--------------+------------+-------+---------------+----- ...
- C++ 基础面试题-1
请说出下面代码在32位系统下的输出内容 /* ** 2018/03/21 21:43:00 ** Brief: ** Author:ZhangJianWei ** Email:Dream_Dog@16 ...
- openjudge-2的100次方阶乘
开始进行的第一天 #include <stdio.h> #include <string.h> int main() { int n; scanf("%d" ...
- [问题] docker: Failed to start Docker Application Container Engine.
docker无法启动: # systemctl restart docker Job for docker.service failed because the control process exi ...
- Entity Framework 基本概念
概念 LINQ to Entities 一种 LINQ 技术,使开发人员可以使用 LINQ 表达式和 LINQ 标准查询运算符,针对实体数据模型 (EDM) 对象上下文创建灵活的强类型化查询. ESQ ...
- Thunder团队第二周 - Scrum会议3
Scrum会议3 小组名称:Thunder 项目名称:爱阅app Scrum Master:代秋彤 工作照片: 参会成员: 王航:http://www.cnblogs.com/wangh013/ 李传 ...
- 关于CString总结
前言:串操作是编程中最常用也最基本的操作之一. 做为VC程序员,无论是菜鸟或高手都曾用过CString.而且好像实际编程中很难离得开它(虽然它不是标准C++中的库).因为MFC中提供的这个类对 我们操 ...
- 3ds Max学习日记(九)
添加了几根线条,又跟着教程细扣了一下面部细节,并把鼻子做的更细致了一些,如图: 又做了好久,按着教程抠出了眼睛和嘴,感觉自己做的模型就跟鬼似的... 做了下头发,看了下视频最后,并没教如何 ...
- PowerMock用法[转]
转:http://agiledon.github.io/blog/2013/11/21/play-trick-with-powermock/ 当我们面对一个遗留系统时,常见的问题是没有测试.正如Mic ...
- Python运算符与编码
阅读目录 while 循环 运算符 编码的问题 单位转换 整数 布尔值 while 循环 在生活中,我们遇到过循环的事情吧?比如循环听歌.在程序中,也是存才的,这就是流程控制语句 while 1.基本 ...