Skip to content

Commit

Permalink
Merge pull request #443 from kim-young/main
Browse files Browse the repository at this point in the history
  • Loading branch information
kim-young authored Sep 14, 2024
2 parents ea9b6b4 + 1c6cef0 commit 7109b99
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions best-time-to-buy-and-sell-stock/kimyoung.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 left = 0, right = 1;
let max = 0;

while(right < prices.length) {
if(prices[right] < prices[left]) {
left = right;
} else {
max = Math.max(max, prices[right] - prices[left]);
}
right++;
}
return max;
};

// time - O(n) iterate through prices once
// space - O(1)

0 comments on commit 7109b99

Please sign in to comment.