[Leetcode]134.加油站
这一题是贪心不是模拟 是贪心不是模拟 是贪心不是模拟!
如果用模拟的做法会比较慢,也失去了做这一题的趣味了。
模拟的方法很简单,就是每一个加油站都做起点模拟一遍,试一下能不能完成一圈,能完成一圈就保存答案,不能完成的就往下一个找
如果都不能完成则返回-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.加油站的更多相关文章
- leetcode 134 加油站问题
leetcode 134 解析 在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升. 你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 co ...
- Java实现 LeetCode 134 加油站
134. 加油站 在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升. 你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] 升 ...
- LeetCode 134. 加油站(Gas Station)
题目描述 在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升. 你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] 升.你从其 ...
- LeetCode:加油站【134】
LeetCode:加油站[134] 题目描述 在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升. 你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要 ...
- [Leetcode 134]汽车加油站 Gas Station (环形)
[题目] There are N gas stations along a circular route, where the amount of gas at station i is gas[i] ...
- [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 ...
- [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 ...
- 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 ...
- leetcode@ [134] Gas station (Dynamic Programming)
https://leetcode.com/problems/gas-station/ 题目: There are N gas stations along a circular route, wher ...
随机推荐
- 2018.11.01 NOIP训练 梭哈(模拟)
传送门 这题貌似不考智商啊. 直接按题意写就可以了. 事实上把牌从小到大排序之后写起来很舒服的. 然后就是有些地方可以人脑减代码量和判断次数. (提示:满堂红和某几种同类型的牌的大小判断) 然后注意A ...
- shell 常见面试
1.求100以内的质数 #!/bin/bash n= ;i<=n;i++)) do ;x<=i;x++)) do b=$(( $i%$x )) ]]; then a=$a+ fi done ...
- mybatis xml中的大于、小于等符号写法
xml特殊符号转义写法 < < > > <> <> & & ' ...
- powerdesiginer 生成oracle脚本问题,一步解决
select * from tablename时, 总是提示错误:table or view does not exits 但是在user_tables中却可以看到刚建立的表. 原因是powerDes ...
- Spring MVC 指导文档解读(二)
Special Bean Types In the WebApplicationContext 解读 1.WebApplicationContext 特有的几种 Bean Types 2. 也表明 与 ...
- Docker网络简介
Docker允许通过外部访问容器或则容器互联的方式来提供网络服务. 外部访问容器 容器中可以运行一些网络应用,要让外部也可以访问这些应用,可以通过-P或则-P参数来指定断开映射.当使用 -P 标记时, ...
- (转)什么是.NET?什么是CLI?什么是CLR?IL是什么?JIT是什么,它是如何工作的?GC是什么,简述一下GC的工作方式?
转自:http://www.cnblogs.com/haofaner/articles/2288968.html 1:什么是.NET? NET 是 Microsoft 的用以创建 XML Web 服务 ...
- War Chess (hdu 3345)
http://acm.hdu.edu.cn/showproblem.php?pid=3345 Problem Description War chess is hh's favorite game:I ...
- 实用shell命令100条
1,echo "aa" > test.txt 和 echo "bb" >> test.txt //>将原文件清空,并且内容写入到文件中, ...
- OpenGL中的渐变颜色绘图(应力可视化)
#include <GL/glut.h> #include <iostream> #include <cmath> using namespace std; ; ; ...