Skip to content

Commit 9100c95

Browse files
committed
Add best time to buy and sell stock solution
1 parent edddfe9 commit 9100c95

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* 시간 복잡도: O(n)
3+
* 공간 복잡도: O(1)
4+
*/
5+
6+
class Solution {
7+
fun maxProfit(prices: IntArray): Int {
8+
var minPrice = Int.MAX_VALUE
9+
var maxProfit = 0
10+
11+
prices.forEach { price ->
12+
minPrice = minOf(minPrice, price)
13+
maxProfit = maxOf(maxProfit, price - minPrice)
14+
}
15+
16+
return maxProfit
17+
}
18+
}

0 commit comments

Comments
 (0)