Advertisement
HXXXXJ

152. Maximum Product Subarray

Feb 15th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.41 KB | None | 0 0
  1.     func maxProduct(_ nums: [Int]) -> Int {
  2.         var minSub = 1
  3.         var maxSub = 1
  4.         var res = Int.min
  5.         for (index, n) in nums.enumerated(){
  6.             let possible1 = n * minSub
  7.             let possible2 = n * maxSub
  8.             minSub = min(n , possible1, possible2)
  9.             maxSub = max(n , possible1, possible2)
  10.             res = max(res , maxSub)
  11.         }
  12.         return res
  13.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement