[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, otherwise return -1.
Note:
The solution is guaranteed to be unique.
问题:在一个环形的路径上,有 n 个加油站。每个加油站 i 有汽油 gas[i] ,从 i 行驶到下一个加油站需要耗油 cost[i],汽车的油箱没有限制。找出一个加油站,从该加油站能走完整个环形路径。其中,本题目只有唯一一个解。
思路:
环形路径并且需要走完一整圈,则有:
1),若全部加油站的汽油 小于 路径总消耗油量,则不可能走完全程。
2),反之,若全部加油站的汽油 大于等于 路径总消耗油量,那么必然有至少存在一个站可以出发走完全程。
以上是我能想到的,至于如何环形路径中找到 2) 中的加油站,则没有什么思路。
在网上看到一个讲解,才明白,而解决方案的关键就是题目的限制条件“本题目只有唯一一个解”。
由于只有唯一解 i ,则以下性质:
性质1. 任何 j ( 0<= j < i) 都不可能到达 i 。
性质2. 任何 k (i <= i < n) ,i 都可以到达。
所以,从左往右扫,当遇到第一个i, 满足 sum[i,j] (i <= j < n) 均大于等于0,那么 i 便是题目的解。
若对每一个元素都检查 sum[i,j] ,那么耗时为 O(n*n) 。这里借助 性质1 优化查找。
假设 sum[i1, j] (i1<= j < t2) 都大于等于 0 ,当 j = t2 时,sum[i1, j] 小于 0 ,那么 i1 不是解,根据性质1,i1~ t2 都不是解。可以从 t2+1 开始继续扫。
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
vector<int> rem(gas.size());
int totalrem = ;
for ( int i = ; i < gas.size(); i ++){
rem[i] = gas[i] - cost[i];
totalrem += rem[i];
}
if (totalrem < ) {
return -;
}
int l = ;
int sum = ;
for (int i = ; i < rem.size(); i++) {
sum += rem[i];
if(sum < ){
sum = ;
l = i + ;
}
}
return l;
}
记录以下:最近做的几道 Greedy 类型的题目,自己基本没有很好的解题思路,即使看了几个讲解也没有找到他们的共通之处。看来自己还没有很好掌握 Greedy 的求解。
本题思路参考 [LeetCode] Gas Station, 喜刷刷.
[LeetCode] 134. Gas Station 解题思路的更多相关文章
- 【LeetCode】Gas Station 解题报告
[LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...
- 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 ...
- 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加油站
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 ----- java
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 (medium)
原题 题意: 过一个循环的加油站,每个加油站可以加一定数量的油,走到下一个加油站需要消耗一定数量的油,判断能否走一圈. 思路: 一开始思路就是遍历一圈,最直接的思路. class Solution { ...
- 134. Gas Station leetcode
134. Gas Station 不会做. 1. 朴素的想法,就是针对每个位置判断一下,然后返回合法的位置,复杂度O(n^2),显然会超时. 把这道题转化一下吧,求哪些加油站不能走完一圈回到自己,要求 ...
- 贪心:leetcode 870. Advantage Shuffle、134. Gas Station、452. Minimum Number of Arrows to Burst Balloons、316. Remove Duplicate Letters
870. Advantage Shuffle 思路:A数组的最大值大于B的最大值,就拿这个A跟B比较:如果不大于,就拿最小值跟B比较 A可以改变顺序,但B的顺序不能改变,只能通过容器来获得由大到小的顺 ...
随机推荐
- 你的第一个Windows程序——绘制窗口
MSDN原文(英文) 绘制窗口 你已经创建了你的窗口,现在你想在它里面显示东西.在WIndows术语里,这就是所谓的绘制窗口.混合隐喻,一个窗口是一个空白画布,等待你去填充它. 有时你的程序将启动绘制 ...
- [Angular 2] ngrx/store
@ngrx/store builds on the concepts made popular by Redux and supercharges it with the backing of RxJ ...
- java设计模式---享元模式
享元模式 顾名思义:共享元对象.如果在一个系统中存在多个相同的对象,那么只需要共享一份对象的拷贝,而不必为每一次使用创建新的对象. 享元模式是为数不多的.只为提升系统性能而生的设计模式.它的主要作用就 ...
- 监听视图树 OnGlobalLayoutListener
背景 我们都知道在onCreate()里面获取控件的高度是0,这是为什么呢?我们来看一下示例: 首先我们写一个控件 public class MyImageView extends ImageView ...
- 选课 树形dp+路径输出
#include<iostream> #include<cstdio> #include<cstring> #define maxn 2010 using name ...
- 【转】Multithreaded Python Tutorial with the “Threadworms” Demo
The code for this tutorial can be downloaded here: threadworms.py or from GitHub. This code works wi ...
- (转)DevExpress GridView属性设置
GirdControl是数据的容器,它包含多种显示方式,GridView则是一种二维表格视图. 绑定数据源: List<Student> list = new List<Studen ...
- [译]一个灵活的 Trello 敏捷工作流
[译]一个灵活的 Trello 敏捷工作流 翻译自 An Agile Trello Workflow That Keeps Tasks Flexible Getting things done 可不只 ...
- 武汉科技大学ACM:1010: 电话号码
Problem Description LXD打算换个手机号码,但是他去营业厅选号码的时候却把移动的客服小姐烦得不行,因为他太挑三捡四啦.对于一个手机号的后六位数字(前面五位他就无所谓了),LXD有很 ...
- Java语言实现简单FTP软件------>FTP软件远程窗口的实现(六)
1.首先看一下远程窗口的布局效果 2.看一下本地窗口实现的代码框架 3.远程窗口主要实现代码FtpPanel.java package com.oyp.ftp.panel.ftp; import ja ...