Skip to content

Commit a9844d6

Browse files
committed
add: stock
1 parent 1dbfd13 commit a9844d6

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// 시간복잡도: O(n)
2+
// 공간복잡도: O(1)
3+
4+
// 최소값을 계속 갱신하면서 최대 이익을 계산
5+
6+
/**
7+
* @param {number[]} prices
8+
* @return {number}
9+
*/
10+
var maxProfit = function (prices) {
11+
let minPrice = Infinity;
12+
let maxProfit = 0;
13+
14+
for (let price of prices) {
15+
if (price < minPrice) {
16+
minPrice = price;
17+
} else {
18+
maxProfit = Math.max(maxProfit, price - minPrice);
19+
}
20+
}
21+
22+
return maxProfit;
23+
};

0 commit comments

Comments
 (0)