Lintcode 150. Best Time to Buy and Sell Stock II

題目鏈接:https://www.lintcode.com/problem/best-time-to-buy-and-sell-stock-ii/description

/**
 * @param prices: Given an integer array
 * @return: Maximum profit
 */
func maxProfit (prices []int) int {
	// write your code here
	var profit int = 0;
	var i int = 0;
	for i = 1; i < len(prices); i++ {
		var diff int = prices[i] - prices[i - 1];
		if (diff > 0) {
			profit += diff;
		}
	}
	return profit;
}

func main() {
	var prices []int = []int{2,1,2,0,1};
	var profit int = maxProfit(prices);
	fmt.Println(profit);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章