Skip to content

Commit 8140869

Browse files
authored
Merge pull request #872 from bus710/week05
[bus710] week 05
2 parents d4b8b1f + 6906deb commit 8140869

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
2+
3+
package hello
4+
5+
func maxProfit(prices []int) int {
6+
min := prices[0]
7+
maxProfit := 0
8+
9+
for i := 1; i < len(prices); i++ {
10+
if prices[i] < min {
11+
min = prices[i]
12+
}
13+
if (prices[i] - min) > maxProfit {
14+
maxProfit = prices[i] - min
15+
}
16+
}
17+
18+
return maxProfit
19+
}

0 commit comments

Comments
 (0)