notes/OJ notes/pages/Leetcode Best-Time-To-Buy-And-Sell-Stock.md

132 lines
3.2 KiB
Markdown
Raw Normal View History

2022-06-14 23:33:35 +08:00
# Leetcode Best-Time-To-Buy-And-Sell-Stock
#### 2022-06-11
---
2022-09-03 15:41:36 +08:00
2022-06-14 23:33:35 +08:00
##### Data structures:
2022-09-03 15:41:36 +08:00
#DS #array
2022-06-14 23:33:35 +08:00
##### Algorithms:
2022-09-03 15:41:36 +08:00
2022-06-14 23:33:35 +08:00
#algorithm #Kadane_s_algorithm
2022-09-03 15:41:36 +08:00
2022-06-14 23:33:35 +08:00
##### Difficulty:
2022-09-03 15:41:36 +08:00
2022-09-06 20:22:48 +08:00
#leetcode #coding_problem #difficulty_easy
2022-09-03 15:41:36 +08:00
2022-06-14 23:33:35 +08:00
##### Related topics:
2022-09-03 15:41:36 +08:00
2022-06-14 23:33:35 +08:00
##### Links:
2022-09-03 15:41:36 +08:00
2022-06-14 23:33:35 +08:00
- [Link to problem](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/)
- [Kadane's Algo solution](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/solution/554875)
2022-09-03 15:41:36 +08:00
---
2022-06-14 23:33:35 +08:00
### Problem
2022-09-03 15:41:36 +08:00
2022-06-14 23:33:35 +08:00
You are given an array `prices` where `prices[i]` is the price of a given stock on the `ith` day.
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.
Return _the maximum profit you can achieve from this transaction_. If you cannot achieve any profit, return `0`.
#### Examples
2022-09-03 15:41:36 +08:00
2022-06-14 23:33:35 +08:00
Example 1:
```
Input: prices = [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.
```
Example 2:
```
Input: prices = [7,6,4,3,1]
Output: 0
Explanation: In this case, no transactions are done and the max profit = 0.
```
#### Constraints
2022-09-03 15:41:36 +08:00
- 1 <= prices.length <= 105
- 0 <= prices[i] <= 104
2022-06-14 23:33:35 +08:00
### Thoughts
2022-09-03 15:41:36 +08:00
Firstly I thought of brute forcing, which is O(n \* (n-1))
2022-06-14 23:33:35 +08:00
Then, I came up with dynamic programming, but this is still not so optimized
Lastly, from [here](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/solution/554875) I know We can use Kadane's algo.
> [!tip]
> In Kadane's algorithm:
2022-09-03 15:41:36 +08:00
>
2022-06-14 23:33:35 +08:00
> - buyprice = min(buyprice, price[i]) // Achieve best min price
> - profit = max(profit, price[i] - buyprice) // find best profit **==so far==**
To explain that, in each iteration, there are two cases:
2022-09-03 15:41:36 +08:00
2022-06-14 23:33:35 +08:00
- if the current price is the lowest, set the buyprice to the lowest one
- if the current price minus the `buyprice` is bigger tham profit, record that to profit.
So, the magic part happens **at setting the buyprice**, because the best profit will and only will occur **after the lowest buyprice is set**, thus we can find best solution in one run.
### Solution
Time O(2n) Space O(n) solution (inefficient reverse kadane's algorithm)
2022-09-03 15:41:36 +08:00
2022-06-14 23:33:35 +08:00
```cpp
class Solution {
public:
int maxProfit(vector<int> &prices) {
// Dynamic programming. and hash table.
int size = prices.size();
unordered_map<int, int> localmax;
int max = 0;
// populate localmax: max value after this one
// the first one
localmax[size - 1] = prices[size - 1];
for (int i = size - 2; i >= 0; i--) {
localmax[i] = std::max(prices[i + 1], localmax[i + 1]);
}
for (int i = 0; i < size; i++) {
if (localmax[i] - prices[i] > max) {
max = localmax[i] - prices[i];
}
}
return max;
}
};
```
Kadane's algorithm, Time O(n) Space O(1)
2022-09-03 15:41:36 +08:00
2022-06-14 23:33:35 +08:00
```cpp
class Solution {
public:
int maxProfit(vector<int> &prices) {
// Kadane's algorithm
// buyprice == min(buyprice, prices[i])
int buyprice = INT_MAX;
int profit = 0;
for (int i = 0; i < prices.size(); i++) {
buyprice = min(buyprice, prices[i]);
profit = max(prices[i] - buyprice, profit);
}
return profit;
}
};
2022-09-03 15:41:36 +08:00
```