题目:

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. 两数之和-数据结构设计 · Two Sum - Data structure design

    [抄题]: 设计b并实现一个 TwoSum 类.他需要支持以下操作:add 和 find.add -把这个数添加到内部的数据结构.find -是否存在任意一对数字之和等于这个值 [思维问题]: 不知道 ...

  2. php Pthread 多线程 (四) 共享内存

    有些时候我们希望在多个线程中共享一些需要的数据,我们可以使用shmop扩展. <?php class Count extends Thread { private $name = ''; pub ...

  3. 【转】HttpApplication的认识与加深理解

    原文:http://www.cnblogs.com/whtydn/archive/2009/10/16/1584584.html HttpApplication对象是经由HttpApplication ...

  4. 解决ios手机页面overflow scroll滑动很卡的问题

    在移动端html中经常出现横向/纵向滚动的效果,但是在iPhone中滚动速度很慢,感觉不流畅,有种卡卡的感觉,但是在安卓设备上没有这种感觉; 要解决这个问题很简单: 一行代码搞定 -webkit-ov ...

  5. Golang之(if)流程控制

    (if)我能坚持做好一只地鼠,慢慢的刨坑,讲洞挖的深一点…… package main import ( "fmt" ) func testIf1() { num := //var ...

  6. Python.__getattr__Vs__getattribute__

    __getattr__ Vs __getattribute__ class Fish(object): def __getattr__(self, key): if key == 'color': p ...

  7. 03 解析库之Beautifulsoup模块

    Beautifulsoup模块   一 介绍 Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式 ...

  8. 品味性能之道<一>:性能测试思维与误区

           <java performance><品悟性能优化 oracle><面向模式的软件架构-模式系统>读书笔记应用调优分享.      性能问题的解决,首 ...

  9. 关于UI设计的一些工作了解

    关于UI设计相信大家在刚接触UI的时候都不太了解,我来说说我在一段学习时间后的了解. UI从工作内容上来说分为3大类,即研究工具,研究人与界面的关系,研究人与之相应. ​ UI设计师的职能一个是图形设 ...

  10. Linux多线程服务端编程 使用muduo C++网络库 学习笔记 日志log

    代码来自陈硕开源代码库 muduo中 地址是https://github.com/chenshuo/muduo #pragma once #include <string> #define ...