题目:

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. ubuntu安装谷歌拼音输入法

    在这篇教程中,我将告诉你如何在ubuntu系统上安装谷歌拼音输入法.谷歌拼音输入法有基于ibus框架的,也有基于fcitx框架的.我只演示fcitx框架下谷歌拼音输入法的安装,因为ibus框架的谷歌拼 ...

  2. cdoj203-Islands 【并查集】

    http://acm.uestc.edu.cn/#/problem/show/203 Islands Time Limit: 30000/10000MS (Java/Others)     Memor ...

  3. Java label

    标号label提供了一种简单的break语句所不能实现的控制循环的方法.当你嵌套在几层循环中想退出循环时,break只能退出一重循环,可以用标号标出想退出哪一个语句. 标号的命名不能以"_& ...

  4. python之递归锁【Rlock】

    # 递归锁:就是一把锁中还有一把小锁,比如学校的大门口有一个大锁,学校里的 #每个教室也有一把小锁,以后所有的锁都用rlock就可以了,不要用lock,尤其是多层锁的时候,必须要用递归锁 import ...

  5. centos自定义安装pip3

    题记 在之前的文章centos云服务器安装Python3记录 记录了怎么自定义安装 Python3 ,在后边测试pip3的时候发现了个问题: pip --version terminal 打印: pi ...

  6. 动态调用WebService方法

      好像很多人做WebService的时候都是直接添加引用的方式,然后调用服务端的方法.这样就个问题,就是每次我服务端添加了方法或者修改了方法后都要更新Web引用,这样比较麻烦.下面给一个不用添加引用 ...

  7. php5.3 php-fpm 开启 关闭 重启

    自php5.3开始,php源码中包含了php-fpm,不需要单独通过补丁的方式安装php-fpm,在源码安装的时候直接 configure 中增加参数 –enable-fpm 即可.   所以启动.关 ...

  8. Codeforces 660A. Co-prime Array 最大公约数

    A. Co-prime Array time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  9. 还不好好读书吗?清华3D录取通知书出炉,还能动!

    近日,清华大学2018录取通知书“亮相”!看完后,网友直呼:哪里可以买到? 打开录取通知书 3D“二校门”跃然纸上 由清华师生共同打造.手工定制.独一无二的2018新版录取通知书来了!在新版录取通知书 ...

  10. Netty系列(四)TCP拆包和粘包

    Netty系列(四)TCP拆包和粘包 一.拆包和粘包问题 (1) 一个小的Socket Buffer问题 在基于流的传输里比如 TCP/IP,接收到的数据会先被存储到一个 socket 接收缓冲里.不 ...