-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaxProductSubArray.cpp
52 lines (49 loc) · 1.35 KB
/
maxProductSubArray.cpp
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
45
46
47
48
49
50
51
52
/*
Used divide and conquer approach to solve this problem,
not the most optimal solution
Time complexity - O(N*LogN), each merging part tooks O(N) times the height
of tree.
Space complexity - O(Logn) or O(h) - the height of recursion tree.
*/
class Solution {
int solve(int low,int high,vector<int>& nums)
{
if(low > high)
{
return 1;
}
if(low == high)
{
return nums[low];
}
int mid = low + (high-low)/2;
int left_prod = solve(low,mid,nums);
int right_prod = solve(mid+1,high,nums);
int prod = 1;
// j = mid ---> high
// i = mid - 1 ---> low
int minr1 = 0;
int maxr1 = 0;
for(int j = mid;j<=high;j++)
{
prod = prod * nums[j];
minr1 = min(prod,minr1);
maxr1 = max(prod,maxr1);
}
prod = 1;
int minr2 = 0;
int maxr2 = 0;
for(int i = mid-1;i>=0;i--)
{
prod = prod * nums[i];
minr2 = min(minr2,prod);
maxr2 = max(maxr2,prod);
}
prod = max(max(maxr1,maxr2),max(minr1*minr2,maxr1*maxr2));
return max(max(left_prod,right_prod),prod);
}
public:
int maxProduct(vector<int>& nums) {
return solve(0,nums.size()-1,nums);
}
};