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)的更多相关文章

  1. [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 ...

  2. 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 ...

  3. 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 ...

  4. [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 ...

  5. 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 ...

  6. [leetcode] 134. Gas Station (medium)

    原题 题意: 过一个循环的加油站,每个加油站可以加一定数量的油,走到下一个加油站需要消耗一定数量的油,判断能否走一圈. 思路: 一开始思路就是遍历一圈,最直接的思路. class Solution { ...

  7. 134. Gas Station leetcode

    134. Gas Station 不会做. 1. 朴素的想法,就是针对每个位置判断一下,然后返回合法的位置,复杂度O(n^2),显然会超时. 把这道题转化一下吧,求哪些加油站不能走完一圈回到自己,要求 ...

  8. 贪心: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的顺序不能改变,只能通过容器来获得由大到小的顺 ...

  9. 【LeetCode】Gas Station 解题报告

    [LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...

随机推荐

  1. python参考手册--第8章

    1.模块和import (1)首次import module会做以下3件事: a)创建新的命名空间,用作在该源文件中定义的所有对象的容器.在模块中定义的函数和方法在使用global语句时将访问该命名空 ...

  2. 使用rsyslog+loganalzey收集日志显示客户端ip

    http://www.ituring.com.cn/article/128536 rsyslog localhost 转发 http://bigsec.net/one/tool/rsyslog.htm ...

  3. tomcat 禁止某些文件(夹)的访问

    tomcat 禁止某些文件(夹)的访问 <!-- 不允许访问的文件以及文件夹 --> <security-constraint> <display-name>Tom ...

  4. MysqlHelper类

    连接方式:server=localhost;port=3306;userid=root;password=123456789;database=mysql;persist security info= ...

  5. http://blog.csdn.net/majian_1987/article/details/44939911

    http://blog.csdn.net/majian_1987/article/details/44939911

  6. Android开发之ListView-ArrayAdapter的使用

    ArrayAdapter: ArrayAdapter<String>(Context context, int resource, int textViewResourceId, Stri ...

  7. bzoj2456

    有趣的题目 空间1mb,所以开数组的算法就不要想了(我一开始没看到……) 仔细读题,然后发现这里他限定众数为出现超过n div 2次 也就是说,这个数可以对应每一个不相同的数消掉,最终还剩下这个数 也 ...

  8. bzoj1789 AHOI 维护数列(线段树)

    首先想到线段树,然后刚开始写忽然想到树状数组求和岂不是更快,而且编程复杂度又小,于是把之前写的删掉,写树状数组,写完模版之后忽然发现这题竟然是区间修改! 于是又删掉重写,忽然发现不会处理又加又乘的,果 ...

  9. Hibernate事务与并发问题处理(乐观锁与悲观锁)

    目录 一.数据库事务的定义 二.数据库事务并发可能带来的问题 三.数据库事务隔离级别 四.使用Hibernate设置数据库隔离级别 五.使用悲观锁解决事务并发问题 六.使用乐观锁解决事务并发问题 Hi ...

  10. XUtils框架的学习(一)

    一  xutils框架引入到AndroidStudio工程,最简单的方法:① 在APP的build.gradle里面加入 compile 'org.xutils:xutils:3.3.36'.② 一定 ...