code every day with me

–DAY 18–

Hi, I am going to make #100DaysOfCode Challenge. Everyday I will try solve 1 problem from leetcode or hackerrank. Hope you can go with me until end.
Now let’s solve problem today:

Problem: Best Time to Buy and Sell Stock
Detail:…


This content originally appeared on DEV Community and was authored by duccanhole

--DAY 18--

Hi, I am going to make #100DaysOfCode Challenge. Everyday I will try solve 1 problem from leetcode or hackerrank. Hope you can go with me until end.
Now let's solve problem today:

  • Problem: Best Time to Buy and Sell Stock
  • Detail: here
  • My solution(javascript):
var maxProfit = function(prices) {
    let profit=0,buy=100000;
    for(let x of prices){
        if(x<buy) buy=x;
        if(x-buy>profit) profit=(x-buy);
    }
    return profit;
};

-->If you have better solution or any question, please comment below. I will appreciate.


This content originally appeared on DEV Community and was authored by duccanhole


Print Share Comment Cite Upload Translate Updates
APA

duccanhole | Sciencx (2021-11-12T03:11:17+00:00) code every day with me. Retrieved from https://www.scien.cx/2021/11/12/code-every-day-with-me-6/

MLA
" » code every day with me." duccanhole | Sciencx - Friday November 12, 2021, https://www.scien.cx/2021/11/12/code-every-day-with-me-6/
HARVARD
duccanhole | Sciencx Friday November 12, 2021 » code every day with me., viewed ,<https://www.scien.cx/2021/11/12/code-every-day-with-me-6/>
VANCOUVER
duccanhole | Sciencx - » code every day with me. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/11/12/code-every-day-with-me-6/
CHICAGO
" » code every day with me." duccanhole | Sciencx - Accessed . https://www.scien.cx/2021/11/12/code-every-day-with-me-6/
IEEE
" » code every day with me." duccanhole | Sciencx [Online]. Available: https://www.scien.cx/2021/11/12/code-every-day-with-me-6/. [Accessed: ]
rf:citation
» code every day with me | duccanhole | Sciencx | https://www.scien.cx/2021/11/12/code-every-day-with-me-6/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.