【LeetCode】加油站
【问题】在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升。
你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] 升。你从其中的一个加油站出发,开始时油箱为空。
如果你可以绕环路行驶一周,则返回出发时加油站的编号,否则返回 -1。
说明:
如果题目有解,该答案即为唯一答案。
输入数组均为非空数组,且长度相同。
输入数组中的元素均为非负数。
输入:
gas = [,,,,]
cost = [,,,,]
输出:
解释:
从 号加油站(索引为 处)出发,可获得 升汽油。此时油箱有 = + = 升汽油
开往 号加油站,此时油箱有 - + = 升汽油
开往 号加油站,此时油箱有 - + = 升汽油
开往 号加油站,此时油箱有 - + = 升汽油
开往 号加油站,此时油箱有 - + = 升汽油
开往 号加油站,你需要消耗 升汽油,正好足够你返回到 号加油站。
因此, 可为起始索引。
【思路】首先我们要分析这个起点会不会存在?这个其实很简单,如果将所有的gas总油量 >= 车子的cost总耗油量,从而车子可以走一周的。
我们分析一下,假设起始点为x, 如果总的gas-总的cost大于零时,则我们将当前路径从其实路径x一分为二,左侧的部分剩余油量必定 <= 0,而右侧的部分剩余油量必定 >= 0. 这样才可以使得车子可以完成一周的路程!
class Solution {
public:
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
int total = , cur = , start = ;
for(int i = ; i < gas.size(); i++){
total += (gas[i] - cost[i]);
cur += (gas[i] - cost[i]);
if(cur < ){
start = i + ;
cur = ;
}
}
return (total >= ) ? start : -;
}
};
【LeetCode】加油站的更多相关文章
- [LeetCode] Minimize Max Distance to Gas Station 最小化去加油站的最大距离
On a horizontal number line, we have gas stations at positions stations[0], stations[1], ..., statio ...
- LeetCode:加油站【134】
LeetCode:加油站[134] 题目描述 在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升. 你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要 ...
- [LeetCode] 871. Minimum Number of Refueling Stops 最少的加油站个数
A car travels from a starting position to a destination which is target miles east of the starting p ...
- leetcode 134 加油站问题
leetcode 134 解析 在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升. 你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 co ...
- [LeetCode] Gas Station 加油站问题
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- 【leet-code】135. 加油站
题目描述 在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升. 你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[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] ...
- [Leetcode]134.加油站
这一题是贪心不是模拟 是贪心不是模拟 是贪心不是模拟! 如果用模拟的做法会比较慢,也失去了做这一题的趣味了. 模拟的方法很简单,就是每一个加油站都做起点模拟一遍,试一下能不能完成一圈,能完成一圈就保存 ...
- [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 OJ:Gas Station(加油站问题)
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
随机推荐
- vector的使用-Hdu 4841
圆桌问题 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submis ...
- JavaScript - 包装类型
1. 概念 基本类型是没有方法的 包装类型作用是为了方便操作基本数据类型 // 下面代码的问题? // s1是基本类型,基本类型是没有方法的 var s1 = 'zhangsan'; var s2 = ...
- 了解js闭包
定义:闭包是由函数以及创建该函数的环境组合而成.这个环境包含了这个闭包创建时所能访问的所有局部变量 理解:能够读取其他函数的内部变量的函数,并开辟新的作用域(环境) 例子1 function pare ...
- 1003 Emergency (25分) 求最短路径的数量
1003 Emergency (25分) As an emergency rescue team leader of a city, you are given a special map of ...
- js运动框架及应用
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- Spark教程——(11)Spark程序local模式执行、cluster模式执行以及Oozie/Hue执行的设置方式
本地执行Spark SQL程序: package com.fc //import common.util.{phoenixConnectMode, timeUtil} import org.apach ...
- Activity切换动画。从右边滑入,关闭时从左边滑入
直接贴代码吧 1. 动画文件(两个动画文件配置到res/anim目录下) activity_anim_in_right.xml <?xml version="1.0" e ...
- Windows API—CreateEvent—创建事件
事件是一个允许一个线程在某种情况发生时,唤醒另外一个线程的同步对象.事件告诉线程何时去执行某一给定的任务,从而使多个线程流平滑,CreateEvent是创建windows事件的意思,作用主要用在判断线 ...
- 吴裕雄--天生自然HADOOP操作实验学习笔记:pig简介
实验目的 了解pig的该概念和原理 了解pig的思想和用途 了解pig与hadoop的关系 实验原理 1.Pig 相比Java的MapReduce API,Pig为大型数据集的处理提供了更高层次的抽象 ...
- JavaScript中的变量定义和声明
变量声明旨在分配内存,定义为这个分配的内存分配一个值.