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. centos python environment

    3. 在Centos7的docker里装好了httpd,运行报错: $ systemctl start httpd.service Failed to get D-Bus connection: Op ...

  2. python 3 :list

    2 List List中支持混合类型 number_list=[1,2,3,4,5] str_list = ["a","b","c",&qu ...

  3. Java Blob类型和String类型相互转换

    1.String 转 Blob: String content = "Hello World!"; Blob blob = Hibernate.createBlob(content ...

  4. 关于hstack和Svstack

    关于hstack和Svstack import numpy as np>>> a = np.array((1,2,3))>>> aarray([1, 2, 3])& ...

  5. JSP 自定义标签 生命周期

    1. 2.

  6. C语言面试相关知识点

    1.关键字static的作用是什么? 有三个明显的作用: 1)在函数体内,一个被声明为静态的变量在这个函数被调用过程中维持其值不变 2)在模块内(但在函数体外),静态的变量可以被模块内所有函数访问,但 ...

  7. 【vue】canvas验证码组件--数字/数字加字母

    基于canvas的数字/数字+字符验证码   SIdentify.vue 组件 <!-- 基于canvas的数字/数字+字符验证码 --> <!-- 调用格式 <s-ident ...

  8. tensorflow 屏蔽 Log

    pip install alfred-py 在代码中加入 from alfred.dl.tf.common import mute_tf mute_tf()

  9. Django学习系列6:使用selenium测试用户交互

    学习系列5中的单元测试有报错信息,这儿来编写functional_tests.py文件,扩充其中的功能测试 # File: functional_test.py # Author: Rxf # Cre ...

  10. etl-p

    java excel 导入数据库 上传文件包  解压导入excel包 导入mysql