leetcode 134 加油站问题
leetcode 134 解析
在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升。
你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] 升。你从其中的一个加油站出发,开始时油箱为空。
如果你可以绕环路行驶一周,则返回出发时加油站的编号,否则返回 -1。
说明:
- 如果题目有解,该答案即为唯一答案。
- 输入数组均为非空数组,且长度相同。
- 输入数组中的元素均为非负数。
自己的答案,基本算是暴力解法了,双重循环,不过考虑实际情况大多数都break掉了,测试时间也还好,C++代码如下:
C++解法一:
class Solution {
public:
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
int re=-,i=,j=;
int l=gas.size();
while(re==-&&i<l){
gas.push_back(gas[i]);//下标每次后移前都把当前下标数目push_back
cost.push_back(cost[i]);
if(gas[i]>=cost[i]){//当前point满足出发条件进入子循环;
int oil=gas[i],m=;
while(m<l-){
if(oil-cost[i+m]>=){
oil=oil-cost[i+m]+gas[i+m+];
m++;
}else{
break;
}
}
if((m==l-)&&(oil-cost[i+m]>=)){
re=i;
break;
}
}
i++;
}
return re;
}
};
最优解:
只遍历一遍,有三个特征量,total,sum,start
start用来记录起点,也就是最终的返回值;
total用来记录整个环的gas和cost,即验证能否满足走完一圈的最低条件;
sum用来记录start到当前点是否满足向前行驶的条件;
时间复杂度只有O(n);
但我总觉得这个程序实际上必须是满足total>0一定有解,这个解就是sum>0的第一个点的下标;至于为什么满足这个条件一定有解,等想明白了再更新;
C++解法二:
class Solution {
public:
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
int total=,sum=,start=;
for(int i=;i<gas.size();i++){
total+=gas[i]-cost[i];//走完整个环的前提
sum+=gas[i]-cost[i];//走完起点到当前点的前提
if(sum<){//当到达某一站点时油不够了,更换next为新起点
start=i+;
sum=;
}
}
return (total<)? - : start;
}
};
leetcode 134 加油站问题的更多相关文章
- Java实现 LeetCode 134 加油站
134. 加油站 在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升. 你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] 升 ...
- [Leetcode]134.加油站
这一题是贪心不是模拟 是贪心不是模拟 是贪心不是模拟! 如果用模拟的做法会比较慢,也失去了做这一题的趣味了. 模拟的方法很简单,就是每一个加油站都做起点模拟一遍,试一下能不能完成一圈,能完成一圈就保存 ...
- LeetCode 134. 加油站(Gas Station)
题目描述 在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升. 你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] 升.你从其 ...
- LeetCode:加油站【134】
LeetCode:加油站[134] 题目描述 在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升. 你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要 ...
- [Leetcode 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]. Y ...
- [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 ...
随机推荐
- 简单告诉你-"border:0"与"border:none"的区别
"border:0"与"border:none"的差异主要体现在性能差异和兼容差异.1.性能差异"border:0"表示把border定义为 ...
- JSP 自定义标签 生命周期
1. 2.
- init, telinit - 进程处理初始化
总览 /sbin/init [ -a ] [ -s ] [ -b ] [ -z xxx ] [ 0123456Ss ] /sbin/telinit [ -t 秒 ] [ 0123456sSQqabcU ...
- pyhton接口自动化测试-requests.post()
一.方法定义 二.post方法简单使用 1.带数据的post 2.带header的post 3.带json的post 4.带参数的post 5.普通文件上传 6.定制化文件上传 7.多文件上传 一.方 ...
- SpringBootMVC02——SpringDataJpa与ThymeLeaf
大纲 - SpringDataJpa进阶使用- SpringDataJpa自定义查询- 整合Servlet.Filter.Listener- 文件上传- Thymeleaf常用标签 1.整合Servl ...
- 数据结构之查找(图片来源,老师PPT)
顺序查找进行遍历元素,进行查找 总计全部比较次数为:1+2+…+n = (1+n)n/2 若求某一个元素的平均查找次数,还应当除以n(等概率), 即: ASL=(1+n)/2 ,时间效率为 O(n) ...
- python基础练习题5
01:输入一行字符,分别统计出其中英文字母,空格,数字和其他字符的个数 import string s = input('input a string:\n') letters =0 space = ...
- PHP三种字符串界定符的区别(单引号,双引号,<<<)
单引号,双引号,<<<的区别如下: 前续:今天突然遇到了<<<EOT,可在运行的时候出错了,所以就度娘了下. 1.单引号:’a string’ \’是唯一的转 ...
- 【HDU5289】Assignment
题目大意:给定一个长度为 N 的序列,求序列中最大值和最小值相差小于 K 的连续段的个数. 题解: 最大值和最小值相差不超过 K 是一个在值域角度的限制,应考虑采用平衡树或权值...数据结构进行维护. ...
- 【10】Python urllib、编码解码、requests、多线程、多进程、unittest初探、__file__、jsonpath
1 urllib urllib是一个标准模块,直接import就可以使用 1.1get请求 from urllib.request import urlopen url='http://www.nnz ...