Skip to content

Commit 44c8ea7

Browse files
committed
Solution Best Time to Buy and Sell Stock
1 parent cdd1826 commit 44c8ea7

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
func maxProfit(_ prices: [Int]) -> Int {
3+
guard var anchor = prices.first, prices.count > 1 else {
4+
return 0
5+
}
6+
7+
var result = 0
8+
for price in prices {
9+
if price < anchor {
10+
anchor = price
11+
} else if price - anchor > result {
12+
result = price - anchor
13+
}
14+
}
15+
16+
return result
17+
}
18+
}

0 commit comments

Comments
 (0)