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 ...
随机推荐
- 使用File类操作文件或目录的属性
在学I/O流之前,我先总结一下使用File类操作文件或目录的属性. package com.File; import java.io.File; import java.io.IOException; ...
- ROS:Nvidia Jetson TK1开发平台
原文链接: http://wiki.ros.org/NvidiaJetsonTK1 1. Nvidia Jetson TK1 Jetson TK1 comes pre-installed with L ...
- react基础篇二
组件 & Props & 生命周期 组件可以将UI切分成一些独立的.可复用的部件,这样你就只需专注于构建每一个单独的部件. 组件从概念上看就像是函数,它可以接收任意的输入值(称之为“p ...
- 11.03 在外链接中用OR逻辑
select e.ename,d.deptno,d.dname,d.locfrom dept d left join emp e on(d.deptno = e.deptnoand (e.deptno ...
- [转]使用Fiddler进行iOS APP的HTTP/HTTPS抓包
Fiddler不但能截获各种浏览器发出的HTTP请求, 也可以截获各种智能手机发出的HTTP/HTTPS请求.Fiddler能捕获iOS设备发出的请求,比如IPhone, IPad, MacBook. ...
- 【编程工具】Vim编辑器的使用
1.Vim简介 Vim最初起源于古老的贝尔实验室,由"Bram Moolenaar等人"开发,是一个功能强大的文本编辑器,被推崇为类Vi编辑器中最好的一个. Vim是一个类 ...
- apache+php连接数据库
######## 安装APACHE ##############安装apr/usr/src/apache+php/tar xf apr-1.5.2.tar.gzcd apr-1.5.2./config ...
- Python中字符串操作函数string.split('str1')和string.join(ls)
Python中的字符串操作函数split 和 join能够实现字符串和列表之间的简单转换, 使用 .split()可以将字符串中特定部分以多个字符的形式,存储成列表 def split(self, * ...
- 使用MySQL自身复制来恢复binlog
如果需要恢复的二进制日志较多,较复杂,强烈建议使用MySQL自身复制来恢复binlog,而不要使用mysqlbinlog. 目录 [hide] 1. 如何操作 1.1 将binlog作为relay l ...
- 《深入理解Android 卷III》第八章深入理解Android壁纸
<深入理解Android 卷III>即将公布,作者是张大伟. 此书填补了深入理解Android Framework卷中的一个主要空白,即Android Framework中和UI相关的部分 ...