Best Time to Buy and Sell Stock系列

1、Best Time to Buy and Sell Stock II

 1 class Solution {
 2 public:
 3     int maxProfit(vector<int>& prices) {
 4            if(prices.size() < 1)   //prices.size is unsigned int
 5                 return 0;
 6            int pro = 0;
 7            for(int i=0; i<prices.size()-1; i++)
 8            {
 9                if(prices[i+1] >prices[i])
10                       pro += prices[i+1]-prices[i];
11            }
12            return pro;
13     }
14 };

实际股票是不可能知道今天以后的票价,否则就会根据走向趋势,每次取谷底与山顶就可以了。

2、Best Time to Buy and Sell Stock with Cooldown

 1 class Solution {
 2 public:
 3     int maxProfit(vector<int>& prices) {
 4         int n = prices.size();
 5         if(n < 2)
 6             return 0;
 7         vector<int> sells(n, 0);
 8         vector<int> buys(n, 0);
 9         int delay = 0;
10         sells[0] = 0;
11         buys[0] = -prices[0];
12         sells[1] =  prices[1]-prices[0];
13         buys[1] =  -prices[1];
14         int res = max(0, prices[1]-prices[0]);
15         for(int i=2; i<n; ++i)
16         {
17             delay = prices[i]-prices[i-1];
18             buys[i] = max(sells[i-2]-prices[i], buys[i-1]-delay);
19             sells[i] = max(buys[i-1]+prices[i], sells[i-1]+delay);
20             if(res <sells[i])
21                   res = sells[i];
22         }
23         return res;
24     }
25 };

分析见:http://bookshadow.com/weblog/2015/11/24/leetcode-best-time-to-buy-and-sell-stock-with-cooldown/

3、

时间: 2024-10-24 17:06:47

Best Time to Buy and Sell Stock系列的相关文章

Best Time to Buy and Sell Stock 系列

Best Time to Buy and Sell Stock I Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorith

Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】

Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete at most k transactions. 解题思路: 本题是Best Time to Buy and Sell Stock系列最难的一道,需要用到dp,JAVA实现如下: public i

[Lintcode] Best Time to Buy and Sell Stock I, II, III &amp;&amp; Maximum Subarray

买卖股票系列 && Subarray 相关题目: Best Time to Buy and Sell Stock I && II && III (IV 单独开贴) Maximum Subarray I && II 为什么这些我要总结到一起呢?因为思路基本一致. 题目简略: 买卖股票1: 一次买卖,利润最大. 买卖股票2: N次买卖,利润最大. 买卖股票3: 2次不重叠的买卖,利润最大. Maximum Subarray: Given an a

[leetcode]_Best Time to Buy and Sell Stock I &amp;&amp; II

一个系列三道题,我都不会做,google之答案.过了两道,第三道看不懂,放置,稍后继续. 一.Best Time to Buy and Sell Stock I 题目:一个数组表示一支股票的价格变换.要求只买卖一次,获得最大收益. 思路:一开始我认为是寻找最大.最小值,但由于最大值不一定总是出现在最小值的后面,因此WA. 参考思路:DP.对第i个价格,减去前i-1个价格中的最小值(保证该收益是在第i个价格卖出的最大收益),其收益与之前获得的最大收益相比. 代码: 1 public int max

[LeetCode] 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 algorithm to find the maximum profit. You may complete at most two transactions. Note:You may not engage in multiple transactions at the same time (ie,

LeetCode Best Time to Buy and Sell Stock II

Best Time to Buy and Sell Stock II Total Accepted: 41127 Total Submissions: 108434 My Submissions Question Solution Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit

Best Time to Buy and Sell Stock

Best Time to Buy and Sell Stock I Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorith

[Leetcode][JAVA] Best Time to Buy and Sell Stock I, II, III

Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm

[LeetCode]Best Time to Buy and Sell Stock III 动态规划

本题是Best Time to Buy and Sell Stock/的改进版. 本题中,可以买最多买进卖出两次股票. 买两次股票可以看成是第0~i天买进卖出以及第i+1~n-1天买进卖出两部分.这要枚举i并求出0th~ith的最大利益与(i+1)th~(n-1)th的最大利益之和的最大值就是买进卖出两次可以得到的最大利益.即状态转移方程: dp[0,n-1]=max{dp[0,k]+dp[k+1,n-1]},k=1,...,n-2 而只买进卖出一次的最大利润通过0th~ith可以求得. 这里求