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. Malloc与Free不调用构造函数与析构函数

    例子: #include "stdafx.h" #include <new> #include <iostream> using namespace std ...

  2. linux命令详解——tee

    tee 重定向输出到多个文件 在执行Linux命令时,我们既想把输出保存到文件中,又想在屏幕上看到输出内容,就可以使用tee命令 要注意的是:在使用管道线时,前一个命令的标准错误输出不会被tee读取. ...

  3. Delphi 标识符

  4. JavaScript运算符及语句

    ECMAScript 算术运算符 加,减,乘,除,-号可以表示负号 递增(++),递减(--) 两种写法:例:i++,i--,++i,--i,区别是运算符放在前面是先计算后输出,运算符放在后面先输出再 ...

  5. opencv-python用原图和mask实现抠图

    1.先上图 原图:test1.png mask图:test-mask.png 结果图:mask.png 2.代码部分 import cv2 from PIL import Image import n ...

  6. jquery在线引用地址大全 全部来自官网

    谷歌的就算了,容易被屏蔽,下面都是官方原版的 最新版本 <script src="http://code.jquery.com/jquery-latest.js">&l ...

  7. Django学习系列4:编写第一个简单的应用代码

    首页视图编写 lists/tests.py from django.test import TestCasefrom django.urls import resolvefrom lists.view ...

  8. ubuntu 配置smb后无法访问

    配置如下 [/gscloud] path = /gscloud browseable = yes writable = yes guest ok = yes read only = no create ...

  9. 上传图片,正在加载,loading

    https://blog.csdn.net/yansong_8686/article/details/50361573

  10. react-native window下创建Hello(解决创建一路的坑)

    今天真的颇为激动,1年没有玩RN,竟然被最新的RN版本0.55.4创建Hello折腾了半天,想当年刚玩RN创建环境用了3天, 想想现在也是不容易啊半天就搞定了,目测以后创建的话也就1-2个小时就搞定了 ...