Problem List

Best Time To Buy And Sell Stock

March 03, 2025Go array, dynamic programming

Go Solution
func maxProfit(prices []int) int {
	buy := prices[0]
    maxProfit := 0
	for _, price := range prices {
		if price < buy {
			buy = price
		} else {
            profit := price - buy
            if profit > maxProfit {
                maxProfit = profit
            }
		}
	}
	return maxProfit
}
LeetCode Problem Link