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. js获取当前具体时间

    /** * 获取当前时间 * @param isTime true:显示日期和时间,如:2018-09-20 13:25:12:false:显示日期,如:2018-09-20. * @returns ...

  2. elasticsearch重建索引

    1.重建索引 一个field的设置是不能被修改的,如果要修改一个Field,那么应该重新按照新的mapping,建立一个index,然后将数据批量查询出来,重新用bulk api写入index中 批量 ...

  3. 关于MySQL Server影响ASP.NET网站使用的问题:未能加载文件或程序集MySql.Web.v20

    最近开发的ASP.NET MVC 4网站,之前头头说如果遇到装过MySQL的机器就绕着走,还觉得奇怪 嘛,该来的迟早都会来 于是撞上了一台 启动网站再访问,总是出错,提示“未能加载文件或程序集”,名字 ...

  4. eas之dep的前置脚本和后置脚本

    dep的前置脚本和后置脚本,什么时候写,是这样解释的:    前置脚本是在方法前执行,后置脚本是在方法后执行    1.比如保存扩展,如果你要在保存前校验某个字段的值,你要在前置脚本中写,如果要保存后 ...

  5. 15.5.2 【Task实现细节】骨架方法的结构

    尽管骨架方法中的代码非常简单,但它暗示了状态机的职责.代码清单15-11生成的骨架方 法如下所示: [DebuggerStepThrough] [AsyncStateMachine(typeof(De ...

  6. HAOI2010软件安装(树形背包)

    HAOI2010软件安装(树形背包) 题意 有n个物品,每个物品最多会依赖一个物品,但一个物品可以依赖于一个不独立(依赖于其它物品)的物品,且可能有多个物品依赖一个物品,并且依赖关系可能形成一个环.现 ...

  7. hashlib模块 简单了解

    import hashlib '''不可逆加密''' password = 'wwwwww7777'.encode('utf8') word = hashlib.md5(password) # md5 ...

  8. Spring MVC学习总结(3)——Spring3 MVC详解

    DispatcherServlet 前置控制器 使用Spring MVC,配置DispatcherServlet是第一步.DispatcherServlet是一个Servlet,所以可以配置多个Dis ...

  9. (36)Spring Boot Cache理论篇【从零开始学Spring Boot】

    Spring Boot Cache理论篇 在上一篇中我们介绍了Spring Boot集成Redis的实战例子,里面使用到了Spring Cache,那么什么是Spring Cache呢,本章将会做一个 ...

  10. git分支合并概念

    git merge命令用于合并指定分支到当前分支. git merge命令用于合并指定分支到当前分支. git merge命令用于合并指定分支到当前分支. 创建与合并分支 阅读: 931277 在版本 ...