DEV Community

codingpineapple
codingpineapple

Posted on

3 2

LeetCode 122. Best Time to Buy and Sell Stock II (javascript)

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;
};
Enter fullscreen mode Exit fullscreen mode

SurveyJS custom survey software

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

Top comments (7)

Collapse
 
aleksandar15 profile image
Aleksandar15

A working solution from the one non-working above (JAVASCRIPT):

const maxPorfit = (prices) => {
let maxProfit = 0
    minPrice= prices[0]
    for(let sell = 1; sell < prices.length; sell++) {
        let sellPrice = prices[sell]
        profit = sellPrice - minPrice

        maxProfit = Math.max(maxProfit, profit)
        if(sellPrice < minPrice) minPrice = sellPrice
    }
    return maxProfit
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
aleksandar15 profile image
Aleksandar15

NOTE: on line 3 and line 6 I didn't write "let" to declare a variable, because I declared one prior to it so I didn't need to use "let" again, but for clarification you can use the "let" keyword and it will work the same.

Collapse
 
kaodik profile image
kao

Original post is valid I have tested it.

Collapse
 
qurashieman profile image
Tuba Inam

I found that solution is very popular and helpful : youtube.com/watch?v=HOLeZ5ct2h4

Collapse
 
aleksandar15 profile image
Aleksandar15

You refer to Java youtube solution on a topic of javascript , whats wrong with this post...
Are u a pseudo-coder and not real developer, or a developer who knows both java and javascript so u mistaken it one for the other ? Like how otherwise?!

Collapse
 
aleksandar15 profile image
Aleksandar15

This code is unfinished nor is it giving the correct output?
What was the purpose of this post, have u tested it urself?

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay