LeetCode 122. Best Time to Buy and Sell Stock II (javascript)
codingpineapple
Posted on May 2, 2021
Description:
You are given an array prices where prices[i] is the price of a given stock on the ith day.
Find the maximum profit you can achieve. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times).
Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).
Solution:
Time Complexity : O(n)
Space Complexity: O(1)
var maxProfit = function(prices) {
let maxprofit = 0;
for (let i = 1; i < prices.length; i++) {
// Add to the max profit total if the current price is greater than previous
// We "sell" when the the opposite is true because we will not buy on days that dip.
if (prices[i] > prices[i - 1])
// Add the increse in price to our profit
maxprofit += prices[i] - prices[i - 1];
}
return maxprofit;
};
π πͺ π
π©
codingpineapple
Posted on May 2, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
sorting Recap the highlight of the sorting algorithms using JavaScript for beginners
October 5, 2024
datastructures DSA with JS: Understanding Custom Array Data Structure in JavaScript - A Step-by-Step Guide
September 29, 2024