|
| 1 | + |
1 | 2 | [#0122-best-time-to-buy-and-sell-stock-ii]
|
2 |
| -= 122. Best Time to Buy and Sell Stock II |
| 3 | += 122. 买卖股票的最佳时机 II |
3 | 4 |
|
4 |
| -{leetcode}/problems/best-time-to-buy-and-sell-stock-ii/[LeetCode - Best Time to Buy and Sell Stock II^] |
| 5 | +https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-ii/[LeetCode - 122. 买卖股票的最佳时机 II ^] |
5 | 6 |
|
6 |
| -Say you have an array for which the _i^th^_ element is the price of a given stock on day _i_. |
| 7 | +给你一个整数数组 `prices` ,其中 `prices[i]` 表示某支股票第 `i` 天的价格。 |
7 | 8 |
|
8 |
| -Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times). |
| 9 | +在每一天,你可以决定是否购买和/或出售股票。你在任何时候 *最多* 只能持有 *一股* 股票。你也可以先购买,然后在 *同一天* 出售。 |
9 | 10 |
|
10 |
| -*Note:* You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again). |
| 11 | +返回 _你能获得的 *最大* 利润_ 。 |
11 | 12 |
|
12 |
| -.Example 1: |
13 |
| ----- |
14 |
| -Input: [7,1,5,3,6,4] |
15 |
| -Output: 7 |
16 |
| -Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4. |
17 |
| - Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3. |
18 |
| ----- |
| 13 | +*示例 1:* |
| 14 | + |
| 15 | +.... |
| 16 | +输入:prices = [7,1,5,3,6,4] |
| 17 | +输出:7 |
| 18 | +解释:在第 2 天(股票价格 = 1)的时候买入,在第 3 天(股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5 - 1 = 4。 |
| 19 | +随后,在第 4 天(股票价格 = 3)的时候买入,在第 5 天(股票价格 = 6)的时候卖出, 这笔交易所能获得利润 = 6 - 3 = 3。 |
| 20 | +最大总利润为 4 + 3 = 7 。 |
| 21 | +.... |
| 22 | + |
| 23 | +*示例 2:* |
| 24 | + |
| 25 | +.... |
| 26 | +输入:prices = [1,2,3,4,5] |
| 27 | +输出:4 |
| 28 | +解释:在第 1 天(股票价格 = 1)的时候买入,在第 5 天 (股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5 - 1 = 4。 |
| 29 | +最大总利润为 4 。 |
| 30 | +.... |
| 31 | + |
| 32 | +*示例 3:* |
| 33 | + |
| 34 | +.... |
| 35 | +输入:prices = [7,6,4,3,1] |
| 36 | +输出:0 |
| 37 | +解释:在这种情况下, 交易无法获得正利润,所以不参与交易可以获得最大利润,最大利润为 0。 |
| 38 | +.... |
| 39 | + |
| 40 | + |
| 41 | +*提示:* |
| 42 | + |
| 43 | +* `1 \<= prices.length \<= 3 * 10^4^` |
| 44 | +* `0 \<= prices[i] \<= 10^4^` |
19 | 45 |
|
20 |
| -.Example 2: |
21 |
| ----- |
22 |
| -Input: [1,2,3,4,5] |
23 |
| -Output: 4 |
24 |
| -Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4. |
25 |
| - Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are |
26 |
| - engaging multiple transactions at the same time. You must sell before buying again. |
27 |
| ----- |
28 | 46 |
|
29 |
| -.Example 3: |
30 |
| ----- |
31 |
| -Input: [7,6,4,3,1] |
32 |
| -Output: 0 |
33 |
| -Explanation: In this case, no transaction is done, i.e. max profit = 0. |
34 |
| ----- |
35 | 47 |
|
36 | 48 | == 思路分析
|
37 | 49 |
|
@@ -74,8 +86,18 @@ include::{sourcedir}/_0122_BestTimeToBuyAndSellStockII.java[tag=answer]
|
74 | 86 | include::{sourcedir}/_0122_BestTimeToBuyAndSellStockII_2.java[tag=answer]
|
75 | 87 | ----
|
76 | 88 | --
|
| 89 | +
|
| 90 | +三刷:: |
| 91 | ++ |
| 92 | +-- |
| 93 | +[{java_src_attr}] |
| 94 | +---- |
| 95 | +include::{sourcedir}/_0122_BestTimeToBuyAndSellStockIi_3.java[tag=answer] |
| 96 | +---- |
| 97 | +-- |
77 | 98 | ====
|
78 | 99 |
|
| 100 | + |
79 | 101 | == 参考资料
|
80 | 102 |
|
81 | 103 | . https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-with-cooldown/solutions/8610/yi-ge-fang-fa-tuan-mie-6-dao-gu-piao-wen-ti-by-lab/[309. 买卖股票的最佳时机含冷冻期 - 一个方法团灭 6 道股票问题^]
|
|
0 commit comments