134. Gas Station (Array; DP)
There are N gas stations along a circular route, where the amount of gas at station i is gas[i].
You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journey with an empty tank at one of the gas stations.
Return the starting gas station's index if you can travel around the circuit once, otherwise return -1.
Note:
The solution is guaranteed to be unique.
思路:
1. 是否能travel around? =>只要gas[]加总的和 - cost[]加总的和 > 0,就能够。=>需要一个INT计算gas[]-cost[]的总合
2. 如何找到起点?如果到了某一站油不够,那么说明之前节点作为起点不行,起点必须从它后面开始重新找。=>需要一个INT记录从找到的起点到目前为止的油量
时间复杂度O(n)
class Solution {
public:
int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
int sum = , total = , len = gas.size(), index = -;
for(int i=; i<len; i++){
sum += gas[i]-cost[i];
total += gas[i]-cost[i];
if(sum < ){
index = i;
sum = ;
}
}
return total>= ? index+ : -;
}
};
134. Gas Station (Array; DP)的更多相关文章
- 134. Gas Station leetcode
134. Gas Station 不会做. 1. 朴素的想法,就是针对每个位置判断一下,然后返回合法的位置,复杂度O(n^2),显然会超时. 把这道题转化一下吧,求哪些加油站不能走完一圈回到自己,要求 ...
- 贪心: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 ----- 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 (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 ...
- 134. Gas Station
题目: There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. ...
- [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
Problem: There are N gas stations along a circular route, where the amount of gas at station i is ga ...
- 134. Gas Station加油站
[抄题]: There are N gas stations along a circular route, where the amount of gas at station i is gas[i ...
随机推荐
- [UE4]C++实现动态加载UObject:StaticLoadObject();以Texture和Material为例
相关内容: C++实现动态加载的问题:LoadClass<T>()和LoadObject<T>() http://aigo.iteye.com/blog/2281558C++静 ...
- spark1.0属性配置以及spark-submit简单使用
在spark1.0中属性支持三种配置方式: 1.代码 在代码中构造SparkConf时指定master.appname或者key-value等 val conf = new SparkConf(); ...
- KDD 2018 | 最佳论文:首个面向Facebook、arXiv网络图类的对抗攻击研究
8 月 19 日至 23 日,数据挖掘顶会 KDD 2018 在英国伦敦举行,昨日大会公布了最佳论文等奖项.最佳论文来自慕尼黑工业大学的研究者,他们提出了针对图深度学习模型的对抗攻击方法,是首个在属性 ...
- LaTeX 公式字体大小设置
字体大小: 七号 5.25pt 1.845mm \tiny六号 7.875pt 2.768mm \scriptsize小五号 9pt 3.163mm \footnotesize五号 10.5p ...
- ASP.NET CMS: Administration Template
ASP.NET CMS: Administration Template For many creating advanced ASP.NET website or application admin ...
- oracle11gr2笔记(一)
一,使用scoot用户被锁.解决办法:(http://ciiiso.blog.51cto.com/8779682/1432869/) 二,使用root用户登录系统无法sqlplus,提示说permis ...
- leetcode976
public class Solution { public int LargestPerimeter(int[] A) { var list = A.OrderByDescending(x => ...
- linux 安装svn服务器
一.下载 http://subversion.tigris.org/downloads/subversion-1.6.1.tar.gz http://subversion.tigris.org/dow ...
- iTunes 错误 -50
iTunes,给苹果安装软件,这个软件的体验这么差!!! 手机上基本打不开AppStore,用电脑iTunes,经常莫名其妙的错误代码冒出. 速度奇慢无比. error -50 打开iTunes -- ...
- eclipse 和 javaClass
eclipse 如果设置为 Build automaticaly 会自动对当前的类进行编译,放在项目下的bin文件夹下. 1. 如果此Class有错,则编译后的Class不能用,里面仅仅写会抛出异常代 ...