DEV Community

duccanhole
duccanhole

Posted on

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

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

Top comments (0)