1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-10 18:48:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/买卖股票的最佳时机 III [best-time-to-buy-and-sell-stock-iii].html
2022-03-29 12:43:11 +08:00

49 lines
1.9 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>给定一个数组,它的第<em> </em><code>i</code> 个元素是一支给定的股票在第 <code>i</code><em> </em>天的价格。</p>
<p>设计一个算法来计算你所能获取的最大利润。你最多可以完成 <strong>两笔 </strong>交易。</p>
<p><strong>注意:</strong>你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。</p>
<p> </p>
<p><strong>示例 1:</strong></p>
<pre>
<strong>输入:</strong>prices = [3,3,5,0,0,3,1,4]
<strong>输出:</strong>6
<strong>解释:</strong>在第 4 天(股票价格 = 0的时候买入在第 6 天(股票价格 = 3的时候卖出这笔交易所能获得利润 = 3-0 = 3 。
  随后,在第 7 天(股票价格 = 1的时候买入在第 8 天 (股票价格 = 4的时候卖出这笔交易所能获得利润 = 4-1 = 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><strong>示例 4</strong></p>
<pre>
<strong>输入:</strong>prices = [1]
<strong>输出:</strong>0
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= prices.length <= 10<sup>5</sup></code></li>
<li><code>0 <= prices[i] <= 10<sup>5</sup></code></li>
</ul>