|
| 1 | +<h2>unknown-problem</h2><h3>Easy</h3><hr><div><p>Say you have an array <code>prices</code> for which the <em>i</em><sup>th</sup> element is the price of a given stock on day <em>i</em>.</p> |
| 2 | + |
| 3 | +<p>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).</p> |
| 4 | + |
| 5 | +<p><strong>Note:</strong> You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).</p> |
| 6 | + |
| 7 | +<p><strong>Example 1:</strong></p> |
| 8 | + |
| 9 | +<pre><strong>Input:</strong> [7,1,5,3,6,4] |
| 10 | +<strong>Output:</strong> 7 |
| 11 | +<strong>Explanation:</strong> Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4. |
| 12 | + Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3. |
| 13 | +</pre> |
| 14 | + |
| 15 | +<p><strong>Example 2:</strong></p> |
| 16 | + |
| 17 | +<pre><strong>Input:</strong> [1,2,3,4,5] |
| 18 | +<strong>Output:</strong> 4 |
| 19 | +<strong>Explanation:</strong> Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4. |
| 20 | + Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are |
| 21 | + engaging multiple transactions at the same time. You must sell before buying again. |
| 22 | +</pre> |
| 23 | + |
| 24 | +<p><strong>Example 3:</strong></p> |
| 25 | + |
| 26 | +<pre><strong>Input:</strong> [7,6,4,3,1] |
| 27 | +<strong>Output:</strong> 0 |
| 28 | +<strong>Explanation:</strong> In this case, no transaction is done, i.e. max profit = 0.</pre> |
| 29 | + |
| 30 | +<p> </p> |
| 31 | +<p><strong>Constraints:</strong></p> |
| 32 | + |
| 33 | +<ul> |
| 34 | + <li><code>1 <= prices.length <= 3 * 10 ^ 4</code></li> |
| 35 | + <li><code>0 <= prices[i] <= 10 ^ 4</code></li> |
| 36 | +</ul> |
| 37 | +</div> |
0 commit comments