|
| 1 | +## 1. Brute Force |
| 2 | + |
| 3 | +::tabs-start |
| 4 | + |
| 5 | +```python |
| 6 | +class Solution: |
| 7 | + def maxProfit(self, prices: List[int]) -> int: |
| 8 | + res = 0 |
| 9 | + for i in range(len(prices)): |
| 10 | + buy = prices[i] |
| 11 | + for j in range(i + 1, len(prices)): |
| 12 | + sell = prices[j] |
| 13 | + res = max(res, sell - buy) |
| 14 | + return res |
| 15 | +``` |
| 16 | + |
| 17 | +```java |
| 18 | +public class Solution { |
| 19 | + public int maxProfit(int[] prices) { |
| 20 | + int res = 0; |
| 21 | + for (int i = 0; i < prices.length; i++) { |
| 22 | + int buy = prices[i]; |
| 23 | + for (int j = i + 1; j < prices.length; j++) { |
| 24 | + int sell = prices[j]; |
| 25 | + res = Math.max(res, sell - buy); |
| 26 | + } |
| 27 | + } |
| 28 | + return res; |
| 29 | + } |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +```cpp |
| 34 | +class Solution { |
| 35 | +public: |
| 36 | + int maxProfit(vector<int>& prices) { |
| 37 | + int res = 0; |
| 38 | + for (int i = 0; i < prices.size(); i++) { |
| 39 | + int buy = prices[i]; |
| 40 | + for (int j = i + 1; j < prices.size(); j++) { |
| 41 | + int sell = prices[j]; |
| 42 | + res = max(res, sell - buy); |
| 43 | + } |
| 44 | + } |
| 45 | + return res; |
| 46 | + } |
| 47 | +}; |
| 48 | +``` |
| 49 | +
|
| 50 | +```javascript |
| 51 | +class Solution { |
| 52 | + /** |
| 53 | + * @param {number[]} prices |
| 54 | + * @return {number} |
| 55 | + */ |
| 56 | + maxProfit(prices) { |
| 57 | + let res = 0; |
| 58 | + for (let i = 0; i < prices.length; i++) { |
| 59 | + let buy = prices[i]; |
| 60 | + for (let j = i + 1; j < prices.length; j++) { |
| 61 | + let sell = prices[j]; |
| 62 | + res = Math.max(res, sell - buy); |
| 63 | + } |
| 64 | + } |
| 65 | + return res; |
| 66 | + } |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +```csharp |
| 71 | +public class Solution { |
| 72 | + public int MaxProfit(int[] prices) { |
| 73 | + int res = 0; |
| 74 | + for (int i = 0; i < prices.Length; i++) { |
| 75 | + int buy = prices[i]; |
| 76 | + for (int j = i + 1; j < prices.Length; j++) { |
| 77 | + int sell = prices[j]; |
| 78 | + res = Math.Max(res, sell - buy); |
| 79 | + } |
| 80 | + } |
| 81 | + return res; |
| 82 | + } |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +::tabs-end |
| 87 | + |
| 88 | +### Time & Space Complexity |
| 89 | + |
| 90 | +* Time complexity: $O(n ^ 2)$ |
| 91 | +* Space complexity: $O(1)$ |
| 92 | + |
| 93 | +--- |
| 94 | + |
| 95 | +## 2. Two Pointers |
| 96 | + |
| 97 | +::tabs-start |
| 98 | + |
| 99 | +```python |
| 100 | +class Solution: |
| 101 | + def maxProfit(self, prices: List[int]) -> int: |
| 102 | + l, r = 0, 1 |
| 103 | + maxP = 0 |
| 104 | + |
| 105 | + while r < len(prices): |
| 106 | + if prices[l] < prices[r]: |
| 107 | + profit = prices[r] - prices[l] |
| 108 | + maxP = max(maxP, profit) |
| 109 | + else: |
| 110 | + l = r |
| 111 | + r += 1 |
| 112 | + return maxP |
| 113 | +``` |
| 114 | + |
| 115 | +```java |
| 116 | +public class Solution { |
| 117 | + public int maxProfit(int[] prices) { |
| 118 | + int l = 0, r = 1; |
| 119 | + int maxP = 0; |
| 120 | + |
| 121 | + while (r < prices.length) { |
| 122 | + if (prices[l] < prices[r]) { |
| 123 | + int profit = prices[r] - prices[l]; |
| 124 | + maxP = Math.max(maxP, profit); |
| 125 | + } else { |
| 126 | + l = r; |
| 127 | + } |
| 128 | + r++; |
| 129 | + } |
| 130 | + return maxP; |
| 131 | + } |
| 132 | +} |
| 133 | +``` |
| 134 | + |
| 135 | +```cpp |
| 136 | +class Solution { |
| 137 | +public: |
| 138 | + int maxProfit(vector<int>& prices) { |
| 139 | + int l = 0, r = 1; |
| 140 | + int maxP = 0; |
| 141 | + |
| 142 | + while (r < prices.size()) { |
| 143 | + if (prices[l] < prices[r]) { |
| 144 | + int profit = prices[r] - prices[l]; |
| 145 | + maxP = max(maxP, profit); |
| 146 | + } else { |
| 147 | + l = r; |
| 148 | + } |
| 149 | + r++; |
| 150 | + } |
| 151 | + return maxP; |
| 152 | + } |
| 153 | +}; |
| 154 | +``` |
| 155 | + |
| 156 | +```javascript |
| 157 | +class Solution { |
| 158 | + /** |
| 159 | + * @param {number[]} prices |
| 160 | + * @return {number} |
| 161 | + */ |
| 162 | + maxProfit(prices) { |
| 163 | + let l = 0, r = 1; |
| 164 | + let maxP = 0; |
| 165 | + |
| 166 | + while (r < prices.length) { |
| 167 | + if (prices[l] < prices[r]) { |
| 168 | + let profit = prices[r] - prices[l]; |
| 169 | + maxP = Math.max(maxP, profit); |
| 170 | + } else { |
| 171 | + l = r; |
| 172 | + } |
| 173 | + r++; |
| 174 | + } |
| 175 | + return maxP; |
| 176 | + } |
| 177 | +} |
| 178 | +``` |
| 179 | + |
| 180 | +```csharp |
| 181 | +public class Solution { |
| 182 | + public int MaxProfit(int[] prices) { |
| 183 | + int l = 0, r = 1; |
| 184 | + int maxP = 0; |
| 185 | + |
| 186 | + while (r < prices.Length) { |
| 187 | + if (prices[l] < prices[r]) { |
| 188 | + int profit = prices[r] - prices[l]; |
| 189 | + maxP = Math.Max(maxP, profit); |
| 190 | + } else { |
| 191 | + l = r; |
| 192 | + } |
| 193 | + r++; |
| 194 | + } |
| 195 | + return maxP; |
| 196 | + } |
| 197 | +} |
| 198 | +``` |
| 199 | + |
| 200 | +::tabs-end |
| 201 | + |
| 202 | +### Time & Space Complexity |
| 203 | + |
| 204 | +* Time complexity: $O(n)$ |
| 205 | +* Space complexity: $O(1)$ |
| 206 | + |
| 207 | +--- |
| 208 | + |
| 209 | +## 3. Dynamic Programming |
| 210 | + |
| 211 | +::tabs-start |
| 212 | + |
| 213 | +```python |
| 214 | +class Solution: |
| 215 | + def maxProfit(self, prices: List[int]) -> int: |
| 216 | + maxP = 0 |
| 217 | + minBuy = prices[0] |
| 218 | + |
| 219 | + for sell in prices: |
| 220 | + maxP = max(maxP, sell - minBuy) |
| 221 | + minBuy = min(minBuy, sell) |
| 222 | + return maxP |
| 223 | +``` |
| 224 | + |
| 225 | +```java |
| 226 | +public class Solution { |
| 227 | + public int maxProfit(int[] prices) { |
| 228 | + int maxP = 0; |
| 229 | + int minBuy = prices[0]; |
| 230 | + |
| 231 | + for (int sell : prices) { |
| 232 | + maxP = Math.max(maxP, sell - minBuy); |
| 233 | + minBuy = Math.min(minBuy, sell); |
| 234 | + } |
| 235 | + return maxP; |
| 236 | + } |
| 237 | +} |
| 238 | +``` |
| 239 | + |
| 240 | +```cpp |
| 241 | +class Solution { |
| 242 | +public: |
| 243 | + int maxProfit(vector<int>& prices) { |
| 244 | + int maxP = 0; |
| 245 | + int minBuy = prices[0]; |
| 246 | + |
| 247 | + for (int& sell : prices) { |
| 248 | + maxP = max(maxP, sell - minBuy); |
| 249 | + minBuy = min(minBuy, sell); |
| 250 | + } |
| 251 | + return maxP; |
| 252 | + } |
| 253 | +}; |
| 254 | +``` |
| 255 | + |
| 256 | +```javascript |
| 257 | +class Solution { |
| 258 | + /** |
| 259 | + * @param {number[]} prices |
| 260 | + * @return {number} |
| 261 | + */ |
| 262 | + maxProfit(prices) { |
| 263 | + let maxP = 0; |
| 264 | + let minBuy = prices[0]; |
| 265 | + |
| 266 | + for (let sell of prices) { |
| 267 | + maxP = Math.max(maxP, sell - minBuy); |
| 268 | + minBuy = Math.min(minBuy, sell); |
| 269 | + } |
| 270 | + return maxP; |
| 271 | + } |
| 272 | +} |
| 273 | +``` |
| 274 | + |
| 275 | +```csharp |
| 276 | +public class Solution { |
| 277 | + public int MaxProfit(int[] prices) { |
| 278 | + int maxP = 0; |
| 279 | + int minBuy = prices[0]; |
| 280 | + |
| 281 | + foreach (int sell in prices) { |
| 282 | + maxP = Math.Max(maxP, sell - minBuy); |
| 283 | + minBuy = Math.Min(minBuy, sell); |
| 284 | + } |
| 285 | + return maxP; |
| 286 | + } |
| 287 | +} |
| 288 | +``` |
| 289 | + |
| 290 | +::tabs-end |
| 291 | + |
| 292 | +### Time & Space Complexity |
| 293 | + |
| 294 | +* Time complexity: $O(n)$ |
| 295 | +* Space complexity: $O(1)$ |
0 commit comments