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 ...
随机推荐
- PHP截取字符串,获取长度,获取字符位置的函数
strstr(string,string) = strchr(,) //从前面第一次出现某个字符串的地方截取到最后strrchr(string,string) //从某个字符串从最后出现的位置截取到结 ...
- 软考类----编码、ASII码等
淘米2014实习生笔试,今年是淘米第一年招暑期实习生,笔试好大部分考的是软考的题目啊啊啊啊(劳资后悔当年没考软考刷加权),其他是浅而泛的风格,C++,SQL语句,数据结构(哈夫曼树,二叉查找树,栈后缀 ...
- POJ2187Beauty Contest
http://poj.org/problem?id=2187 题意 :有一个农场有N个房子,问最远的房子相距多少距离 . 思路 :凸包,旋转卡壳,通过寻找所有的对锺点,找出最远的点对. #includ ...
- iOS中关于KVC与KVO知识点
iOS中关于KVC与KVO知识点 iOS中关于KVC与KVO知识点 一.简介 KVC/KVO是观察者模式的一种实现,在Cocoa中是以被万物之源NSObject类实现的NSKeyValueCodin ...
- 使用头文件climits中的符号常量获知整型数据的表数范围---gyy整理
在头文件climits(limits.h)以宏定义的方式定义了各种符号常量来表示各种整型类型表示数的范围,如int的最大最小值,long的最大最小值等. 符号常量 表示 CHAR_BIT char 的 ...
- 轻量级Java_EE企业应用实战-第5章Hibernate的基本用法-001
1. package org.crazyit.app.domain; import javax.persistence.*; /** * Description: <br/> * ��վ: ...
- 新的HTTP框架:Daraja Framework
https://www.habarisoft.com/daraja_framework.html
- XST综合、实现过程包含哪些步骤
2013-06-25 18:53:50 在ISE的主界面的处理子窗口的synthesis的工具可以完成下面的任务: 查看RTL原理图(View RTL schematic) 查看技术原理图(View ...
- DIV内英文或者数字不换行的问题 解决办法
word-wrap:break-word; word-break:break-all;
- JavaScript DOM高级程序设计 3.-DOM2和HTML2--我要坚持到底!
由一个HTML进行说明,我就不敲了,直接copy <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" " ...