Skip to content

Commit bc1bd0d

Browse files
committed
solve problem Maximum Product Of Three Numbers
1 parent ea79b88 commit bc1bd0d

File tree

5 files changed

+100
-0
lines changed

5 files changed

+100
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ All solutions will be accepted!
141141
|125|[Valid Palindrome](https://leetcode-cn.com/problems/valid-palindrome/description/)|[java/py/js](./algorithms/ValidPalindrome)|Easy|
142142
|141|[Linked List Cycle](https://leetcode-cn.com/problems/linked-list-cycle/description/)|[java/py/js](./algorithms/LinkedListCycle)|Easy|
143143
|387|[First Unique Character In A String](https://leetcode-cn.com/problems/first-unique-character-in-a-string/description/)|[java/py/js](./algorithms/FirstUniqueCharacterInAString)|Easy|
144+
|628|[Maximum Product Of Three Numbers](https://leetcode-cn.com/problems/maximum-product-of-three-numbers/description/)|[java/py/js](./algorithms/MaximumProductOfThreeNumbers)|Easy|
144145

145146
# Database
146147
|#|Title|Solution|Difficulty|
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Maximum Product Of Three Numbers
2+
This problem is easy to solve
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public int maximumProduct(int[] nums) {
3+
int max1 = Integer.MIN_VALUE,
4+
max2 = Integer.MIN_VALUE,
5+
max3 = Integer.MIN_VALUE,
6+
min1 = Integer.MAX_VALUE,
7+
min2 = Integer.MAX_VALUE;
8+
9+
for (int num : nums) {
10+
if (num >= max1) {
11+
max3 = max2;
12+
max2 = max1;
13+
max1 = num;
14+
} else if (num >= max2) {
15+
max3 = max2;
16+
max2 = num;
17+
} else if (num >= max3) {
18+
max3 = num;
19+
}
20+
21+
if (num <= min1) {
22+
min2 = min1;
23+
min1 = num;
24+
} else if (num <= min2) {
25+
min2 = num;
26+
}
27+
}
28+
29+
return Math.max(max1 * max2 * max3, max1 * min1 * min2);
30+
}
31+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var maximumProduct = function(nums) {
6+
let max1 = Number.MIN_SAFE_INTEGER,
7+
max2 = Number.MIN_SAFE_INTEGER,
8+
max3 = Number.MIN_SAFE_INTEGER,
9+
min1 = Number.MAX_SAFE_INTEGER,
10+
min2 = Number.MAX_SAFE_INTEGER
11+
12+
nums.forEach(num => {
13+
if (num >= max1) {
14+
max3 = max2
15+
max2 = max1
16+
max1 = num
17+
} else if (num >= max2) {
18+
max3 = max2
19+
max2 = num
20+
} else if (num >= max3) {
21+
max3 = num
22+
}
23+
24+
if (num <= min1) {
25+
min2 = min1
26+
min1 = num
27+
} else if (num <= min2) {
28+
min2 = num
29+
}
30+
})
31+
32+
return Math.max(max1 * max2 * max3, max1 * min1 * min2)
33+
};
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution(object):
2+
def maximumProduct(self, nums):
3+
"""
4+
:type nums: List[int]
5+
:rtype: int
6+
"""
7+
# maximum product must be max1 * max2 * max3 or max1 * min1 * min2
8+
9+
max1 = - sys.maxint
10+
max2 = - sys.maxint
11+
max3 = - sys.maxint
12+
13+
min1 = sys.maxint
14+
min2 = sys.maxint
15+
16+
for num in nums:
17+
if num >= max1:
18+
max3 = max2
19+
max2 = max1
20+
max1 = num
21+
elif num >= max2:
22+
max3 = max2
23+
max2 = num
24+
elif num >= max3:
25+
max3 = num
26+
27+
if num <= min1:
28+
min2 = min1
29+
min1 = num
30+
elif num <= min2:
31+
min2 = num
32+
33+
return max(max1 * max2 * max3, max1 * min1 * min2)

0 commit comments

Comments
 (0)