diff --git a/pkg/best_time_to_buy_and_sell_stock/main.go b/pkg/best_time_to_buy_and_sell_stock/main.go new file mode 100644 index 0000000..a9003c7 --- /dev/null +++ b/pkg/best_time_to_buy_and_sell_stock/main.go @@ -0,0 +1,11 @@ +package besttimetobuyandsellstock + +func MaxProfit(prices []int) int { + buy, sell := 1_000_000_000, 0 + + for _, price := range prices { + buy, sell = min(buy, price), max(sell, price-buy) + } + + return sell +}