使用动态规划,下面的代码可以通过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的更多相关文章

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

  2. leetcode309 买卖股票

    一.穷举框架 首先,还是一样的思路:如何穷举?这里的穷举思路和上篇文章递归的思想不太一样. 递归其实是符合我们思考的逻辑的,一步步推进,遇到无法解决的就丢给递归,一不小心就做出来了,可读性还很好.缺点 ...

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

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

  5. leetcode 714. 买卖股票的最佳时机含手续费

    继承leetcode123以及leetcode309的思路,,但应该也可以写成leetcode 152. 乘积最大子序列的形式 class Solution { public: int maxProf ...

随机推荐

  1. windows环境搭建Vue开发环境

    一.安装node.js(https://nodejs.org/en/) 下载完毕后,可以安装node,建议不要安装在系统盘(如C:). 二.设置nodejs prefix(全局)和cache(缓存)路 ...

  2. Java语法基础学习DayThirteen(枚举类和注解)

    一.枚举类 1.概述:即一个类中只能有有限个对象,若只有一个对象,则可以作为单例模式的一种实现. 2.自定义枚举类(JDK1.5以前这么做) //枚举类 class Season{ //1.提供类的属 ...

  3. SVG的用法

    三种添加方式 <iframe src="图的地址" frameborder="0"></iframe> <object width ...

  4. Java学习笔记40(sql:将数据库内数据存入对象中)

    新建一个数据表: use qy97; create table student( id int primary key auto_increment, sname ), gander ), age i ...

  5. 基于Verilog的按键检测实验

    一.模块框图及基本思路 detect_module:检测按键输入脚的电平边沿变化 delay_10ms_module:延时消抖,输出按键有效信号 debounce_module:前两个模块的组合模块 ...

  6. C# 文件上传下载功能实现 文件管理引擎开发

    Prepare 本文将使用一个NuGet公开的组件技术来实现一个服务器端的文件管理引擎,提供了一些简单的API,来方便的实现文件引擎来对您自己的软件系统的文件进行管理. 在Visual Studio ...

  7. Java错误:结束的字符文字

    编译器为NetBeans 在学习java的时候突然出现了以下错误 错误代码是: Gen <Integer ,String> a = new Gen <Integer, String& ...

  8. 01bootstrap_基本结构和布局

    01bootstrap_基本结构 学习bootstrap需要下载必要的文件:www.bootcss.com 基本结构 container page-header 布局 1.响应式布局:containe ...

  9. 面向对象+JAVA基础

    泛泛的研究了面向对象的前五章,感觉没有代码的参照理解的知识看过就忘,所以又在推荐下开始了JAVA基础,希望不会鄙视我目前么有一本书能完整看完的记录... public class LeapYear { ...

  10. js 自定义类Android吐司提示框

    (function(){     var mouseX = 0;     var mouseY = 0;     //定义一个全局toaslist用来存在新建的吐司     var toastLsit ...