Skip to content

Commit 14eecaf

Browse files
committed
best time to buy and sell stock
1 parent 095850e commit 14eecaf

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+
class Solution {
2+
func maxProfit(_ prices: [Int]) -> Int {
3+
var maxProfit = 0
4+
var minPrice = prices[0]
5+
6+
for i in (1..<prices.count) {
7+
let profit = prices[i] - minPrice
8+
maxProfit = max(profit, maxProfit)
9+
minPrice = min(prices[i], minPrice)
10+
}
11+
12+
return maxProfit
13+
14+
//시간복잡도 O(n)
15+
//공간복잡도 O(1)
16+
}
17+
}
18+

0 commit comments

Comments
 (0)