diff --git a/best-time-to-buy-and-sell-stock/sol.go b/best-time-to-buy-and-sell-stock/sol.go new file mode 100644 index 0000000..28fad5a --- /dev/null +++ b/best-time-to-buy-and-sell-stock/sol.go @@ -0,0 +1,15 @@ +package main + +// Time: O(n) +// Space: O(1) +func maxProfit(prices []int) int { + min, max := prices[0], 0 + for _, price := range prices { + if price < min { + min = price + } else if diff := price - min; diff > max { + max = diff + } + } + return max +}