[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 ...
随机推荐
- Linux系统中ElasticSearch搜索引擎安装配置Head插件
近几篇ElasticSearch系列: 1.阿里云服务器Linux系统安装配置ElasticSearch搜索引擎 2.Linux系统中ElasticSearch搜索引擎安装配置Head插件 3.Ela ...
- 第一模块·开发基础-第1章 Python基础语法
Python开发工具课前预习 01 Python全栈开发课程介绍1 02 Python全栈开发课程介绍2 03 Python全栈开发课程介绍3 04 编程语言介绍(一) 05 编程语言介绍(二)机器语 ...
- Dos命令以及相关文件的访问
1.转到相关目录 有时候想从当前目录转到D盘,用此目录cd d:是没有用的, 最好用cd /d d:是可以的 2.查看目录文件 dir 3.往服务器上传文件文件 通过文件浏览器上传文件,只适用于Win ...
- Java开发工程师(Web方向) - 04.Spring框架 - 第5章.Web框架
第5章--Web框架 Web框架概述 Web框架单元测验 本次得分为:13.50/15.00, 本次测试的提交时间为:2017-09-25 1单选(2分) 关于Spring MVC中Dispatche ...
- 定时任务 linux crontab 学习整理
1. 定时任务命令概念 crontab命令用于设置周期性被执行的指令.即设定脚本 按照规定时间执行相关的操作. 2.定时任务书写规范 * * * ...
- 【python 3.6】从网站抓图并存放到本地路径
#!/usr/bin/python # -*- coding: UTF-8 -*- _author_ = 'BH8ANK' import urllib.request import re import ...
- 【Python 开发】第一篇:计算机基础
一.计算机基础 首先Python是一门编程语言 语言: 那什么是语言? 语言就是一种事物与另一种事物沟通的介质.所以说编程语言是程序员跟计算机沟通的介质. 什么是编程: 准确来说就是程序员用计算机所能 ...
- 【Linux】Face Recognition的封装
使用虹软的人脸识别 写了一个linux下的Face Recognition的封装,当作是练习. C++的封装,结合opencv,使用方便.https://github.com/zacario-li/F ...
- anaconda安装scrapy报错解决办法
今天在用anaconda安装scrapy的时候遇见个坑,现在将解决办法发出来,供大家参考使用: 问题描述: anaconda安装scrapy,使用 conda install scrapy 命令.安装 ...
- Thunder团队第一周 - Scrum会议7
Scrum会议7 小组名称:Thunder 项目名称:爱阅app Scrum Master:宋雨 工作照片: 参会成员: 王航:http://www.cnblogs.com/wangh013/ 李传康 ...