LeetCode134:Gas Station
题目:
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]
.
You have a car with an unlimited gas tank and it costs cost[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.
解题思路:
一开始,我打算通过两层循环,依次从某个点出发并测试是否能够运行一圈,可想而知时间复杂度为O(n2),不满足要求。之后看了http://blog.csdn.net/kenden23/article/details/14106137这篇博客的解题思路,才发现有更简单更优雅的解决方案,大概思路如下:
1 如果总的gas - cost小于零的话,那么没有解返回-1
2 如果前面所有的gas - cost加起来小于零,那么前面所有的点都不能作为出发点。
关于第一点无需多言,这里详解下第二点,为什么前面所有的点都不能作为起始站了,原因是:
假设从第0个站点开始,0~1,剩余的煤气left1 = gas[i]-cost[i],如果left为负,则过不去,必须从下一个站点从新开始,如果为正,则1~2时,left2 = gas[1]+left – cost[1],然后是2~3等等继续下去,如果left一直为正,则表示这些站点都可以过去,但当某个站点i~i+1时,left为负数,说明过不去,且之前的所有站点都不能作为起始站,因为,每个站点要到下一个站点时,gas = gas +left,都不能过去,现在如果从某个站点开始,即gas量为它自身,更过不去。
实现代码:
#include <iostream>
#include <vector>
using namespace std; /*
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You have a car with an unlimited gas tank and it costs cost[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.
*/
class Solution {
public:
int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
if(gas.size() == || cost.size() == || gas.size() != cost.size())
return -;
int left = ;
int total = ;
int start = ;
int n = gas.size();
for(int i = ; i < n; i++)
{
left += gas[i] - cost[i];//从i到i+i,剩余的煤气
total += gas[i] - cost[i];
if(left < )//表示前面那些站点都不能作为起始站,现在开始从下一个站点开始
{
start = i + ;
left = ;
}
}
return total >= ? start : -;//煤气总量是否大于等于总消耗 }
};
ing main(void)
{
return ;
}
LeetCode134:Gas Station的更多相关文章
- [Swift]LeetCode134. 加油站 | Gas Station
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- LeetCode OJ: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
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 There are ...
- [LeetCode] Gas Station 加油站问题
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- PAT 1072. Gas Station (30)
A gas station has to be built at such a location that the minimum distance between the station and a ...
- 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 ...
- 20. Candy && Gas Station
Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- Gas Station
Description: There are N gas stations along a circular route, where the amount of gas at station i i ...
- leetcode@ [134] Gas station (Dynamic Programming)
https://leetcode.com/problems/gas-station/ 题目: There are N gas stations along a circular route, wher ...
随机推荐
- Django1.8:403错误:CSRF verification failed. Request aborted.
问题:Django 403错误:CSRF verification failed. Request aborted. 原因:需要加cookie验证 解决方法: 1.在view.py中增加 fr ...
- 安装64位的oracle数据库, 使用自带的sqldeveloper
个人感觉这个东西比plsql要好用, 虽然界面有点丑, 整个使用与plsql也没多大区别, 这里是他的位置C:\oracle_11g\product\11.2.0\dbhome_1\sqldevelo ...
- 使用git提交代码到GitHub
0.下载Git Bash,在Windows系统可以用Git Bash通过简单的命令将代码提交到GitHub1.打开项目所在的文件夹,右键,"Git Bash Here"2.初次使用 ...
- np.random.randn()、np.random.rand()、np.random.randint()
(1)np.random.randn()函数 语法: np.random.randn(d0,d1,d2……dn) 1)当函数括号内没有参数时,则返回一个浮点数: 2)当函数括号内有一个参数时,则返回秩 ...
- Island Transport
Island Transport http://acm.hdu.edu.cn/showproblem.php?pid=4280 Time Limit: 20000/10000 MS (Java/Oth ...
- 使用jsonp跨域发送请求
如果获取的数据文件存放在远程服务器上(域名不同,也就是跨域获取数据),则需要使用jsonp类型. 使用这种类型的话,会创建一个查询字符串参数 callback=? ,这个参数会加在请求的URL后面. ...
- asp.net状态保持
1.首先如果不是asp.net webform而只是一个纯粹的html页面和ashx一般处理程序的话,因为http协议的无状态,每一次的页面请求都会重新实例化一个页面对象(注意实例化页面对象其实是通过 ...
- part1:8-远程登录Linux
Linux远程登录 Linux系统中是通过ssh服务实现的远程登录功能.默认ssh服务开启了22端口,而且在安装完成系统时,这个服务已经安装,并且是开机启动的.所以不需要额外配置就能直接远程登录Lin ...
- 2018.09.27 hdu5564Clarke and digits(数位dp+矩阵快速幂)
传送门 好题啊. 我只会写l,rl,rl,r都很小的情况(然而题上并没有这种数据范围). 但这个dp转移式子可以借鉴. 我们用f[i][j][k]f[i][j][k]f[i][j][k]表示当前在第i ...
- 2018.07.04 POJ 2398 Toy Storage(二分+简单计算几何)
Toy Storage Time Limit: 1000MS Memory Limit: 65536K Description Mom and dad have a problem: their ch ...