[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 have a car with an unlimited gas tank and it costs cost[i]
of gas to travel from station i to its next station (i+1). You begin the journey with an empty tank at one of the gas stations.
Return the starting gas station's index if you can travel around the circuit once in the clockwise direction, otherwise return -1.
Note:
- If there exists a solution, it is guaranteed to be unique.
- Both input arrays are non-empty and have the same length.
- Each element in the input arrays is a non-negative integer.
Example 1:
Input:
gas = [1,2,3,4,5]
cost = [3,4,5,1,2] Output: 3 Explanation:
Start at station 3 (index 3) and fill up with 4 unit of gas. Your tank = 0 + 4 = 4
Travel to station 4. Your tank = 4 - 1 + 5 = 8
Travel to station 0. Your tank = 8 - 2 + 1 = 7
Travel to station 1. Your tank = 7 - 3 + 2 = 6
Travel to station 2. Your tank = 6 - 4 + 3 = 5
Travel to station 3. The cost is 5. Your gas is just enough to travel back to station 3.
Therefore, return 3 as the starting index.
题意:
给定一条环形路,上面有N个加油站。每个加油都有到达所需油量和可加油量。求能走完全程的出发站。
思路:
非常经典的一道题。可以转换成求最大连续和做,但是有更简单的方法。基于一个数学定理:
如果一个数组的总和非负,那么一定可以找到一个起始位置,从他开始绕数组一圈,累加和一直都是非负的
(证明貌似不难,以后有时间再补)
有了这个定理,判断到底是否存在这样的解非常容易,只需要把全部的油耗情况计算出来看看是否大于等于0即可。
那么如何求开始位置在哪?
注意到这样一个现象:
1. 假如从位置i开始,i+1,i+2...,一路开过来一路油箱都没有空。说明什么?说明从i到i+1,i+2,...肯定是正积累。
2. 现在突然发现开往位置j时油箱空了。这说明什么?说明从位置i开始没法走完全程(废话)。那么,我们要从位置i+1开始重新尝试吗?不需要!为什么?因为前面已经知道,位置i肯定是正积累,那么,如果从位置i+1开始走更加没法走完全程了,因为没有位置i的正积累了。同理,也不用从i+2,i+3,...开始尝试。所以我们可以放心地从位置j+1开始尝试。
以上转自 https://www.cnblogs.com/boring09/p/4248482.html
代码:
class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
int start = 0; // 起点
int tank = 0; // 当前油量
int deficit = 0; //赤足
for(int i = 0; i< gas.length; i++){
tank = tank + gas[i]-cost[i];
if(tank < 0){
start = i+1;
deficit = deficit + tank;
tank = 0;
}
}
return tank + deficit >=0 ? start : -1 ;
}
}
[leetcode]134. Gas Station加油站的更多相关文章
- [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 ...
- leetcode 134. Gas Station ----- java
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- 134. Gas Station加油站
[抄题]: There are N gas stations along a circular route, where the amount of gas at station i is gas[i ...
- Java for 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 ...
- 134 Gas Station 加油站
在一条环路上有 N 个加油站,其中第 i 个加油站有汽油gas[i].你有一辆油箱容量无限的的汽车,从第 i 个加油站前往第 i+1 个加油站需要消耗汽油 cost[i].你从其中一个加油站出发,开始 ...
- [leetcode] 134. Gas Station (medium)
原题 题意: 过一个循环的加油站,每个加油站可以加一定数量的油,走到下一个加油站需要消耗一定数量的油,判断能否走一圈. 思路: 一开始思路就是遍历一圈,最直接的思路. class Solution { ...
- 134. Gas Station leetcode
134. Gas Station 不会做. 1. 朴素的想法,就是针对每个位置判断一下,然后返回合法的位置,复杂度O(n^2),显然会超时. 把这道题转化一下吧,求哪些加油站不能走完一圈回到自己,要求 ...
随机推荐
- Android sdk 更新后编译不过,【Could not find com.android.sdklib.build.ApkBuilderMain】
最近更新了Android sdk,发现编译不过了 解决方案: 进入 sdk/tool/lib/ 目录下,看看有没有 sdklib.jar 这个文件,如果没有看看有没有sdklib-25.*.*.jar ...
- 转:Android-apt
转自http://blog.csdn.net/zjbpku/article/details/22976291 What is this? The Android-apt plugin assists ...
- input输入框外联式样式控制不了字体
1.问题背景 在做项目过程中,发现input输入框利用外联样式,控制不了输入框的样式 2.问题原因 (1)HTML代码 <!DOCTYPE html> <html> <h ...
- C++可调用对象与函数表
c++的可调用对象 有 函数 函数指针 lambda表达式 bind的对象 重载了函数调用运算符的类 如何调用? 函数调用 void afuncToCall() { cout << &qu ...
- 《DSP using MATLAB》Problem 2.5
2.代码: %% ------------------------------------------------------------------------ %% Output Info abo ...
- Jenkins搭建windows service自动编译发布环境
类库项目(Task)部署 前面搭建了Web站点的环境,类库项目发布不同于站点项目,它只需要将MSBuild编译出来的dll复制到目标服务器上即可,而不需要通过Web Deploy,下面来说一下如何发布 ...
- 【idea】如何彻底卸载idea
卸载MAC中的IDEA Intellij 14 使用命令行: 1 2 3 4 5 6 cd /Applications/ rm -r IntelliJ\ IDEA\ 14.app/ rm -r /Us ...
- OrmLite动态创建表,一个实体类创建多张表的的偏招
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/LonelyRoamer/article/details/26299355 在做一个Android的项 ...
- visual studio内置“iis”组件提取及二次开发
简介 visual studio安装后会自带小型的“iis”服务器,本文就简单提取一下这个组件,自己做一个小型“iis”服务器吧.先来说用途吧(废话可绕过),比如在服务器上没有安装iis,或者给客户演 ...
- TCP和UDP Client 代码
最近学习要求做网络编程,使用从网上找了一些资料,主要是网络协议的分层等通讯,你可以查看英文版的资料:CScharp网络编程英文版 下面直接给出代码吧,我想一看应该就懂. TCP Client 代码: ...