solution.md 3.7 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1
# 买卖股票的最佳时机 II
F
fix bug  
feilong 已提交
2

每日一练社区's avatar
每日一练社区 已提交
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
<p>给定一个数组 <code>prices</code> ,其中 <code>prices[i]</code> 是一支给定股票第 <code>i</code> 天的价格。</p>

<p>设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易(多次买卖一支股票)。</p>

<p><strong>注意:</strong>你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。</p>

<p> </p>

<p><strong>示例 1:</strong></p>

<pre>
<strong>输入:</strong> prices = [7,1,5,3,6,4]
<strong>输出:</strong> 7
<strong>解释:</strong> 在第 2 天(股票价格 = 1)的时候买入,在第 3 天(股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。
     随后,在第 4 天(股票价格 = 3)的时候买入,在第 5 天(股票价格 = 6)的时候卖出, 这笔交易所能获得利润 = 6-3 = 3 。
</pre>

<p><strong>示例 2:</strong></p>

<pre>
<strong>输入:</strong> prices = [1,2,3,4,5]
<strong>输出:</strong> 4
<strong>解释:</strong> 在第 1 天(股票价格 = 1)的时候买入,在第 5 天 (股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。
     注意你不能在第 1 天和第 2 天接连购买股票,之后再将它们卖出。因为这样属于同时参与了多笔交易,你必须在再次购买前出售掉之前的股票。
</pre>

<p><strong>示例 3:</strong></p>

<pre>
<strong>输入:</strong> prices = [7,6,4,3,1]
<strong>输出:</strong> 0
<strong>解释:</strong> 在这种情况下, 没有交易完成, 所以最大利润为 0。</pre>

<p> </p>

<p><strong>提示:</strong></p>

<ul>
	<li><code>1 <= prices.length <= 3 * 10<sup>4</sup></code></li>
	<li><code>0 <= prices[i] <= 10<sup>4</sup></code></li>
</ul>

每日一练社区's avatar
每日一练社区 已提交
45
<p>以下<span style="color:red">错误</span>的选项是?</p>
F
fix bug  
feilong 已提交
46

每日一练社区's avatar
每日一练社区 已提交
47
## aop
F
fix bug  
feilong 已提交
48

每日一练社区's avatar
每日一练社区 已提交
49
### before
F
fix bug  
feilong 已提交
50

每日一练社区's avatar
每日一练社区 已提交
51
```c
每日一练社区's avatar
每日一练社区 已提交
52 53 54
#include <bits/stdc++.h>
using namespace std;
```
每日一练社区's avatar
每日一练社区 已提交
55

每日一练社区's avatar
每日一练社区 已提交
56
### after
F
fix bug  
feilong 已提交
57

每日一练社区's avatar
每日一练社区 已提交
58
```c
每日一练社区's avatar
每日一练社区 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71
int main()
{
    Solution sol;
    vector<int> prices = {1, 2, 3, 4, 5};

    int res;
    res = sol.maxProfit(prices);
    cout << res;
    return 0;
}
```

## 答案
F
fix bug  
feilong 已提交
72

每日一练社区's avatar
每日一练社区 已提交
73
```c
每日一练社区's avatar
每日一练社区 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
class Solution
{
public:
    int maxProfit(vector<int> &prices)
    {

        int dp[prices.size()][2];
        dp[0][0] = 0;
        dp[0][1] = -prices[0];
        for (int i = 1; i < prices.size(); i++)
        {
            dp[i][0] = max(dp[i - 1][0], dp[i][1] + prices[i]);
            dp[i][1] = max(dp[i - 1][0] - prices[i], dp[i][1]);
        }
        return dp[prices.size() - 1][0];
    }
};
```
## 选项

F
fix bug  
feilong 已提交
94

每日一练社区's avatar
每日一练社区 已提交
95
### A
F
fix bug  
feilong 已提交
96

每日一练社区's avatar
每日一练社区 已提交
97
```c
每日一练社区's avatar
每日一练社区 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
class Solution
{
public:
    int maxProfit(vector<int> &prices)
    {
        int len = prices.size();
        int mpro = 0;
        if (len < 2)
            return 0;
        for (int i = 0; i < len - 1; i++)
        {
            if (prices[i + 1] > prices[i])
            {
                mpro += prices[i + 1] - prices[i];
            }
        }
        return mpro;
    }
};
```

### B
F
fix bug  
feilong 已提交
120

每日一练社区's avatar
每日一练社区 已提交
121
```c
每日一练社区's avatar
每日一练社区 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
class Solution
{
public:
    int maxProfit(vector<int> &prices)
    {
        int valley, peak, maxProfit = 0, index = 0;
        int len = prices.size();
        while (index < len - 1)
        {
            while (index < prices.size() - 1 && prices[index] >= prices[index + 1])
                index++;
            valley = prices[index];
            while (index < prices.size() - 1 && prices[index] <= prices[index + 1])
                index++;
            peak = prices[index];
            maxProfit += peak - valley;
        }
        return maxProfit;
    }
};
```

### C
F
fix bug  
feilong 已提交
145

每日一练社区's avatar
每日一练社区 已提交
146
```c
每日一练社区's avatar
每日一练社区 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159
class Solution
{
public:
    int maxProfit(vector<int> &prices)
    {
        int profit = 0;
        for (auto i = prices.begin(); i != prices.end(); ++i)
            if (i + 1 != prices.end() && *(i + 1) > *i)
                profit += *(i + 1) - *i;
        return profit;
    }
};
```