|
| 1 | +/* |
| 2 | +
|
| 3 | + -* Best Time to Buy and Sell Stock *- |
| 4 | +
|
| 5 | +You are given an array prices where prices[i] is the price of a given stock on the ith day. |
| 6 | +
|
| 7 | +You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock. |
| 8 | +
|
| 9 | +Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0. |
| 10 | +
|
| 11 | +
|
| 12 | +
|
| 13 | +Example 1: |
| 14 | +
|
| 15 | +Input: prices = [7,1,5,3,6,4] |
| 16 | +Output: 5 |
| 17 | +Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5. |
| 18 | +Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell. |
| 19 | +Example 2: |
| 20 | +
|
| 21 | +Input: prices = [7,6,4,3,1] |
| 22 | +Output: 0 |
| 23 | +Explanation: In this case, no transactions are done and the max profit = 0. |
| 24 | +
|
| 25 | +
|
| 26 | +Constraints: |
| 27 | +
|
| 28 | +1 <= prices.length <= 105 |
| 29 | +0 <= prices[i] <= 104 |
| 30 | +
|
| 31 | +
|
| 32 | +*/ |
| 33 | + |
| 34 | +import 'dart:math'; |
| 35 | + |
| 36 | +class Solution { |
| 37 | +// Runtime: 696 ms, faster than 27.03% of Dart online submissions for Best Time to Buy and Sell Stock. |
| 38 | +// Memory Usage: 186.5 MB, less than 56.76% of Dart online submissions for Best Time to Buy and Sell Stock. |
| 39 | + /* |
| 40 | + Approach |
| 41 | + Follow the steps below to implement the above idea: |
| 42 | + Declare a buy variable to store the buy cost and maxProfit to store the maximum profit. |
| 43 | + Initialize the buy variable to the first element of the prices array. |
| 44 | + Iterate over the prices array and check if the current price is minimum or not. |
| 45 | + If the current price is minimum then buy on this ith day. |
| 46 | + If the current price is greater than the previous buy then make profit from it and maximize the maxProfit. |
| 47 | + Finally, return the maxProfit. |
| 48 | +
|
| 49 | + */ |
| 50 | + int maxProfit(List<int> prices) { |
| 51 | + int buy = 0; |
| 52 | + int maxProfit = 0; |
| 53 | + buy = prices[0]; |
| 54 | + for (int i = 0; i < prices.length; i++) { |
| 55 | + if (prices[i] < buy) { |
| 56 | + buy = prices[i]; |
| 57 | + } else if (prices[i] - buy > maxProfit) { |
| 58 | + maxProfit = prices[i] - buy; |
| 59 | + } |
| 60 | + } |
| 61 | + return maxProfit; |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +class B { |
| 66 | + int maxProfit(List<int> prices) { |
| 67 | + int maxPro = 0, mn = 100050450; |
| 68 | + |
| 69 | + for (int i = 0; i < prices.length; i++) { |
| 70 | + mn = min(mn, prices[i]); |
| 71 | + maxPro = max(maxPro, prices[i] - mn); |
| 72 | + } |
| 73 | + return maxPro; |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +// Massive Stack Overflow |
| 78 | +class C { |
| 79 | + int findMaximumProfit( |
| 80 | + List<int> prices, int i, int k, int buy, List<List<int>> v) { |
| 81 | + // If no stock can be chosen |
| 82 | + if (i >= prices.length || k <= 0) return 0; |
| 83 | + |
| 84 | + if (v[i][buy] != -1) return v[i][buy]; |
| 85 | + |
| 86 | + // If a stock is already bought |
| 87 | + // Buy now |
| 88 | + int nbuy; |
| 89 | + if (buy == 1) |
| 90 | + nbuy = 0; |
| 91 | + else |
| 92 | + nbuy = 1; |
| 93 | + if (buy == 1) { |
| 94 | + // return v[i][buy] = max( |
| 95 | + // -prices[i] + findMaximumProfit(prices, i + 1, k, nbuy, v), |
| 96 | + // findMaximumProfit(prices, i + 1, k, buy, v)); |
| 97 | + return v[i][buy] = max( |
| 98 | + (-prices[i] + findMaximumProfit(prices, i + 1, k, nbuy, v)).toInt(), |
| 99 | + findMaximumProfit(prices, i + 1, k, buy, v)); |
| 100 | + } |
| 101 | + |
| 102 | + // Otherwise |
| 103 | + else { |
| 104 | + // Buy now |
| 105 | + if (buy == 1) |
| 106 | + nbuy = 0; |
| 107 | + else |
| 108 | + nbuy = 1; |
| 109 | + return v[i][buy] = max( |
| 110 | + (prices[i] + findMaximumProfit(prices, i + 1, k - 1, nbuy, v)) |
| 111 | + .toInt(), |
| 112 | + findMaximumProfit(prices, i + 1, k, buy, v)); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + int maxProfit(List<int> prices) { |
| 117 | + int n = prices.length; |
| 118 | + // let v = new Array(n).fill(0).map(() => new Array(2).fill(-1)) |
| 119 | + var v = List.filled(n, 0).map((e) => List.filled(2, -1)).toList(); |
| 120 | + |
| 121 | + // buy = 1 because atmost one |
| 122 | + // transaction is allowed |
| 123 | + return findMaximumProfit(prices, 0, 1, 1, v); |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +class D { |
| 128 | + int findMaximumProfit(int i, int k, List<int> prices, List<List<int>> dp) { |
| 129 | + if (i == prices.length) return 0; |
| 130 | + if (dp[i][k] != -1) return dp[i][k]; |
| 131 | + int profit = 0; |
| 132 | + if (k == 1) { |
| 133 | + int buy = -prices[i] + findMaximumProfit(i + 1, 0, prices, dp); |
| 134 | + int notBuy = findMaximumProfit(i + 1, 1, prices, dp); |
| 135 | + profit = max(buy, notBuy); |
| 136 | + } else { |
| 137 | + int sell = prices[i] + findMaximumProfit(i + 1, 1, prices, dp); |
| 138 | + int notSell = findMaximumProfit(i + 1, 0, prices, dp); |
| 139 | + profit = max(sell, notSell); |
| 140 | + } |
| 141 | + |
| 142 | + return dp[i][k] = profit; |
| 143 | + } |
| 144 | + |
| 145 | + int maxProfit(List<int> prices) { |
| 146 | + int n = prices.length; |
| 147 | + List<List<int>> dp = |
| 148 | + List.filled(n, 0).map((e) => List.filled(2, -1)).toList(); |
| 149 | + |
| 150 | + return findMaximumProfit(0, 1, prices, dp); |
| 151 | + } |
| 152 | +} |
0 commit comments