https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
// https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
package main
func main() {
prices := []int{7, 1, 5, 3, 6, 4}
// prices = []int{7, 6, 4, 3, 1}
maxProfit(prices)
}
// 제출 코드
func maxProfit(prices []int) int {
min := prices[0]
result := 0
for i := 1; i < len(prices); i++ {
if prices[i] < min {
min = prices[i]
} else {
tmp := prices[i] - min
if tmp > result {
result = tmp
}
}
}
return result
}
'Algorithm > Go' 카테고리의 다른 글
[LeetCode] climbing Stairs (2) | 2022.02.07 |
---|---|
[LeetCode] Remove Element (0) | 2022.01.31 |
[LeetCode] Adding Spaces to a String (0) | 2022.01.24 |
[LeetCode] Maximum Subarray (0) | 2022.01.10 |
[LeetCode] Longest Common Prefix (0) | 2022.01.09 |