forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path_628.java
26 lines (23 loc) · 757 Bytes
/
_628.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
package com.fishercoder.solutions;
import java.util.Arrays;
public class _628 {
public static class Solution1 {
public int maximumProduct(int[] nums) {
Arrays.sort(nums);
int product = 1;
if (nums.length >= 3) {
for (int i = nums.length - 1; i >= nums.length - 3; i--) {
product *= nums[i];
}
int anotherProduct = nums[0] * nums
[1] * nums[nums.length - 1];
product = Math.max(product, anotherProduct);
} else {
for (int i = 0; i < nums.length; i++) {
product *= nums[i];
}
}
return product;
}
}
}