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 ...
随机推荐
- 修改文件夹的所有者为www
切换到root用户:su - root 修改文件所属用户和用户组:chown 用户:用户组 文件名如果用户和用户组是www,那么需要执行命令如下:chown www:www filename -R ( ...
- 简单了解 node http(一)
简单了解 node http 模块 文章记录了对http 模块的简单使用与理解. http 服务端 http 客户端 总结 1. http 服务端 先写个小例子 服务端: let http = req ...
- chrome插件2
转自:http://www.codeceo.com/article/15-chrome-extension.html 1. Web Developer 支持Chrome的Web Developer扩展 ...
- php强大的filter过滤用户输入
<?php $filters = array //定义过滤的数组 ( "name" => array ( "filter"=>FILTER_S ...
- 如何计算java程序运行花了多长时间。加时间戳。
long start = System.currentTimeMillis(); // 记录起始时间 try { Thread.sleep(5000); // 线程睡眠5秒,让运行时间不那么小 } c ...
- Git入门指南九:远程仓库的使用【转】
转自:http://blog.csdn.net/wirelessqa/article/details/20152651 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[-] 十三 ...
- Listview.Finditem()函数用法
查找LISTVIEW控件中指定的字符串. Private Sub ListView2_Click() On Error GoTo ONERROR Dim strFindMe As String ...
- vue打包后element-ui部分样式(图标)异常问题
vue项目使用element-ui组件,打包后部分样式(上下左右箭头)异常,变成方框了. 页面报warn错误,有个字体找不到. 解决办法:在build文件夹下找到utils.js,加上一行public ...
- [工具] BurpSuite--XssValidator插件
0x00 安装 所需软件: 1.burpsuite 2.xssvalidator 源码:https://github.com/nVisium/xssValidator(按照编译指导编译) burpsu ...
- CentOS 7安装MySQL 8——萌新超详细教程
1.配置MySQL 8.0的安装源: sudo rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-1.n ...