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 ...
随机推荐
- uva 108
降维 枚举行累加 然后求单行上最大连续和 #include <iostream> #include <cstring> #include <cstdio> # ...
- sqlmap动态sql优化,避免传参失误批量修改和删除操作!
分析以下的sqlmap存在问题: <delete id="deletePartspic" parameterClass="TblSpPartspic"&g ...
- Maven Source jar
http://blog.csdn.net/symgdwyh/article/details/4407945
- mysql-5.7.10-winx64 安装时遇到的问题
1.修改密码# /etc/init.d/mysqld stop # mysqld_safe --user=mysql --skip-grant-tables --skip-networking &am ...
- JavaWeb学习总结(四十八)——模拟Servlet3.0使用注解的方式配置Servlet
一.Servlet的传统配置方式 在JavaWeb开发中, 每次编写一个Servlet都需要在web.xml文件中进行配置,如下所示: 1 <servlet> 2 <servlet- ...
- Altium designer中级篇-名称决定多边形连接样式
在工作中积累了诸多小技巧,可以让工作变的更简单,就比如这个多边形铺铜,与大部分规则的不同之处在于,通过更改多边形的名称,就能达到控制多边形规则的效果.这样多边形铺铜变的及其灵活,下面将对这个经验做一个 ...
- Android:常见错误提示
记录开发中常出现的错误 1.遇到这样的错误时,应该立马想到是书写错误或语法错误,常见为android:name写成了name Attribute is missing the Android name ...
- 173. Binary Search Tree Iterator
题目: Implement an iterator over a binary search tree (BST). Your iterator will be initialized with th ...
- Android 自定义对话框使用静态Handler传递参数
JsdMainDialog.java package com.jsd.demo; import android.app.Activity; import android.content.Context ...
- JavaScript DOM高级程序设计1.2-循序最佳实践--我要坚持到底!
我这人,最大的毛病就是浮躁. 下面开始我再一次的学习之旅,希望我能坚持到最后.记笔记除了分享以外,更重要的是让自己看见自己学习之路. 先把ADS库贴出来http://vdisk.weibo.com/s ...