Skip to content

Commit

Permalink
added JavaScript solution to #121
Browse files Browse the repository at this point in the history
  • Loading branch information
Shuhua-L committed Oct 18, 2023
1 parent 3537ea7 commit a2b5a76
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions Easy/121. Best Time to buy and sell stock/Solutioin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* @param {number[]} prices
* @return {number}
*/
var maxProfit = function (prices) {
let l = 0;
let r = 1;
let max = 0;
let profit = 0;

while (r < prices.length) {
profit = prices[r] - prices[l];
if (profit > 0) {
max = profit > max ? profit : max;
} else {
l = r;
}
r++;
}
return max;
};

0 comments on commit a2b5a76

Please sign in to comment.