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

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

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

如果都不能完成则返回-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. 2018.12.14 codeforces 932E. Team Work(组合数学)

    传送门 组合数学套路题. 要求ans=∑i=0nCni∗ik,n≤1e9,k≤5000ans=\sum_{i=0}^n C_n^i*i^k,n\le 1e9,k\le 5000ans=∑i=0n​Cn ...

  2. 2018.11.07 bzoj2751: [HAOI2012]容易题(easy)(组合数学)

    传送门 组合数学一眼题. 感觉一直做这种题智商会降低. 利用组合数学的分步计数原理. 只用关心每个数不被限制的取值的总和然后乘起来就可以了. 对于大部分数都不会被限制,总和都是n(n+1)2\frac ...

  3. Educational Codeforces Round 53 (Rated for Div. 2) E. Segment Sum

    https://codeforces.com/contest/1073/problem/E 题意 求出l到r之间的符合要求的数之和,结果取模998244353 要求:组成数的数位所用的数字种类不超过k ...

  4. mongodb知识积累

    1: 安装mongodb https://www.cnblogs.com/zhangdaicong/p/7492494.html 2:配置文件 vi /etc/mongodb.conf https:/ ...

  5. yyparse() and yylex()

    Yacc 与 Lex 快速入门 yyparse() returns a value of 0 if the input it parses is valid according to the give ...

  6. MySQL/Oracle视图的创建与使用

    1.什么是视图? 视图是一个虚拟的表,是一个表中的数据经过某种筛选后的显示方式,视图由一个预定义的查询select语句组成.   2.视图的特点. 视图中的数据并不属于视图本身,而是属于基本的表,对视 ...

  7. IntellJ IDEA2017 springboot2.0.2 替代@SpringBootApplication方式

    如果不想用@SpringBootApplication,那么可以用@EnableAutoConfiguration 和@ComponentScan替代@SpringBootApplication 详情 ...

  8. SpringMVC(一)helloWorld

    web.xml文件配置如下: <?xml version="1.0" encoding="UTF-8"?> <web-app version= ...

  9. 记录:CSS选择器学习

    常用选择器:标签选择器.类选择器.ID选择器 子选择器(Child selectors) 还有一个比较有用的选择器子选择器,即大于符号(>),用于选择指定标签元素的第一代子元素. .con> ...

  10. MapGis如何实现WebGIS分布式大数据存储的

    作为解决方案厂商,MapGis是如何实现分布式大数据存储的呢? MapGIS在传统关系型空间数据库引擎MapGIS SDE的基础之上,针对地理大数据的特点,构建了MapGIS DataStore分布式 ...