diff --git a/best-time-to-buy-and-sell-stock/bus710.go b/best-time-to-buy-and-sell-stock/bus710.go new file mode 100644 index 000000000..b02f2ef3d --- /dev/null +++ b/best-time-to-buy-and-sell-stock/bus710.go @@ -0,0 +1,19 @@ +// https://leetcode.com/problems/best-time-to-buy-and-sell-stock/ + +package hello + +func maxProfit(prices []int) int { + min := prices[0] + maxProfit := 0 + + for i := 1; i < len(prices); i++ { + if prices[i] < min { + min = prices[i] + } + if (prices[i] - min) > maxProfit { + maxProfit = prices[i] - min + } + } + + return maxProfit +}