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 加油站问题的更多相关文章

  1. Java实现 LeetCode 134 加油站

    134. 加油站 在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升. 你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] 升 ...

  2. [Leetcode]134.加油站

    这一题是贪心不是模拟 是贪心不是模拟 是贪心不是模拟! 如果用模拟的做法会比较慢,也失去了做这一题的趣味了. 模拟的方法很简单,就是每一个加油站都做起点模拟一遍,试一下能不能完成一圈,能完成一圈就保存 ...

  3. LeetCode 134. 加油站(Gas Station)

    题目描述 在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升. 你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] 升.你从其 ...

  4. LeetCode:加油站【134】

    LeetCode:加油站[134] 题目描述 在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升. 你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要 ...

  5. [Leetcode 134]汽车加油站 Gas Station (环形)

    [题目] There are N gas stations along a circular route, where the amount of gas at station i is gas[i] ...

  6. [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 ...

  7. [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 ...

  8. 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 ...

  9. leetcode@ [134] Gas station (Dynamic Programming)

    https://leetcode.com/problems/gas-station/ 题目: There are N gas stations along a circular route, wher ...

随机推荐

  1. 【转载】Django自带的注册登陆功能

    1.登陆 知识点: a.auth.authenticate(username=name值, password=password值) 验证用户名和密码 b.auth.login(request, use ...

  2. 生成二维码功能(js前端)

    生成二维码需要引入qrcode.js和jquery.min.js <!DOCTYPE html> <head> <title>二维码</title> & ...

  3. java中的进制转换

    java中的进制转换及转换函数 转自:https://blog.csdn.net/V0218/article/details/74945203 Java的进制转换 进制转换原理 十进制 转 二进制: ...

  4. Laravel 向公共模板赋值

    开发过程中许多时候都会向公共模板赋值,比如顶部导航栏,页面底部等等,不可能在每个控制器中都赋值一遍. Laravel 中解决办法如下:修改 App\Providers\AppServiceProvid ...

  5. SpringBoot01——Framework Introduced and Helloworld

    1.介绍 SpringBoot主要解决的是在微服务的架构下简化配置(有快速配置).前后端分离.快速开发 优点: l 提供了快速启动入门 l 开箱即用.提供默认配置 l 内嵌容器化web项目 l 没有冗 ...

  6. oracle高水位降低法

    1.什么是高水位?(high water mark 简称:HWM)     所有的oracle段(segments,在此,为了理解方便,建议把segment作为表的一个同义词)都有一个在段内存放数据的 ...

  7. Mysterious Crime CodeForces - 1043D (思维+组合数学)

    Acingel is a small town. There was only one doctor here — Miss Ada. She was very friendly and nobody ...

  8. 你可能用到的Spring工具类?

    现在绝大部分项目都已经拥抱Spring生态,掌握Spring常用的工具类,是非常重要,零成本增加编码效率. 一.常用工具类 ObjectUtils org.springframework.util.O ...

  9. Redis数据类型之列表操作

    redis 目录: 1.自动分配(redis) - 批量导入 2.微信自动绑定 3.django的ORM做不了的操作,怎么自定义操作数据库 extra ’ 4.报表 公司每个月销售的业绩 5.权限 = ...

  10. shell之文本过滤(awk)

    shell之文本过滤(awk) 分类: linux shell脚本学习2012-09-19 15:53 1241人阅读 评论(0) 收藏 举报 shell正则表达式脚本任务语言 如果要格式化报文或从一 ...