-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathmaximumProduct.java
65 lines (53 loc) · 1.48 KB
/
maximumProduct.java
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
53
54
55
56
57
58
59
60
61
62
63
64
65
public class Solution {
/*
Trace the algorithm with this example: -8, -4, -2, 0, 1.
Identify 3 Maximum numbers: -2, 0, 1
2 Smallest numbers: -8, -4
max1 * max2 * max3 = 0
min1 * min2 * max1 = -8 * -4 * 1 = 32 -> Return as answer
*/
// Time O(NLogN) Space O(N) - Because of searching
public int maximumProduct(int[] nums)
{
Arrays.sort(nums);
int len = nums.length - 1;
int end = nums[len-2] * nums[len-1] * nums[len];
int start = nums[0] * nums[1] * nums[len];
return Math.max(end, start);
}
}
// Solution 2
//Time O(N) Space O(1)
public class Solution {
public int maximumProduct(int[] nums)
{
int max1 = Integer.MIN_VALUE, max2 = Integer.MIN_VALUE, max3 = max1;
int min1 = Integer.MAX_VALUE, min2 = min1;
for(int n : nums)
{
if(n < min1)
{
min2 = min1;
min1 = n;
}
else if ( n < min2)
min2 = n;
if(n > max1)
{
max3 = max2;
max2 = max1;
max1 = n;
}
else if(n > max2)
{
max3 = max2;
max2 = n;
}
else if(n > max3)
{
max3 = n;
}
}
return max1 * Math.max(min1 * min2, max2 * max3);
}
}