134. Gas Station leetcode
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的更多相关文章
- 贪心: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的顺序不能改变,只能通过容器来获得由大到小的顺 ...
- leetcode@ [134] Gas station (Dynamic Programming)
https://leetcode.com/problems/gas-station/ 题目: There are N gas stations along a circular route, wher ...
- [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 ...
- 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 ...
- 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 ...
- 【LeetCode】134.Gas Station
Problem: There are N gas stations along a circular route, where the amount of gas at station i is ga ...
- [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 ...
- [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 ...
- 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)dotnet开源电商系统-brnshop&brnMall 和老外开发的nopCommerce(dotnet两套电商来PK--第一篇)
一直想做电商软件,但是实在不想学PHP了,所以前后关注了这两个开源电商系统.一个是国人出品的,一个据说是俄罗斯人写得(不知道对不对).目前两个开源软件都在学习了解中,以下的博文可能会涉及到这两套系统, ...
- (转)C#开发微信门户及应用(3)--文本消息和图文消息的应答
http://www.cnblogs.com/wuhuacong/p/3622636.html 微信应用如火如荼,很多公司都希望搭上信息快车,这个是一个商机,也是一个技术的方向,因此,有空研究下.学习 ...
- (转)Arcgis for JS之对象捕捉
http://blog.csdn.net/gisshixisheng/article/details/44098615 在web操作,如绘制或者测量的时候,为了精确,需要捕捉到某一图层的对象,在此,讲 ...
- 移动端mui常用方法
本文分享一些用Mui的时候所采的坑 1.mui中上拉刷新事件a标签中的链接.元素onclick事件在手机上点击不了 mui('body').on('tap','a',function(){docume ...
- 5G vs AI谁更有前途?
5G vs AI谁更有前途? 5G通信技术和AI人工智能技术是两个不同层面的技术领域,而它们两者都将在未来20年内对世界的发展有着革命性和里程碑式的影响.未来5G和AI谁更有前途呢? 5G技术的发展和 ...
- Wireshark 如何捕获网络流量数据包
转自:http://www.4hou.com/web/7465.html?hmsr=toutiao.io&utm_medium=toutiao.io&utm_source=toutia ...
- 操作符重载(day07)
二十 操作符重载 eg:复数x+yi +4i (+2i) + (+4i) = +6i 双目操作符(L # R) 1.1 运算类的双目操作符:+ - * / -->左右操作数可以是左值也可以是右值 ...
- Python学习笔记之异常处理
1.概念 Python 使用异常对象来表示异常状态,并在遇到错误时引发异常.异常对象未被捕获时,程序将终止并显示一条错误信息 >>> 1/0 # Traceback (most re ...
- nginx下部署showdoc
1. 安装nginx服务器sudo apt-get install nginx -y 2. 启动服务sudo service nginx start 3. 安装php环境 sudo apt-get i ...
- javascript基础扫盲
JavaScript基础扫盲 null和undefined 非十进制的表示方法 强制类型转换 运算 null和undefined null是一个是非来表示一个空对象的,故 typeof 的返回值是ob ...