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 ...
随机推荐
- 在myeclipse中使用./和../遇到的问题
今天用ajax验证的时候,ajax的代码一直不起作用,我在浏览器里打开了开发者模式,错误的原因是找不到"jquery-1.8.3.min.js",但是我的目录结构都没有问题. &l ...
- JavaScript实现本地图片上传前进行裁剪预览
本项目支持IE8+,测试环境IE8,IE9,IE10,IE11,Chrome,FireFox测试通过 另:本项目并不支持Vue,React等,也不建议,引入JQuery和Vue.React本身提倡的开 ...
- 编译QFileSystemModel
QT在windows系统下可以直接安装,但有些时候,可以只编译一个类,这里需要有一些需要注意的.下面是github路径:https://github.com/1171597779/compile_of ...
- Linux每天一个命令:nc/ncat
nmap-ncat.x86_64版nc/ncat nc/ncat所做的就是在两台电脑之间建立链接并返回两个数据流,在这之后所能做的事就看你的想像力了.你能建立一个服务器,传输文件,与朋友聊天,传输流媒 ...
- 【未解决】对于使用Windows的IDEA进行编译的文件,但无法在Linux系统中统计代码行数的疑问
在我学习使用Windows的IDEA的过程中,将代码文件转移到Linux虚拟机当中,但无法在Linux系统中统计代码行数. 注意:拷贝进虚拟机的文件均能编译运行. 具体过程如下: root@yogil ...
- makeObjectsPerformSelector对数组中的对象发送消息执行对象中方法
- (void)makeObjectsPerformSelector:(SEL)aSelector; - (void)makeObjectsPerformSelector:(SEL)aSelector ...
- 2017年4月7日16:18:17 java8 常用记录
List<String> customerUids = customerTagModel.stream().map(CustomerTagModel::getCustomerUid) ...
- CSS 实现单、多行文本溢出显示省略号(…)
如果实现单行文本的溢出显示省略号同学们应该都知道用text-overflow:ellipsis属性来,当然还需要加宽度width属来兼容部分浏览. 实现方法: overflow: hidden; te ...
- wpf-x命名空间-Markup Extension(标记扩展)
1.x:type 用于前端类型声明 与C# 代码 Type类似 2.x:Null 代表Null 某些时候需要显示的为一些值设置为空 前端为 x:Null C# 中 为 Null 3.x:ar ...
- Linux 创建虚拟机,配置网卡,桥接,连接XShell
一.新建虚拟机 1.“root” 输入密码: 2.看虚拟机的IP地址 “ifconfig” 二.配置网卡 1.更改设置(打开vim编辑) “vim /etc/sysconfig/network- ...