import "fmt"
//2.设计算法
//这个算法本质:当明天的价格比今天的价格贵的时候我们今天买,
// 明天卖,这样能够获取最大利润
//类似股票图,只要把所有的股市增长的全部加起来就行
func maxProfit(prices []int) int {
profit := 0
for i := 0; i < len(prices)-1; i++ {
if prices[i] < prices[i+1] {
profit += prices[i+1] - prices[i]
}
}
return profit
}
func main() {
prices := []int{7,1,5,3,6,4}
profit := maxProfit(prices)
fmt.Println(profit)
}
Output:
7