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 ...
随机推荐
- java Web(2)
Servlet与web容器的配合: 1)客户端向Web服务器发起一个HTTP请求. 2)HTTP请求被Web服务器接受,如果请求的是静态页面,则由Web服务器负责处理.如果请求的是Java Web组件 ...
- AI:PR的数学表示-传统方法PR
前言: 接上一篇:AI:模式识别的数学表示 在图像处理PR领域,相对于ANN方法,其他的方法一般称为传统方法.在结构上,几乎所有的PR方法都是可解释的.且任一传统方法,在一定约束下,可以转换为SV近邻 ...
- 使用Reporting Services时遇到的小问题
同事忙不开了交过来的一个小问题,以前我也没有做过Reporting Services相关的. 大概看了一下,是数据集那边的查询代码接收不到报表参数,而且不是单纯地接收不到,具体情况如下.. 命令类型是 ...
- IronPython中共享的C#基类如何向下转型
在项目中,我们使用IronPython来定义工作流脚本来以应对科研多变的需求.项目使用的主要语言仍然是C#,使用C#封装了各种基础服务与基础设施.Python脚本只使用C#提供的服务,或者说只定义了逻 ...
- slf4j日志只输出到控制台,没输出到日志文件
最近使用SLF4J遇到了一个比较头疼的坑,日志输出到控制台没有问题,但是始终没有输出到日志文件.无论怎麽修改日志配置,始终是老样子. 有一种绝望,是各种百度.google却还是解决不了问题..直到我在 ...
- C# for 遍历 IPagedList
IPagedList<Doc> ss = Doclist.ToPagedList(page, pageSize); ;i<ss.Count;i++) { var yy = ss[i] ...
- 【剑指Offer】51、构建乘积数组
题目描述: 给定一个数组A[0,1,...,n-1],请构建一个数组B[0,1,...,n-1]. 其中B中的元素B[i]=A[0] * A[1]... * A[i-1] * A[i+1] ...
- qbxt 考前集训 Day1
立方数(cubic) Time Limit:1000ms Memory Limit:128MB 题目描述 LYK定义了一个数叫“立方数”,若一个数可以被写作是一个正整数的3次方,则这个数就是立方数 ...
- 博弈论题目总结(二)——SG组合游戏及变形
SG函数 为了更一般化博弈问题,我们引入SG函数 SG函数有如下性质: 1.如果某个状态SG函数值为0,则它后继的每个状态SG函数值都不为0 2.如果某个状态SG函数值不为0,则它至少存在一个后继的状 ...
- [luogu4159 SCOI2009] 迷路(矩阵乘法)
传送门 Solution 矩阵乘法新姿势qwq 我们知道当边权为1是我们可以利用矩阵快速幂来方便的求出路径数 那么对于边权很小的时候,我们可以将每个点都拆成若干个点 然后就将边权不为1转化为边权为1了 ...