这一题是贪心不是模拟 是贪心不是模拟 是贪心不是模拟!

如果用模拟的做法会比较慢,也失去了做这一题的趣味了。

模拟的方法很简单,就是每一个加油站都做起点模拟一遍,试一下能不能完成一圈,能完成一圈就保存答案,不能完成的就往下一个找

如果都不能完成则返回-1

贪心的做法非常的巧妙,整个循环数组如下性质。

对于一个循环数组,如果这个数组整体和 >= 0,那么必然可以在数组中找到这么一个元素:从这个数组元素出发,绕数组一圈,能保证累加和一直是出于非负状态。

这个需要用集合的方法证明:

循环数组分成两个必存在一个区间二分,使得{区间1的和}>0 且{区间1的和}>=abs{区间2的和}

我们只要从这个找到的区间1的起始位置出发,必然能累加和处于非负状态。

注释中有讲解如何处理一些细节的问题。

class Solution {
public:
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
int n=gas.size();
int j=-;
int sum=,total=;
for(int i=;i<n;i++){
sum+=gas[i]-cost[i];
total+=gas[i]-cost[i];
if(sum<){
/*说明之前的油不能支撑到这个加油站,之前的加油站都不能作为起点*/
sum=;
/*重置为0*/
j=i;
/*将此加油站后一点设为起点再寻找*/
}
}
/*先判断total是否小于0,小于0说明找不到这个起始点*/
if(total<)return -;
else return j+;
}
};

[Leetcode]134.加油站的更多相关文章

  1. leetcode 134 加油站问题

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

  2. Java实现 LeetCode 134 加油站

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

  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. Java核心技术之基础知识

    一.类型转换 数值类型之间的转换 强制类型转换 a)       将一个数值强制转换成另一种类型时,如果超出目标类型的便是范围,结果就会截断成一个完全不同的值.(如:(byte)300的实际值为44) ...

  2. vue.js实战(文摘)

    ---------------第1篇 基础篇 第1章 初始vue.js 第2章 数据绑定和第一个vue应用 第3章 计算属性 第4章 v-bind及class与style绑定 第5章 内置命令 第6章 ...

  3. 下载安装mysql的一些坑

    在mysql下载安装的过程中会有一些坑,另外navicat连接mysql数据库时也存在一定的坑,总结如下: 1.计算机中丢失某个dll文件 这个问题好解决,下载个文件就搞定了,下载地址:https:/ ...

  4. (8)What makes a good life? Lessons from the longest study on happiness

    https://www.ted.com/talks/robert_waldinger_what_makes_a_good_life_lessons_from_the_longest_study_on_ ...

  5. c# 文件笔记

    1.文件属性操作 File类与FileInfo都能实现.静态方法与实例化方法的区别! //use File class Console.WriteLine(File.GetAttributes(fil ...

  6. mysql学习之路_sql

    查看数据库: Show databases; 查看指定部分数据库:模糊查询 Show databases like ‘patten’;--paatten是匹配模式 %:表示是匹配模式 _:表示匹配单个 ...

  7. SAX vs. DOM (Event vs. Tree)

    http://www.saxproject.org/event.html Events vs. Trees(大XML文档用SAX) There are two major types of XML ( ...

  8. The file left unchanged.

    This files have types: *.___jb_old___ and *.___jb_bak___ The file left unchanged. You can disable &q ...

  9. C语言中:static与extern对变量和函数的作用

    1.两者对全局变量 static对全局变量,表示定义一个内部变量 extern对全局变量,表示声明一个外部变量 说明: 1.内部变量:定义的变量只能在本文件中访问,不能被其他文件访问. 2.不同文件中 ...

  10. 验证手机格式的js代码

    function isMobil(s)         {             var patrn = /(^0{0,1}1[3|4|5|6|7|8|9][0-9]{9}$)/;          ...