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 ...
随机推荐
- 数据库编程加入transaction
TransManager tm = new TransManager(); tm.begin();//开启事物 try { //sql执行代码 // // tm.commit();//更改完sql之后 ...
- ceph存储集群性能测试工具步骤(初稿)
一.源码安装fio工具: #yum install libaio-devel make #wget http://brick.kernel.dk/snaps/fio-2.2.10.tar.gz #ta ...
- 前端-URL到页面显示的全过程
大概有以下几步: DNS解析(浏览器缓存→系统缓存→路由器缓存→ISP DNS缓存→从根域名服务器递归搜索) 建立TCP链接(TCP的三次握手) 浏览器向服务器发送HTTP请求 服务器返回结果给浏览器 ...
- Socket网络编程入门
Socket:专业术语:套接字;通俗的解释:两孔插座(一个孔:IP地址,一个孔:端口号).使用场景:通信,如QQ好友交谈,如浏览器的进程怎么与web服务器通信等. Socket来历: socket起源 ...
- shell脚本监测DNS链接状态给传给zabbix值
#!/bin/sh time_out=0 querygt3s=0 i=1 while [[ $i -le 15 ]] do i=`expr $i + 1` sleep 2 while read lin ...
- linux日常命令之一
zcat Linux中,cat命令可查看文本内容: 对于压缩包内的文本,可使用zcat命令,在不解压的情况下查看文本内容: iconv file -i 可查看文件字符集: iconv为字符集转换命令, ...
- codeforces 804A Find Amir 思维/水题
A few years ago Sajjad left his school and register to another one due to security reasons. Now he w ...
- POJ2376 Cleaning Shifts
题意 POJ2376 Cleaning Shifts 0x50「动态规划」例题 http://bailian.openjudge.cn/practice/2376 总时间限制: 1000ms 内存限制 ...
- PythonStudy——Python 内存池机制 (Memory pool mechanism) Pymalloc
Python是如何进行内存管理-内存池机制 Pymalloc Python引用了一个内存池(memory pool)机制,即Pymalloc机制(malloc:n.分配内存),用于对小块内存的申请和释 ...
- PLC 通讯
几个之前整理的高级语言与PLC通讯的资源下载链接:三菱:http://blog.sina.com.cn/s/blog_16d7d3ecb0102x6wj.html倍福:http://bbs.elecf ...