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.

问题:在一个环形的路径上,有 n 个加油站。每个加油站 i 有汽油 gas[i] ,从 i 行驶到下一个加油站需要耗油 cost[i],汽车的油箱没有限制。找出一个加油站,从该加油站能走完整个环形路径。其中,本题目只有唯一一个解。

思路:

环形路径并且需要走完一整圈,则有:

1),若全部加油站的汽油 小于 路径总消耗油量,则不可能走完全程。

2),反之,若全部加油站的汽油 大于等于 路径总消耗油量,那么必然有至少存在一个站可以出发走完全程。

以上是我能想到的,至于如何环形路径中找到 2) 中的加油站,则没有什么思路。

在网上看到一个讲解,才明白,而解决方案的关键就是题目的限制条件“本题目只有唯一一个解”。

由于只有唯一解 i ,则以下性质:

性质1. 任何 j ( 0<= j < i) 都不可能到达 i 。

性质2. 任何 k (i <= i < n) ,i 都可以到达。

所以,从左往右扫,当遇到第一个i, 满足 sum[i,j] (i <= j < n) 均大于等于0,那么 i 便是题目的解。

若对每一个元素都检查 sum[i,j] ,那么耗时为 O(n*n) 。这里借助 性质1 优化查找。

假设 sum[i1, j] (i1<= j < t2) 都大于等于 0 ,当 j = t2 时,sum[i1, j] 小于 0 ,那么 i1 不是解,根据性质1,i1~ t2 都不是解。可以从 t2+1 开始继续扫。

     int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {

         vector<int> rem(gas.size());

         int totalrem = ;
for ( int i = ; i < gas.size(); i ++){
rem[i] = gas[i] - cost[i];
totalrem += rem[i];
} if (totalrem < ) {
return -;
} int l = ;
int sum = ; for (int i = ; i < rem.size(); i++) {
sum += rem[i];
if(sum < ){
sum = ;
l = i + ;
}
} return l;
}

记录以下:最近做的几道 Greedy 类型的题目,自己基本没有很好的解题思路,即使看了几个讲解也没有找到他们的共通之处。看来自己还没有很好掌握 Greedy 的求解。

本题思路参考 [LeetCode] Gas Station, 喜刷刷.

[LeetCode] 134. Gas Station 解题思路的更多相关文章

  1. 【LeetCode】Gas Station 解题报告

    [LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...

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

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

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

  4. [leetcode]134. Gas Station加油站

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

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

  6. leetcode 134. Gas Station ----- java

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

  7. [leetcode] 134. Gas Station (medium)

    原题 题意: 过一个循环的加油站,每个加油站可以加一定数量的油,走到下一个加油站需要消耗一定数量的油,判断能否走一圈. 思路: 一开始思路就是遍历一圈,最直接的思路. class Solution { ...

  8. 134. Gas Station leetcode

    134. Gas Station 不会做. 1. 朴素的想法,就是针对每个位置判断一下,然后返回合法的位置,复杂度O(n^2),显然会超时. 把这道题转化一下吧,求哪些加油站不能走完一圈回到自己,要求 ...

  9. 贪心:leetcode 870. Advantage Shuffle、134. Gas Station、452. Minimum Number of Arrows to Burst Balloons、316. Remove Duplicate Letters

    870. Advantage Shuffle 思路:A数组的最大值大于B的最大值,就拿这个A跟B比较:如果不大于,就拿最小值跟B比较 A可以改变顺序,但B的顺序不能改变,只能通过容器来获得由大到小的顺 ...

随机推荐

  1. unity3d android导出项目编译Multiple dex files define Lcom/unity3d/player/UnityPlayerActivity

    unity3d版本: 4.1.2 在导出android工程进行编译时,发现出现Multiple dex files define Lcom/unity3d/player/UnityPlayerActi ...

  2. A+B问题(java)

    import java.util.Scanner; public class Main { public static void main ( String args[] ) { Scanner in ...

  3. spring-junit的标注总结

    如果在测试类的类名上面添加了注解 @ContextConfiguration("meta/springConfigured.xml") 如何在标注了@Test的方法里面获取上面xm ...

  4. Windows计划任务 未能启动

    近期在windows server 2003上运行的备份脚本,在7月23日之后,没再运行,在计划任务里看到的状态是:未能启动.结果手动运行了一下备份脚本,没有问题,可以正常运行,但是在计划任务里为什么 ...

  5. iOS 网络与多线程--7.Performselector消息处理方法

    创建一个IOSApp类 IOSApp.h文件 #import <Foundation/Foundation.h> @interface IOSApp : NSObject // 1.添加一 ...

  6. new date() 函数在浏览器中的兼容问题!!

    引言: 同一种语言javascript,在不同的浏览器中,存在语言兼容性问题,本质上是由于不同的浏览器是支持的语言标准和实现上各有差异.本文将基于new Date来创建Date对象来分析这个问题. v ...

  7. Windows 7 64位下解决不能创建Django项目问题

    把djingo-admin.py的全路径写出来 在cmd命令行下直接输入python C:\Python27\Scripts\django-admin.py startproject site(sit ...

  8. 安装.net framework 4.0失败,出现HRESULT 0xc8000222错误代码

    安装了一上午的.net framework 4.0,各种失败,查了好多答案,各种不靠谱,最后终于找到答案了 和Windows Update有关系,给目录名重命名一下再次安装,即安装成功了! 方法: 1 ...

  9. Matlab与外部接口:MAT文件基础

    MAT 文件MAT文件是MATLAB使用的一种特有的二进制数据文件.MAT文件可以包含一个或者多个MATLAB 变量.MATLAB通常采用MAT文件把工作空间的变量存储在磁盘里,在MAT文件中不仅保存 ...

  10. Python3 如何优雅地使用正则表达式(详解四)

    更多强大的功能 到目前为止,我们只是介绍了正则表达式的一部分功能.在这一篇中,我们会学习到一些新的元字符,然后再教大家如何使用组来获得被匹配的部分文本. 更多元字符 还有一些元字符我们没有讲到,接下来 ...