You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
1. If array can have at most one negative, we can take same approach as #053 Maximum subarray
10
+
1. If array can have at most one negative, we can take same approach as Kadane's Algo in #053 Maximum subarray
11
11
2. When there are more then one negative number in sub-array,
12
12
1. Positive number multiplication with negative number will make it negative
13
13
2. Negative number multiplication with negative number will make it positive
14
-
3. In that case need to tack, calculate and get min and max both for each element
14
+
3. There are two approach
15
+
1. First find the max product via kadane's algo form left to right and then max from right to left
16
+
1. return the max of both
17
+
2. track min and max
15
18
19
+
Approach #1 : Loop from both side in Kadane algo
20
+
1. Loop from left to right
21
+
1. find currProduct of currMax * n
22
+
2. find max of currProduct and currMax
23
+
3. if currProduct ==0, rest to 1
24
+
2. Loop from right to left and repeat above steps
25
+
3. compare the max of both the loop and return the max
16
26
17
-
Approach
27
+
28
+
Approach #2 : Track max and min in Kadane algo
18
29
1. track max, min. Initialize it with 1
19
30
2. track result and init with nums[0]
20
31
3. for each number in array
21
-
1. store temp = max * n // `because max will change after calculating the max`
32
+
1. store temp = max // `because max will change after calculating the max`
22
33
2. calculate max via max(max *n, min *n, n)
23
34
3. calculate min via min(temp * n, min * n, n)
24
35
4. calculate res = max(res, max)
25
36
4. return res
26
37
27
38
28
39
40
+
41
+
42
+
29
43
# 238. Product of Array Except Self
30
44
Intuition: at every element, product to array except self will be the product of all the element before that element and product of all the element after that element.
0 commit comments