leetcode309
使用动态规划,下面的代码可以通过210个测试,最后1个(第211个)会超时。说明思路是正确的,但是其中有一些是无效的计算。
class Solution {
public:
int maxProfit(vector<int>& prices) {
int n=prices.size();
if(n==)
{
return ;
}
int N=;
int **D;
D = new int*[N];
for(int i=;i<n;i++){
D[i]=new int[N];
}
for(int i=;i<n;i++){
for(int j=i;j<n;j++){
int pj=prices[j];
int pi=prices[i];
D[i][j]=pj-pi;
}
} for(int len=;len<n;len++){
for(int i=;i<n-;i++){
int j=i+len;
if(j>=n){
break;
}
int m_ij=D[i][j];
int max_k=;
for(int k=i+;k<j;k++){
int m11 = D[i][k];
int m12=;
if(k+<j){
m12=D[k+][j];
}
int m1=m11+m12; int m21=D[k][j];
int m22=;
if(k->i){
m22=D[i][k-];
}
int m2=m21+m22;
int m=max(m1,m2);
max_k=max(max_k,m);
}
D[i][j]=max(m_ij,max_k);
}
} return D[][n-];
}
};
再提供网上AC的参考实现:
class Solution {
public:
int maxProfit(vector<int>& prices) {
if (prices.size() <= )
return ;
int s0 = ;
int s1 = -prices[];
int s2 = INT_MIN;
for (int i = ; i < prices.size(); i++){
int pre0 = s0;
int pre1 = s1;
int pre2 = s2;
s0 = max(pre0, pre2);
s1 = max(pre0 - prices[i], pre1);
s2 = pre1 + prices[i];
} return max(s0, s2);
}
};
再补充一个:
class Solution {
public:
int maxProfit(vector<int>& prices) {
int len = prices.size();
if(len == || len == )
return ;
if(len == )
return prices[] > prices[] ? prices[] - prices[] : ; vector<int> s0(len);
vector<int> s1(len);
vector<int> s2(len);
s0[] = ;
s1[] = max(-prices[], -prices[]);
s2[] = prices[] - prices[];
for(int i = ; i < len; i++)
{
s0[i] = max(s0[i-], s2[i-]);
s1[i] = max(s0[i-] - prices[i-], s1[i-]);
s2[i] = s1[i-] + prices[i - ];
}
return max(max(s0[len-], s2[len-]), s1[len-] + prices[len-]);
}
};
leetcode309的更多相关文章
- [Swift]LeetCode309. 最佳买卖股票时机含冷冻期 | Best Time to Buy and Sell Stock with Cooldown
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- leetcode309 买卖股票
一.穷举框架 首先,还是一样的思路:如何穷举?这里的穷举思路和上篇文章递归的思想不太一样. 递归其实是符合我们思考的逻辑的,一步步推进,遇到无法解决的就丢给递归,一不小心就做出来了,可读性还很好.缺点 ...
- LeetCode-714.Best Time to Buy and Sell Stock with Transaction Fee
Your are given an array of integers prices, for which the i-th element is the price of a given stock ...
- LeetCode-188.Best Time to Buy and Sell Stock IV
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- leetcode 714. 买卖股票的最佳时机含手续费
继承leetcode123以及leetcode309的思路,,但应该也可以写成leetcode 152. 乘积最大子序列的形式 class Solution { public: int maxProf ...
随机推荐
- 说说VBA中的面向对象
对象是 Visual Basic 的结构基础,在 Visual Basic 中进行的所有操作几乎都与修改对象有关.Microsoft Word 的任何元素,如文档.表格.段落.书签.域等,都可用 Vi ...
- 剑指Offer 18. 二叉树的镜像 (二叉树)
题目描述 操作给定的二叉树,将其变换为源二叉树的镜像. 输入描述: 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ ...
- 微信小程序富文本中的图片大小超出屏幕
这个问题我在小程序社区中提的,后来有个帮我回答了这个问题,我试了一下可以. 解决办法是过滤富文本内容,给图片标签添加一个样式,限制图片的最大宽度. replace(/\<img/gi, '& ...
- 公众号及H5支付
本篇主要记录微信支付中公众号及H5支付全过程. 1|1准备篇 公众号或者服务号(并开通微信支付功能).商户平台中开通JSAPI支付.H5支付. 1|2配置篇 公众号或者服务号中 -------开发-- ...
- SQL-记录查询篇-009
在学习记录查询之前,学习一些关键字的使用: 1.逻辑运算符:and . or . not .is null select * from table_name where id>2 and ...
- python------SocketServer (0809)
socket(一对一) 与socketserver 一.socketserver 1. 正式定义:The socketserver module simplifies the task of writ ...
- Entity Framework教程翻译 ---- 系列教程
Entity Framework教程(第二版) (翻译)Entity Framework技巧系列之十四 - Tip 56 (翻译)Entity Framework技巧系列之十三 - Tip 51 - ...
- springboot解决跨域问题
在启动类里加入corsFilter import org.springframework.boot.SpringApplication; import org.springframework.boot ...
- systemd service 设置limit,不生效问题
参考博文: http://smilejay.com/2016/06/centos-7-systemd-conf-limits/(解决方法参考此博文) 问题简述:Centos7下修改系统的最大文件打 ...
- Centos7 下安装VMware tools
1:先在虚拟机点击安装VMware Tools 2:然后挂载 mount /dev/cdrom /mnt 3:进入/mnt,可以看到有 4:拷贝VMwareTools到其他 ...