题意:用一个数组表示股票每天的价格,数组的第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. Suffix Automaton

    后缀自动机 先上SAM builder,备用链接.之前的垃圾博客,洛谷的某篇教程,饕餮传奇的题单. 后缀自动机,点数是2n! 首先对着代码讲一遍三种插入. inline void insert(cha ...

  2. jQuery使用(十):jQuery实例方法之位置、坐标、图形(BOM)

    offset() position() scrollTop().scrollLeft width().height() innerWidth().outerWidth().innerHeight(). ...

  3. word中中文保持正体,英文用斜体的方法.

    有时候,大段的文字中夹杂着英文字母,英文需要斜体,如果一个接一个选中再斜体,费时费力,那么怎样快速实现文斜体中文不斜体呢? 工具/原料 word软件 方法/步骤 选中要修改的段落,替换-查找内容-特殊 ...

  4. Collections of Zujin Zhang's Published works

    I am not good, but I shall do my best to be better. Any questions, please feel free to contact zhang ...

  5. MySQL学习5 - 数据类型二.md

    一 字符类型 二 枚举类型和集合类型 一 字符类型 #官网:https://dev.mysql.com/doc/refman/5.7/en/char.html #注意:char和varchar括号内的 ...

  6. 第30月第3天 iOS图标icon自动生成和自定义尺寸

    1. http://icon.wuruihong.com/ https://www.jianshu.com/p/684751c14735 2.status bar UIViewControllerBa ...

  7. How to avoid the 0-byte file on Webclient download error

    _client.DownloadDataAsync(new Uri(url)); _client.DownloadDataCompleted += (sender, e) => { try { ...

  8. MATLAB更换编辑器配色方案

    MATLAB的默认编辑配色方案白色,长时间面对高亮度的白色界面容易产生眼睛疲劳的感觉,那么如何更换编辑器配色方案呢?经过不断探索以及查阅资料,发现了下列几种配色方案.配色文件来源于https://gi ...

  9. Pycharm新建模板默认添加作者时间等信息

    在pycharm使用过程中,对于每次新建文件的shebang行和关于代码编写者的一些个人信息快捷填写,使用模板的方式比较方便. 方法如下: 1.打开pycharm,选择File-Settings 2. ...

  10. 近日测试发现所有Excel相关功能均会抛异常,查后发现与福昕阅读器不兼容

    报这种错: System.Runtime.InteropServices.COMException (0x80010105): 服务器出现意外情况. (异常来自 HRESULT:0x80010105 ...