java.lang.Object
g0101_0200.s0122_best_time_to_buy_and_sell_stock_ii.Solution

public class Solution extends Object
122 - Best Time to Buy and Sell Stock II.<p>Medium</p> <p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>ith</code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy it then immediately sell it on the <strong>same day</strong>.</p> <p>Find and return <em>the <strong>maximum</strong> profit you can achieve</em>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> prices = [7,1,5,3,6,4]</p> <p><strong>Output:</strong> 7</p> <p><strong>Explanation:</strong> Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4. Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3. Total profit is 4 + 3 = 7.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> prices = [1,2,3,4,5]</p> <p><strong>Output:</strong> 4</p> <p><strong>Explanation:</strong> Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4. Total profit is 4.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> prices = [7,6,4,3,1]</p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong> There is no way to make a positive profit, so we never buy the stock to achieve the maximum profit of 0.</p> <p><strong>Constraints:</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>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • maxProfit

      public int maxProfit(int[] prices)