|
| 1 | +package com.diguage.algorithm.leetcode; |
| 2 | + |
| 3 | +import java.util.Objects; |
| 4 | + |
| 5 | +/** |
| 6 | + * = 122. Best Time to Buy and Sell Stock II |
| 7 | + * |
| 8 | + * https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/[Best Time to Buy and Sell Stock II - LeetCode] |
| 9 | + * |
| 10 | + * Say you have an array for which the ith element is the price of a given stock on day i. |
| 11 | + * |
| 12 | + * 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). |
| 13 | + * |
| 14 | + * Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again). |
| 15 | + * |
| 16 | + * .Example 1: |
| 17 | + * [source] |
| 18 | + * ---- |
| 19 | + * Input: [7,1,5,3,6,4] |
| 20 | + * Output: 7 |
| 21 | + * Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4. |
| 22 | + * Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3. |
| 23 | + * ---- |
| 24 | + * |
| 25 | + * .Example 2: |
| 26 | + * [source] |
| 27 | + * ---- |
| 28 | + * Input: [1,2,3,4,5] |
| 29 | + * Output: 4 |
| 30 | + * Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4. |
| 31 | + * Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are |
| 32 | + * engaging multiple transactions at the same time. You must sell before buying again. |
| 33 | + * ---- |
| 34 | + * |
| 35 | + * .Example 3: |
| 36 | + * [source] |
| 37 | + * ---- |
| 38 | + * Input: [7,6,4,3,1] |
| 39 | + * Output: 0 |
| 40 | + * Explanation: In this case, no transaction is done, i.e. max profit = 0. |
| 41 | + * ---- |
| 42 | + * |
| 43 | + * @author D瓜哥, https://www.diguage.com/ |
| 44 | + * @since 2020-01-04 10:55 |
| 45 | + */ |
| 46 | +public class BestTimeToBuyAndSellStockIi { |
| 47 | + |
| 48 | + /** |
| 49 | + * Runtime: 1 ms, faster than 85.09% of Java online submissions for Best Time to Buy and Sell Stock II. |
| 50 | + * |
| 51 | + * Memory Usage: 37 MB, less than 100.00% of Java online submissions for Best Time to Buy and Sell Stock II. |
| 52 | + */ |
| 53 | + public int maxProfit(int[] prices) { |
| 54 | + if (Objects.isNull(prices) || prices.length == 0) { |
| 55 | + return 0; |
| 56 | + } |
| 57 | + int result = 0; |
| 58 | + for (int i = 1; i < prices.length; i++) { |
| 59 | + if (prices[i - 1] < prices[i]) { |
| 60 | + result += prices[i] - prices[i - 1]; |
| 61 | + } |
| 62 | + } |
| 63 | + return result; |
| 64 | + } |
| 65 | + /** |
| 66 | + * Runtime: 1 ms, faster than 85.09% of Java online submissions for Best Time to Buy and Sell Stock II. |
| 67 | + * |
| 68 | + * Memory Usage: 37 MB, less than 100.00% of Java online submissions for Best Time to Buy and Sell Stock II. |
| 69 | + * |
| 70 | + * 我的思路,别人的代码。 |
| 71 | + */ |
| 72 | + public int maxProfitPeakValleyApproach(int[] prices) { |
| 73 | + if (Objects.isNull(prices) || prices.length == 0) { |
| 74 | + return 0; |
| 75 | + } |
| 76 | + int i = 0; |
| 77 | + int valley = prices[0]; |
| 78 | + int peak = prices[0]; |
| 79 | + int result = 0; |
| 80 | + while (i < prices.length - 1) { |
| 81 | + while (i < prices.length - 1 && prices[i] > prices[i + 1]) { |
| 82 | + i++; |
| 83 | + } |
| 84 | + valley = prices[i]; |
| 85 | + while (i < prices.length - 1 && prices[i] < prices[i++]) { |
| 86 | + i++; |
| 87 | + } |
| 88 | + peak = prices[i]; |
| 89 | + result += peak - valley; |
| 90 | + } |
| 91 | + |
| 92 | + return result; |
| 93 | + } |
| 94 | + |
| 95 | + public static void main(String[] args) { |
| 96 | + BestTimeToBuyAndSellStockIi solution = new BestTimeToBuyAndSellStockIi(); |
| 97 | + |
| 98 | + int[] a1 = {7, 1, 5, 3, 6, 4}; |
| 99 | + int r1 = solution.maxProfit(a1); |
| 100 | + System.out.println((r1 == 7) + " : " + r1); |
| 101 | + |
| 102 | + int[] a2 = {1, 2, 3, 4, 5}; |
| 103 | + int r2 = solution.maxProfit(a2); |
| 104 | + System.out.println((r2 == 4) + " : " + r2); |
| 105 | + |
| 106 | + int[] a3 = {7, 6, 4, 3, 1}; |
| 107 | + int r3 = solution.maxProfit(a3); |
| 108 | + System.out.println((r3 == 0) + " : " + r3); |
| 109 | + } |
| 110 | +} |
0 commit comments