题意:用一个数组表示股票每天的价格,数组的第i个数表示股票在第i天的价格。 如果最多进行两次交易,但必须在买进一只股票前清空手中的股票,求最大的收益。
示例 1:
Input: [3,3,5,0,0,3,1,4]
Output: 6
Explanation: Buy on day 4 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.
             Then buy on day 7 (price = 1) and sell on day 8 (price = 4), profit = 4-1 = 3.
示例 2:
Input: [1,2,3,4,5]
Output: 4
Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
             Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are
             engaging multiple transactions at the same time. You must sell before buying again.
示例 3:
Input: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.
 
之前有一个Best Time to Buy and Sell Stock I ,比这个题简单点儿,那个题是最多一次操作,求最大的收益。同样是使用动态规划,由于这次允许最多两次操作,只需要分两次,
将输入的整个周期通过for对周期长度限制(第一个子周期长度递增且不超过原周期长)并分割成两个子周期先后进行dp数组的递推即可。
 
 int maxProfit(int* prices, int pricesSize) {
int ans = ;
if (pricesSize > ){
int*dp = (int*)malloc(pricesSize*sizeof(int));
dp[] = ;
int ans2 = ;
for (int i = ; i <= pricesSize; i++){
dp[] = ;
ans2 = ;
for (int j = ; j<i; j++){
dp[j] = (prices[j] + dp[j - ] - prices[j - ]) > ? (prices[j] + dp[j - ] - prices[j - ]) : ;
ans2 = ans2 >= dp[j] ? ans2 : dp[j];
}
ans = ans >= ans2 ? ans : ans2;
if (i < pricesSize)dp[i] = ;
for (int k = i+; k<pricesSize; k++){
dp[k] = (prices[k] + dp[k - ] - prices[k - ]) > ? (prices[k] + dp[k - ] - prices[k - ]) : ;
ans = ans >= (dp[k]+ans2) ? ans : (dp[k]+ans2);
}
}
//free(dp);
}
else if (pricesSize == ){
ans = (prices[] - prices[]) > ? (prices[] - prices[]):;
}
else ans = ;
return ans;
}

但是非常搞笑的是我这个代码虽然在LeetCode上成功AC,但是应该是最差的一个解决方案。。。但是思路上非常便于理解

动态规划——Best Time to Buy and Sell Stock III的更多相关文章

  1. Leetcode之动态规划(DP)专题-123. 买卖股票的最佳时机 III(Best Time to Buy and Sell Stock III)

    Leetcode之动态规划(DP)专题-123. 买卖股票的最佳时机 III(Best Time to Buy and Sell Stock III) 股票问题: 121. 买卖股票的最佳时机 122 ...

  2. 27. Best Time to Buy and Sell Stock && Best Time to Buy and Sell Stock II && Best Time to Buy and Sell Stock III

    Best Time to Buy and Sell Stock (onlineJudge: https://oj.leetcode.com/problems/best-time-to-buy-and- ...

  3. LeetCode 笔记23 Best Time to Buy and Sell Stock III

    Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the price of ...

  4. Best Time to Buy and Sell Stock | & || & III

    Best Time to Buy and Sell Stock I Say you have an array for which the ith element is the price of a ...

  5. 【leetcode】Best Time to Buy and Sell Stock III

    Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the price of ...

  6. LeerCode 123 Best Time to Buy and Sell Stock III之O(n)解法

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  7. 【leetcode】123. Best Time to Buy and Sell Stock III

    @requires_authorization @author johnsondu @create_time 2015.7.22 19:04 @url [Best Time to Buy and Se ...

  8. LeetCode: Best Time to Buy and Sell Stock III 解题报告

    Best Time to Buy and Sell Stock IIIQuestion SolutionSay you have an array for which the ith element ...

  9. [leetcode]123. Best Time to Buy and Sell Stock III 最佳炒股时机之三

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

随机推荐

  1. 使pre的内容自动换行(转)

    <pre> 元素可定义预格式化的文本.被包围在 pre 元素中的文本通常会保留空格和换行符.而文本也会呈现为等宽字体. <pre> 标签的一个常见应用就是用来表示计算机的源代码 ...

  2. CF24D Broken robot

    题目链接 题意 有一个\(n \times m\)的矩阵.机器人从点\((x,y)\)开始等概率的往下,往右,往左走或者不动.如果再第一列,那么不会往左走,再第m列不会往右走.也就是说机器人不会走出这 ...

  3. 给react-native添加图标和启动屏

    react native 项目默认是没有图标,并且启动页面只有文字.这个样子并不能算是一个完整的APP,现在就给APP加一个图标和一个适应所有屏幕尺寸的启动图,并且设置启动图遮住项目启动时候的白色闪屏 ...

  4. pta寒假作业2

    题目二币值转换 题目代码 #include<stdio.h> int main (void) { int n, initial_n; scanf("%d", & ...

  5. FreeNAS系统总结

    FreeNAS简介 FreeNAS是一套免费的NAS服务器,它能将一部普通PC变成网络存储服务器.该软件基于FreeBSD,Samba 及PHP,支持CIFS (samba), FTP, NFS pr ...

  6. 我眼中的K-近邻算法

    有一句话这样说:如果你想了解一个人,你可以从他身边的朋友开始. 如果与他交往的好友都是一些品行高尚的人,那么可以认为这个人的品行也差不了. 其实古人在这方面的名言警句,寓言故事有很多.例如:人以类聚, ...

  7. 在CentOS 上搭建nginx来部署静态页面网站

    在centOs 上搭建nginx来部署静态页面网站 一.部署服务器环境 nginx:轻量级.高性能的HTTP及反向代理服务器,占用内存少,并发能力强,相比老牌的apache作为web服务器,性能更加卓 ...

  8. Centos7安装vsftpd (FTP服务器)

    Centos7安装vsftpd (FTP服务器) 原文链接:https://www.jianshu.com/p/9abad055fff6 TyiMan 关注 2016.02.06 21:19* 字数 ...

  9. Shiro 系列: 简单命令行程序示例

    在本示例中, 使用 INI 文件来定义用户和角色. 首先学习一下 INI 文件的规范. =======================Shiro INI 的基本规范================== ...

  10. [再寄小读者之数学篇](2014-06-23 积分不等式 [中国科学技术大学2013年高等数学B 考研试题])

    设 $f(x)$ 在 $[a,b]$ 上一阶连续可导, $f(a)=0$. 证明: $$\bex \int_a^b f^2(x)\rd x\leq \cfrac{(b-a)^2}{2}\int_a^b ...