134. Gas Station

不会做。

1. 朴素的想法,就是针对每个位置判断一下,然后返回合法的位置,复杂度O(n^2),显然会超时。

把这道题转化一下吧,求哪些加油站不能走完一圈回到自己,要求O(N)的复杂度。

如果sgas < scost,那么显然所有的站点都无法走完一圈。

2. 考虑怎么进行化简,寻找有没有什么可以利用的性质,考虑可不可以进行递推,对每个位置求出gas - cost,然后很明显观察到如果一个位置为负数,那么这个位置显然不能走完一圈,那么接下来

考虑怎么进行简化,找到负数,知道我们把它变成整数,否则,前面的站点都是无法走完一圈的。考虑接下来怎么进行转化,固定头指针的话,无法对其他节点进行更新,我想不出来,O(n)的方法,解决

这个问题,比如5,-3,5,5,-3.你把一个负数转变为一个正数以后,你怎么判断下一个负数在哪里啊,难道标记一下和,记录一下当前的和么。

先贴一下leetcode原题的解法,从discuss看的:

 class Solution {
public:
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
int n = gas.size();
int start = n - , end = ;
int s = gas[start] - cost[start];
while(end < start) {
if(s >= ) {
s += gas[end] - cost[end];
end++;
} else {
start--;
s += gas[start] - cost[start];
}
}
return s >= ? start : -; }
};

转化的问题不会做。

还是不会,-w-。

134. Gas Station leetcode的更多相关文章

  1. 贪心: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的顺序不能改变,只能通过容器来获得由大到小的顺 ...

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

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

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

  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]. You ...

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

  6. 【LeetCode】134.Gas Station

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

  7. [leetcode greedy]134. Gas Station

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

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

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

随机推荐

  1. Python 遍历目录

    代码: 1.递归使用遍历目录 import os def scanfile(path): filelist = os.listdir(path) allfile = [] for filename i ...

  2. (转)C#开发微信门户及应用(2)--微信消息的处理和应答

    http://www.cnblogs.com/wuhuacong/p/3614175.html 微信应用如火如荼,很多公司都希望搭上信息快车,这个是一个商机,也是一个技术的方向,因此,有空研究下.学习 ...

  3. nim游戏解法(转)

    转自:http://acm.hdu.edu.cn/forum/read.php?fid=9&tid=10617 取火柴的游戏 题目1:今有若干堆火柴,两人依次从中拿取,规定每次只能从一堆中取若 ...

  4. Jmeter JSON断言和响应断言的区别是什么?

    假设响应数据是{"code":0,"datas":{"informationStatus":1}} 响应断言:"code" ...

  5. this、super关键字以及他们各自的作用

    this:代表当前对象的引用,谁来调用我,我就代表谁 super:代表当前对象父类的引用 this和super的使用区别 A:调用成员变量 this.成员变量  调用本类的成员变量,也可以调用父类的成 ...

  6. matlab学习下拉菜单

    用matlab添加listbox控件 修改string和value值,value为几就对应第几行字符串 添加button按钮,将string值改为“选择x轴参数”,字体大小为10 再添加一个按钮,将s ...

  7. Java 将File转换为MultipartFile类型

    首先转换时需要用到commons-fileupload-1.3.2.jar包,若项目中没有就先加入jar包,实现代码如下: 1.根据File创建FileItem import java.io.File ...

  8. vue 登录验证码

    vue 登录验证码 最近在开发pc端项目,配合elementui使用 createCode() { var code = ""; var codeLength = 4; //验证码 ...

  9. phpqrcode生成二维码

    这篇文章讲解得非常详细: https://www.jb51.net/article/136418.htm 备注一下: 如果遇到生成的二维码是一串乱码.只需要在代码最后加上 exit();即可解决,原理 ...

  10. Oralce导入数据库出现某一列的值太大

    这是由于导出的文件所运行的Oracle,和导入所运行的Oracle机器字符集不相同导致的,在UTF-8中有的汉字占三个字节, 并不是所有的都占两个字节,