题目:

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

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

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

  3. [LeetCode 题解]:Gas Station

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 There are ...

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

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

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

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

  7. 20. Candy && Gas Station

    Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...

  8. Gas Station

    Description: There are N gas stations along a circular route, where the amount of gas at station i i ...

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

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

随机推荐

  1. 了解大数据的特点、来源与数据呈现方式以及用Python写Mad Libs游戏

    作业的要求来自于:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/2620. 1.浏览2019春节各种大数据分析报告,例如: 这世间,再 ...

  2. html&css精华总结

    1.标题标签 h标签 2.段落标签 p 3.换行 br 4.空格   5.大于号,小于号 > < 6.双引号 " 7.版权符号 © 8.注册符 ® --------------- ...

  3. tomcat执行文件权限

    .当我在linux下某个目录执命令或者安装的时候通常会提示没有权限或者不可以操作,这时需要加权限 chmod /usr/local/tomcat/bin; 2关于LINUX权限(启动tomcat)-b ...

  4. htons、htonl与字节序大小端

    判断字节序大小端code #include <stdio.h> int main() { ) == ) printf("big endian\n"); else pri ...

  5. KbmMW 服务器架构简介

    kbmmw 由于文档比较少,很多同学开始用时很难理解.一直准备写一个关于kbmmw 架构的东西. 这几天与红鱼儿(blog)研究服务器线程时,整理了一下,大概画了一下kbmmw (版本4.5)服务器的 ...

  6. 2018.09.11 bzoj3629: [JLOI2014]聪明的燕姿(搜索)

    传送门 一道神奇的搜索. 直接枚举每个质因数的次数,然后搜索就行了. 显然质因数k次数不超过logkn" role="presentation" style=" ...

  7. 2018.08.19 NOIP模拟 dp(二分+状压dp)

    Dp 题目背景 SOURCE:NOIP2015-SHY-10 题目描述 一块土地有 n 个连续的部分,用 H[1],H[2],-,H[n] 表示每个部分的最初高度.有 n 种泥土可用,他们都能覆盖连续 ...

  8. BeanUtils.populate的方法的作用

    BeanUtils位于org.apache.commons.beanutils.BeanUtils下面,其方法populate的作用解释如下: 完整方法: BeanUtils.populate( Ob ...

  9. 【转】关于编译链接——gcc/g++

    添加运行时共享库目录 运行使用共享库的程序需要加载共享库(不同于G++ 编译时指定的链接库),添加共享库的步骤: 修改文件 /etc/ld.so.conf 添加共享库目录 运行 ldconfig 同步 ...

  10. php文件上传代码解析

    php文件上传代码解析 is_uploaded_file()  //函数判断指定的文件是否是通过 HTTP POST 上传的,返回一个布尔值. $_FILES['upfile']['tmp_name' ...