LeetCode OJ:Gas Station(加油站问题)
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.
加油站问题,每个加油站可以加的油给出来,从当前当下一个加油站会消耗的汽油量给出来了,求从哪个站点出发可以循环加油站一圈。
一开始是用一个二重循环的,这样复杂度为N^2,一直TLE,代码如下:
class Solution {
public:
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
for(int i = ; i < gas.size(); ++i){
int j = i;
int curGas = gas[j];
while(curGas >= cost[j]){
curGas -= cost[j];
j = (j+)%gas.size();
curGas += gas[j];
if(j == i)
return i;
}
}
return -;
}
};
那只能使用其他方法了,可以看出维护一个部分差的和,如果前面的部分差的和一旦小于0的话,那么可以肯定的是应该在当前节点的下一处开始,然后在维护一个整体的和,当检查部分和可以完成时,查看整体差值是否小于0就可以了,代码如下所示:
class Solution {
public:
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
int totalLeft = ;
int sum = ;
int j = -;
for(int i = ; i < gas.size(); ++i){
totalLeft += gas[i] - cost[i];
sum += gas[i] - cost[i];
if(sum < ){
j = i;
sum = ;
}
}
if(totalLeft < )
return -;
return j + ;
}
};
LeetCode OJ:Gas Station(加油站问题)的更多相关文章
- [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 ...
- [LeetCode OJ] Gas Station
问题描述: There are N gas stations along a circular route, where the amount of gas at station i is gas[i ...
- 【LeetCode】Gas Station 解题报告
[LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...
- [LeetCode] 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
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】Gas Station
Gas Station There are N gas stations along a circular route, where the amount of gas at station i is ...
- 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 _ Gas Station
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
随机推荐
- java中数组以及集合
java中数组: 数组在Java里是一种特殊类型,有别于普通的“类的实例”的对象.但实际数组也是一种对象类型,int[]a = new int[5] a是在java栈中分配的引用变量,类型是int[ ...
- 在DLL编程中,导出函数为什么需要extern "C"
转自:http://blog.csdn.net/zhongjling/article/details/8088664 一般来讲,在DLL编程过程中,对于导出的函数前 都需要加入 extern “C”, ...
- CSS 图片廊
CSS 图片廊 一.示例一 代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8" ...
- 20145216史婧瑶《Java程序设计》第一次实验报告
实验一 Java开发环境的熟悉(Linux + Eclipse) 实验内容 1.使用JDK编译.运行简单的Java程序: 2.使用Eclipse 编辑.编译.运行.调试Java程序. 实验要求 1.没 ...
- 20145310 《Java程序设计》第2周学习总结
20145310 <Java程序设计>第2周学习总结 教材学习内容总结 第三章主要学习了java的基础语法. java中的基本类型主要可区分为,整数.字节.浮点数.字符与布尔.整数shor ...
- 20145312 《Java程序设计》第九周学习总结
20145312 <Java程序设计>第九周学习总结 学习笔记 Chapter 16整合数据库 16.1 JDBC入门 16.1.1 JDBC简介 SUN公司为了简化.统一对数据库的操作, ...
- Xcode8编辑代码崩溃解决办法
更新了Xcode8带来了一系列问题,最大的困扰就是不支持插件了,而且最关键的是一敲代码就崩溃(就是写一个字母就开始崩),在网上找了很多解决,发现是之前装的插件遗留下来的问题,将插件全部删掉就解决了,下 ...
- CentOS 7 SSH远程证书登陆
SSH远程证书登陆是使用"公私钥"认证的方式来进行SSH登录. 1.创建公私钥 创建方式有很多种,比如说通用ssh连接工具创建,然后把公钥上传到Server主机对应的用户目录下: ...
- UVA 12063 Zeros and Ones(三维dp)
题意:给你n.k,问你有多少个n为二进制的数(无前导零)的0与1一样多,且是k的倍数 题解:对于每个k都计算一次dp,dp[i][j][kk][l]表示i位有j个1模k等于kk且第一位为l(0/1) ...
- url拼接
在做网页抓取的时候经常会遇到一个问题就是页面中的链接是相对链接,这个时候就需要对链接进行url拼接,才能得到绝对链接. url严格按照一定的格式构成,一般为如下5个字段: 详细可参考RFC:http: ...