forked from yuyongwei/Algorithms-In-Swift
-
Notifications
You must be signed in to change notification settings - Fork 1
/
maximumProductSubArray.swift
44 lines (32 loc) · 976 Bytes
/
maximumProductSubArray.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
Given an integer array nums, find the contiguous subarray within an array (containing at least one number) which has the largest product.
Example 1:
Input: [2,3,-2,4]
Output: 6
Explanation: [2,3] has the largest product 6.
Example 2:
Input: [-2,0,-1]
Output: 0
Explanation: The result cannot be 2, because [-2,-1] is not a subarray.
https://leetcode.com/problems/maximum-product-subarray/
*/
func maxProduct(_ nums: [Int]) -> Int {
guard nums.count > 0 else { return 0 }
var memo = [[Int]]()
let n = nums.count
memo.append([nums[n-1]])
var max = nums[n-1]
for i in (0..<n-1).reversed() {
let current = nums[i]
if current > max { max = current }
var temp = [current]
if current == 1 { continue }
for v in memo.last! {
let mul = current * v
if mul > max { max = mul }
temp.append(current * v)
}
memo.append(temp)
}
return max
}