Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 6a4b7c1

Browse files
refactor 628
1 parent f39943e commit 6a4b7c1

File tree

2 files changed

+52
-22
lines changed

2 files changed

+52
-22
lines changed

src/main/java/com/fishercoder/solutions/_628.java

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,23 @@
2020
*/
2121
public class _628 {
2222

23-
public int maximumProduct(int[] nums) {
24-
Arrays.sort(nums);
25-
int product = 1;
26-
if (nums.length >= 3) {
27-
for (int i = nums.length - 1; i >= nums.length - 3; i--) {
28-
product *= nums[i];
29-
}
30-
int anotherProduct = nums[0] * nums
31-
[1] * nums[nums.length - 1];
32-
product = Math.max(product, anotherProduct);
33-
} else {
34-
for (int i = 0; i < nums.length; i++) {
35-
product *= nums[i];
23+
public static class Solution1 {
24+
public int maximumProduct(int[] nums) {
25+
Arrays.sort(nums);
26+
int product = 1;
27+
if (nums.length >= 3) {
28+
for (int i = nums.length - 1; i >= nums.length - 3; i--) {
29+
product *= nums[i];
30+
}
31+
int anotherProduct = nums[0] * nums
32+
[1] * nums[nums.length - 1];
33+
product = Math.max(product, anotherProduct);
34+
} else {
35+
for (int i = 0; i < nums.length; i++) {
36+
product *= nums[i];
37+
}
3638
}
39+
return product;
3740
}
38-
return product;
39-
}
40-
41-
public static void main(String... args) {
42-
_628 test = new _628();
43-
// int[] nums = new int[]{1,2,3};
44-
// int[] nums = new int[]{1,2,3,4};
45-
int[] nums = new int[]{-4, -3, -2, -1, 60};
46-
System.out.println(test.maximumProduct(nums));
4741
}
4842
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._628;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _628Test {
10+
private static _628.Solution1 solution1;
11+
private static int[] nums;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _628.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
nums = new int[]{1, 2, 3};
21+
assertEquals(6, solution1.maximumProduct(nums));
22+
}
23+
24+
@Test
25+
public void test2() {
26+
nums = new int[]{1, 2, 3, 4};
27+
assertEquals(24, solution1.maximumProduct(nums));
28+
}
29+
30+
@Test
31+
public void test3() {
32+
nums = new int[]{-4, -3, -2, -1, 60};
33+
assertEquals(720, solution1.maximumProduct(nums));
34+
}
35+
36+
}

0 commit comments

Comments
 (0)