
Leetcode Problem 122: Best Time to Buy and Sell Stock II
You are given an array prices where prices[i] is the price of a given stock on the i-th day. You can buy and sell the stock multiple times, but you must sell the stock before you buy again. Return the maximum profit you can achieve from these transactions.
function maxProfit2(prices: number[]): number {
let maxProfit = 0
for (let i = 1; i < prices.length; i++) {
if (prices[i] > prices[i - 1]) {
maxProfit += prices[i] - prices[i - 1]
}
}
return maxProfit
}
console.log(maxProfit2([7,1,5,3,6,4]))