leetcode@ [134] Gas station (Dynamic Programming)
https://leetcode.com/problems/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.
class Solution {
public:
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
int n = gas.size();
if(n == ) return -;
if(n == ) {
if(gas[] - cost[] >= ) return ;
return -;
}
vector<int> dif; dif.clear();
for(int i=;i<n;++i) dif.push_back(gas[i] - cost[i]);
for(int i=;i<n-;++i) dif.push_back(gas[i] - cost[i]);
vector<int> sum(*n-, );
vector<int> dp(*n-, );
sum[] = dif[];
dp[] = ;
for(int i=;i<*n-;++i) {
if(sum[i-] < ) {
sum[i] = dif[i];
dp[i] = ;
}
else {
sum[i] = sum[i-] + dif[i];
dp[i] = dp[i-] + ;
}
if(dp[i] == n && sum[i] >= ) return i-n+;
}
return -;
}
};
这里正好可以引申一个:环形数组的最大子数组和问题。解决这个问题就是把“环形” 变成 “直线型”。比如: 原来的环形数组为 v[0], v[1], ..., v[n-1]. 可以在v[n-1] 后面继续放置 v[0], v[1], ..., v[n-1]. 然后我们对这个新的数组求解:最大子数组和问题。但是这里也有一个问题,就是求解出来的最大子数组可能长度会超过n,所以我们维护两个变量:sum[i] 和 len[i]。sum[i] 记录以a[i] 为结束位置的最大子数组和,而len[i] 记录以a[i] 为结束位置的最大子数组的长度。最终的答案可以找当len[i] <= n 的时候, sum[i] 最大的值。
int res = INT_MIN;
vector<int> sum(n, );
vector<int> len(n, ); sum[] <- v[]; len[] <- ; for(i<- TO *n) {
if(sum[i-] < ) {
sum[i] <- v[i]
len[i] <-
}
else {
sum[i] <- sum[i-] + v[i]
len[i] <- len[i-] +
}
if(len[i] <= n) res <- max{res, sum[i]);
} return res;
leetcode@ [134] Gas station (Dynamic Programming)的更多相关文章
- [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 ----- 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加油站
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. Y ...
- 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 (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的顺序不能改变,只能通过容器来获得由大到小的顺 ...
- 【LeetCode】Gas Station 解题报告
[LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...
随机推荐
- kafka.network.SocketServer分析
当Kafka启动时,会启动这个SocketServer来接收客户端的连接,处理客户端请求,发送响应. 这个类的注释说明了这个socket server的结构 /** * An NIO socket s ...
- STM32移植UCGUI3.90笔记
在MDK环境下,终于将3.90版本的UCGUI移植到STM32下了,在网上看到的都是例程代码,很少看到有关于在STM32下移植UCGUI的教程方法,为了方便大家,特写此移植方法,大家可以借鉴(有错误之 ...
- Android 二维码扫描与生成
由于源代码比较多,本文不进行讲述,请下载源码. 源码来源于网络,请点击这里下载: http://files.cnblogs.com/wuyou/Android%E4%BA%8C%E7%BB%B4%E7 ...
- Centos系统备份与恢复教程
Linux不像windows,它不限制根用户存取任何东西,因此,你完全可以把一个分区上每一个的文件放入一个TAR文件中. 使用root用户切换到根目录 然后,使用下面的命令备份完整的系统: tar c ...
- QT小技巧(书上没有的)
1. Layout本身不能控制隐藏和显示,但是可以在外面专门套一个Widget,然后控制这个Widget就可以达到相应的效果了. 2. 空目录居然也存在 if (QDir(""). ...
- 面试大总结之二:Java搞定面试中的二叉树题目
package BinaryTreeSummary; import java.util.ArrayList; import java.util.Iterator; import java.util.L ...
- linux目录/术语/英文
1./bin :获得最小的系统可操作性所需要的命令2./boot :内核和加载内核所需的文件3./dev :终端.磁盘.调制解调器等的设备项4./etc :关键的启动文件和配置文件5./home :用 ...
- C#.Net 如何动态加载与卸载程序集(.dll或者.exe)6-----在不卸载程序域的前提下替换程序集文件。
当某个程序集文件被载入AppDomain,该文件在AppDomain.Unload之前是不能被替换和删除的.使用AppDomainSetup的影像复制功能可以实现在不卸载程序的情况下替换或者删除程序集 ...
- 需要保存数据zabbix,不需要保存数据nagios
需要保存数据zabbix,不需要保存数据nagios cacti 有什么好用的基于Web的Linux系统监控开源工具(网管系统) 要求类似于Ubuntu的Landscape,可以记录下历史CPU数值. ...
- awesome cpp
https://github.com/fffaraz/awesome-cpp Awesome C/C++ A curated list of awesome C/C++ frameworks, lib ...