Skip to content

Commit 9c736ad

Browse files
author
이호찬
committed
best-time-to-buy-and-sell-stock solution
1 parent cf65e7b commit 9c736ad

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @param {number[]} prices
3+
* @return {number}
4+
*/
5+
var maxProfit = function(prices) {
6+
let minPrice = Number.MAX_SAFE_INTEGER;
7+
let maxProfit = 0;
8+
9+
for (let i = 0; i < prices.length; i++) {
10+
minPrice = Math.min(minPrice, prices[i]);
11+
maxProfit = Math.max(maxProfit, prices[i] - minPrice);
12+
}
13+
14+
return maxProfit;
15+
};

0 commit comments

Comments
 (0)