122-best-time-to-buy-and-sell-stock-ii
Question
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
Example:
Thought Process
- First we need to find the local min, then the next local max
- repeat the local min and max process until we reach the end
- Time complexity O(n)
- Space complexity O(1)
- Greedy approach
- Instead of trying to find local min and local max, we can pretend that we can buy and sell at the same day, this transaction can be treated as gathering profit as we go
- Anytime that the stock price is higher than the yesterday's, we gather out profit, this is essentially same as the buy at the lowest price and sell at the highest price
- Time complexity O(n)
- Space complexity O(1)
Solution
class Solution {
public int maxProfit(int[] prices) {
int profit = 0;
for (int i = 1; i < prices.length; i++){
if (prices[i] > prices[i - 1]) profit += prices[i] - prices[i -1];
}
return profit;
}
}