[Swift]LeetCode122. 買賣股票的最佳時機 II | Best Time to Buy and Sell Stock II

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-ewewjqeb-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

Say you have an array for which the ith element is the price of a given stock on day i.git

Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times).github

Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).算法

Example 1:數組

Input: [7,1,5,3,6,4]
Output: 7
Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.
             Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.

Example 2:微信

Input: [1,2,3,4,5]
Output: 4
Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
             Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are
             engaging multiple transactions at the same time. You must sell before buying again.

Example 3:this

Input: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.

給定一個數組,它的第 i 個元素是一支給定股票第 i 天的價格。spa

設計一個算法來計算你所能獲取的最大利潤。你能夠儘量地完成更多的交易(屢次買賣一支股票)。設計

注意:你不能同時參與多筆交易(你必須在再次購買前出售掉以前的股票)。code

示例 1:

輸入: [7,1,5,3,6,4]
輸出: 7
解釋: 在第 2 天(股票價格 = 1)的時候買入,在第 3 天(股票價格 = 5)的時候賣出, 這筆交易所能得到利潤 = 5-1 = 4 。
     隨後,在第 4 天(股票價格 = 3)的時候買入,在第 5 天(股票價格 = 6)的時候賣出, 這筆交易所能得到利潤 = 6-3 = 3 。

示例 2:

輸入: [1,2,3,4,5]
輸出: 4
解釋: 在第 1 天(股票價格 = 1)的時候買入,在第 5 天 (股票價格 = 5)的時候賣出, 這筆交易所能得到利潤 = 5-1 = 4 。
     注意你不能在第 1 天和第 2 天接連購買股票,以後再將它們賣出。
     由於這樣屬於同時參與了多筆交易,你必須在再次購買前出售掉以前的股票。

示例 3:

輸入: [7,6,4,3,1]
輸出: 0
解釋: 在這種狀況下, 沒有交易完成, 因此最大利潤爲 0。

 1 class Solution {
 2     func maxProfit(_ prices: [Int]) -> Int {
 3         if prices == nil || prices.count == 0
 4         {
 5             return 0
 6         }
 7         var res:Int = 0
 8         var n:Int = prices.count
 9         for i in 0..<n - 1
10         {
11             if prices[i] < prices[i + 1]
12             {
13                 res += (prices[i + 1] - prices[i])
14             }
15         }
16         return res
17     }
18 }

16ms

 1 class Solution {
 2     func maxProfit(_ prices: [Int]) -> Int {
 3         var max = 0
 4     guard prices.count > 1 else {
 5         return max
 6     }
 7     for i in 1..<prices.count where prices[i] > prices[i - 1] {
 8         max += prices[i] - prices[i - 1]
 9     }
10     return max
11     }
12 }

12ms

 1 class Solution {
 2     func maxProfit(_ prices: [Int]) -> Int {
 3         if prices.count <= 1 {
 4             return 0
 5         }
 6         var maxProfit = 0
 7         for i in 1..<prices.count {
 8             maxProfit += max(0, prices[i] - prices[i - 1])
 9         }
10         
11         return maxProfit
12     }
13 }
相關文章
相關標籤/搜索