Skip to content

Commit

Permalink
feat: best time to buy and sell stock
Browse files Browse the repository at this point in the history
  • Loading branch information
anniemon committed Jan 8, 2025
1 parent 7ae7503 commit f5afe89
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions best-time-to-buy-and-sell-stock/anniemon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* 시간 복잡도: prices.length만큼 순회하므로 O(n)
* 공간 복잡도: 상수 크기의 변수만 사용하므로 O(1)
*/
/**
* @param {number[]} prices
* @return {number}
*/
var maxProfit = function (prices) {
let maxProfit = 0;
let min = prices[0];

for (let i = 0; i < prices.length; i++) {
maxProfit = Math.max(maxProfit, prices[i] - min);
min = Math.min(min, prices[i]);
}
return maxProfit;
};

0 comments on commit f5afe89

Please sign in to comment.