[go: nahoru, domu]

Skip to content

Commit

Permalink
Submit 122_BestTimetoBuyandSellStockII
Browse files Browse the repository at this point in the history
  • Loading branch information
Binlogo committed Jul 10, 2020
1 parent 3660b4b commit 18e5f2a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//: [Previous](@previous)

import Foundation

class Solution {
func maxProfit(_ prices: [Int]) -> Int {
guard !prices.isEmpty else { return 0 }
var maxProfit = 0
for current in 1..<prices.count {
if prices[current] > prices[current - 1] {
maxProfit += prices[current] - prices[current - 1]
}
}
return maxProfit
}
}

// Tests
let s = Solution()
s.maxProfit([7,1,5,3,6,4]) == 7

//: [Next](@next)
1 change: 1 addition & 0 deletions LeetCodePlayground.playground/contents.xcplayground
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,6 @@
<page name='程序员面试金典 - 面试题 16.11. 跳水板'/>
<page name='程序员面试金典 - 面试题 17.13. 恢复空格'/>
<page name='121_BestTimetoBuyandSellStock'/>
<page name='122_BestTimetoBuyandSellStockII'/>
</pages>
</playground>

0 comments on commit 18e5f2a

Please sign in to comment.