文章目录
  1. 1. best time to buy and sell stock I
  2. 2. best time to buy and sell stock II
  3. 3. best time to buy and sell stock III
  4. 4. 代码实现

best time to buy and sell stock I

Description: 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 to find
the maximum profit.

题意:用一个数组表示股票每天的价格,数组的第i个数表示股票在第i天的价格。 如果只允许进行一次交易,也就是说只允许买一支股票并卖掉,求最大的收益

分析:动态规划法。从前向后遍历数组,记录当前出现过的最低价格,作为买入价格,并计算以当天价格出售的收益,作为可能的最大收益,整个遍历过程中,出现过的最大收益就是所求。

时间复杂度O(n),空间复杂度O(1)

best time to buy and sell stock II

Description: 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 as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

题目:用一个数组表示股票每天的价格,数组的第i个数表示股票在第i天的价格。交易次数不限,但一次只能交易一支股票,也就是说手上最多只能持有一支股票,求最大收益。

分析:贪心法。从前向后遍历数组,只要当天的价格高于前一天的价格,就算入收益。

时间复杂度O(n),空间复杂度O(1)

best time to buy and sell stock III

Description: 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, you must sell the stock before you buy again).

题意:用一个数组表示股票每天的价格,数组的第i个数表示股票在第i天的价格。最多交易两次,手上最多只能持有一支股票,求最大收益。

分析:动态规划法。以第i天为分界线,计算第i天之前进行一次交易的最大收益preProfit[i],和第i天之后进行一次交易的最大收益postProfit[i],最后遍历一遍,max{preProfit[i] + postProfit[i]} (0≤i≤n-1)就是最大收益。第i天之前和第i天之后进行一次的最大收益求法同Best Time to Buy and Sell Stock I。

时间复杂度O(n),空间复杂度O(n)

代码实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <vector>

using namespace std;

int stock1(vector<int> &prices)
{

int minp,maxprofit;

minp = prices[0];
maxprofit = 0;

for(int i=1;i<prices.size();i++){
if (minp > prices[i])
{
minp = prices[i];
}

if(maxprofit < prices[i] - minp)
{
maxprofit = prices[i] - minp;
}
}
return maxprofit;
}

int stock2(vector<int> &prices)
{

int maxprofit = 0;

for (int i=1;i<prices.size();i++)
{
if (prices[i]>prices[i-1])
{
maxprofit += prices[i] - prices[i-1];
}
}
return maxprofit;
}

int stock3(vector<int> &prices)
{

int maxprofit = 0;
int tmp;
vector<int> prices1;
vector<int> prices2;

for (int i=1;i<prices.size()-1;i++)
{

prices1.reserve(i);
prices2.reserve(prices.size()-1);
prices1.insert(prices1.begin(),&prices.front(),&prices[i]);
prices2.insert(prices2.begin(),&prices[i-1],&(prices.back()));
//cout<<"front:"<<prices1.front()<<" back:"<<prices1.back()<<endl;
//cout<<"front:"<<prices2.front()<<" back:"<<prices2.back()<<endl;
tmp = stock1(prices1) + stock1(prices2);
if (maxprofit < tmp)
{
maxprofit = tmp;
}
prices1.clear();
prices2.clear();
}
return maxprofit;
}

int main()
{

int p[12] = {3,5,4,2,4,6,4,3,6,7,4,9};
vector<int> prices(&p[0],&p[12]);

cout<<"max profit1:"<<stock1(prices)<<endl;
cout<<"max profit2:"<<stock2(prices)<<endl;
cout<<"max profit3:"<<stock3(prices)<<endl;
return 0;
}
文章目录
  1. 1. best time to buy and sell stock I
  2. 2. best time to buy and sell stock II
  3. 3. best time to buy and sell stock III
  4. 4. 代码实现