Skip to content

Commit 6f74853

Browse files
committed
feat: add solutions to lc problem: No.0628
No.0628.Maximum Product of Three Numbers
1 parent 10a1258 commit 6f74853

File tree

6 files changed

+326
-35
lines changed

6 files changed

+326
-35
lines changed

solution/0600-0699/0628.Maximum Product of Three Numbers/README.md

+137-16
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,25 @@
4444

4545
<!-- 这里可写通用的实现逻辑 -->
4646

47+
**方法一:排序 + 分类讨论**
48+
49+
我们先对数组 $nums$ 进行排序,接下来分两种情况讨论:
50+
51+
- 如果 $nums$ 中全是非负数或者全是非正数,那么答案即为最后三个数的乘积,即 $nums[n-1] \times nums[n-2] \times nums[n-3]$;
52+
- 如果 $nums$ 中既有正数也有负数,那么答案可能是两个最小负数和一个最大整数的乘积,即 $nums[n-1] \times nums[0] \times nums[1]$;也可能是最后三个数的乘积,即 $nums[n-1] \times nums[n-2] \times nums[n-3]$。
53+
54+
最后返回两种情况的最大值即可。
55+
56+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为数组 $nums$ 的长度。
57+
58+
**方法二:一次遍历**
59+
60+
我们可以不用对数组进行排序,而是维护五个变量,其中 $mi1$ 和 $mi2$ 表示数组中最小的两个数,而 $mx1$、$mx2$ 和 $mx3$ 表示数组中最大的三个数。
61+
62+
最后返回 $max(mi1 \times mi2 \times mx1, mx1 \times mx2 \times mx3)$ 即可。
63+
64+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。
65+
4766
<!-- tabs:start -->
4867

4968
### **Python3**
@@ -53,14 +72,18 @@
5372
```python
5473
class Solution:
5574
def maximumProduct(self, nums: List[int]) -> int:
56-
n = len(nums)
5775
nums.sort()
58-
# 全负 0 1 n-1
59-
# 全正 n-1 n-2 n-3
60-
# 有正有负 max([0 1 n-1], [n-1 n-2 n-3])
61-
return max(
62-
nums[0] * nums[1] * nums[n - 1], nums[n - 1] * nums[n - 2] * nums[n - 3]
63-
)
76+
a = nums[-1] * nums[-2] * nums[-3]
77+
b = nums[-1] * nums[0] * nums[1]
78+
return max(a, b)
79+
```
80+
81+
```python
82+
class Solution:
83+
def maximumProduct(self, nums: List[int]) -> int:
84+
top3 = nlargest(3, nums)
85+
bottom2 = nlargest(2, nums, key=lambda x: -x)
86+
return max(top3[0] * top3[1] * top3[2], top3[0] * bottom2[0] * bottom2[1])
6487
```
6588

6689
### **Java**
@@ -72,24 +95,122 @@ class Solution {
7295
public int maximumProduct(int[] nums) {
7396
Arrays.sort(nums);
7497
int n = nums.length;
75-
// 全负 0 1 n-1
76-
// 全正 n-1 n-2 n-3
77-
// 有正有负 max([0 1 n-1], [n-1 n-2 n-3])
78-
return Math.max(nums[0] * nums[1] * nums[n - 1], nums[n - 1] * nums[n - 2] * nums[n - 3]);
98+
int a = nums[n - 1] * nums[n - 2] * nums[n - 3];
99+
int b = nums[n - 1] * nums[0] * nums[1];
100+
return Math.max(a, b);
101+
}
102+
}
103+
```
104+
105+
```java
106+
class Solution {
107+
public int maximumProduct(int[] nums) {
108+
final int inf = 1 << 30;
109+
int mi1 = inf, mi2 = inf;
110+
int mx1 = -inf, mx2 = -inf, mx3 = -inf;
111+
for (int x : nums) {
112+
if (x < mi1) {
113+
mi2 = mi1;
114+
mi1 = x;
115+
} else if (x < mi2) {
116+
mi2 = x;
117+
}
118+
if (x > mx1) {
119+
mx3 = mx2;
120+
mx2 = mx1;
121+
mx1 = x;
122+
} else if (x > mx2) {
123+
mx3 = mx2;
124+
mx2 = x;
125+
} else if (x > mx3) {
126+
mx3 = x;
127+
}
128+
}
129+
return Math.max(mi1 * mi2 * mx1, mx1 * mx2 * mx3);
79130
}
80131
}
81132
```
82133

134+
### **C++**
135+
136+
```cpp
137+
class Solution {
138+
public:
139+
int maximumProduct(vector<int>& nums) {
140+
sort(nums.begin(), nums.end());
141+
int n = nums.size();
142+
int a = nums[n - 1] * nums[n - 2] * nums[n - 3];
143+
int b = nums[n - 1] * nums[0] * nums[1];
144+
return max(a, b);
145+
}
146+
};
147+
```
148+
149+
```cpp
150+
class Solution {
151+
public:
152+
int maximumProduct(vector<int>& nums) {
153+
const int inf = 1 << 30;
154+
int mi1 = inf, mi2 = inf;
155+
int mx1 = -inf, mx2 = -inf, mx3 = -inf;
156+
for (int x : nums) {
157+
if (x < mi1) {
158+
mi2 = mi1;
159+
mi1 = x;
160+
} else if (x < mi2) {
161+
mi2 = x;
162+
}
163+
if (x > mx1) {
164+
mx3 = mx2;
165+
mx2 = mx1;
166+
mx1 = x;
167+
} else if (x > mx2) {
168+
mx3 = mx2;
169+
mx2 = x;
170+
} else if (x > mx3) {
171+
mx3 = x;
172+
}
173+
}
174+
return max(mi1 * mi2 * mx1, mx1 * mx2 * mx3);
175+
}
176+
};
177+
```
178+
83179
### **Go**
84180

85181
```go
86182
func maximumProduct(nums []int) int {
87-
n := len(nums)
88183
sort.Ints(nums)
89-
// 全负 0 1 n-1
90-
// 全正 n-1 n-2 n-3
91-
// 有正有负 max([0 1 n-1], [n-1 n-2 n-3])
92-
return max(nums[0]*nums[1]*nums[n-1], nums[n-1]*nums[n-2]*nums[n-3])
184+
n := len(nums)
185+
a := nums[n-1] * nums[n-2] * nums[n-3]
186+
b := nums[n-1] * nums[0] * nums[1]
187+
if a > b {
188+
return a
189+
}
190+
return b
191+
}
192+
```
193+
194+
```go
195+
func maximumProduct(nums []int) int {
196+
const inf = 1 << 30
197+
mi1, mi2 := inf, inf
198+
mx1, mx2, mx3 := -inf, -inf, -inf
199+
for _, x := range nums {
200+
if x < mi1 {
201+
mi1, mi2 = x, mi1
202+
} else if x < mi2 {
203+
mi2 = x
204+
}
205+
if x > mx1 {
206+
mx1, mx2, mx3 = x, mx1, mx2
207+
} else if x > mx2 {
208+
mx2, mx3 = x, mx2
209+
} else if x > mx3 {
210+
mx3 = x
211+
}
212+
}
213+
return max(mi1*mi2*mx1, mx1*mx2*mx3)
93214
}
94215

95216
func max(a, b int) int {

solution/0600-0699/0628.Maximum Product of Three Numbers/README_EN.md

+118-7
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,18 @@
3434
```python
3535
class Solution:
3636
def maximumProduct(self, nums: List[int]) -> int:
37-
n = len(nums)
3837
nums.sort()
39-
return max(
40-
nums[0] * nums[1] * nums[n - 1], nums[n - 1] * nums[n - 2] * nums[n - 3]
41-
)
38+
a = nums[-1] * nums[-2] * nums[-3]
39+
b = nums[-1] * nums[0] * nums[1]
40+
return max(a, b)
41+
```
42+
43+
```python
44+
class Solution:
45+
def maximumProduct(self, nums: List[int]) -> int:
46+
top3 = nlargest(3, nums)
47+
bottom2 = nlargest(2, nums, key=lambda x: -x)
48+
return max(top3[0] * top3[1] * top3[2], top3[0] * bottom2[0] * bottom2[1])
4249
```
4350

4451
### **Java**
@@ -48,18 +55,122 @@ class Solution {
4855
public int maximumProduct(int[] nums) {
4956
Arrays.sort(nums);
5057
int n = nums.length;
51-
return Math.max(nums[0] * nums[1] * nums[n - 1], nums[n - 1] * nums[n - 2] * nums[n - 3]);
58+
int a = nums[n - 1] * nums[n - 2] * nums[n - 3];
59+
int b = nums[n - 1] * nums[0] * nums[1];
60+
return Math.max(a, b);
5261
}
5362
}
5463
```
5564

65+
```java
66+
class Solution {
67+
public int maximumProduct(int[] nums) {
68+
final int inf = 1 << 30;
69+
int mi1 = inf, mi2 = inf;
70+
int mx1 = -inf, mx2 = -inf, mx3 = -inf;
71+
for (int x : nums) {
72+
if (x < mi1) {
73+
mi2 = mi1;
74+
mi1 = x;
75+
} else if (x < mi2) {
76+
mi2 = x;
77+
}
78+
if (x > mx1) {
79+
mx3 = mx2;
80+
mx2 = mx1;
81+
mx1 = x;
82+
} else if (x > mx2) {
83+
mx3 = mx2;
84+
mx2 = x;
85+
} else if (x > mx3) {
86+
mx3 = x;
87+
}
88+
}
89+
return Math.max(mi1 * mi2 * mx1, mx1 * mx2 * mx3);
90+
}
91+
}
92+
```
93+
94+
### **C++**
95+
96+
```cpp
97+
class Solution {
98+
public:
99+
int maximumProduct(vector<int>& nums) {
100+
sort(nums.begin(), nums.end());
101+
int n = nums.size();
102+
int a = nums[n - 1] * nums[n - 2] * nums[n - 3];
103+
int b = nums[n - 1] * nums[0] * nums[1];
104+
return max(a, b);
105+
}
106+
};
107+
```
108+
109+
```cpp
110+
class Solution {
111+
public:
112+
int maximumProduct(vector<int>& nums) {
113+
const int inf = 1 << 30;
114+
int mi1 = inf, mi2 = inf;
115+
int mx1 = -inf, mx2 = -inf, mx3 = -inf;
116+
for (int x : nums) {
117+
if (x < mi1) {
118+
mi2 = mi1;
119+
mi1 = x;
120+
} else if (x < mi2) {
121+
mi2 = x;
122+
}
123+
if (x > mx1) {
124+
mx3 = mx2;
125+
mx2 = mx1;
126+
mx1 = x;
127+
} else if (x > mx2) {
128+
mx3 = mx2;
129+
mx2 = x;
130+
} else if (x > mx3) {
131+
mx3 = x;
132+
}
133+
}
134+
return max(mi1 * mi2 * mx1, mx1 * mx2 * mx3);
135+
}
136+
};
137+
```
138+
56139
### **Go**
57140

58141
```go
59142
func maximumProduct(nums []int) int {
60-
n := len(nums)
61143
sort.Ints(nums)
62-
return max(nums[0]*nums[1]*nums[n-1], nums[n-1]*nums[n-2]*nums[n-3])
144+
n := len(nums)
145+
a := nums[n-1] * nums[n-2] * nums[n-3]
146+
b := nums[n-1] * nums[0] * nums[1]
147+
if a > b {
148+
return a
149+
}
150+
return b
151+
}
152+
```
153+
154+
```go
155+
func maximumProduct(nums []int) int {
156+
const inf = 1 << 30
157+
mi1, mi2 := inf, inf
158+
mx1, mx2, mx3 := -inf, -inf, -inf
159+
for _, x := range nums {
160+
if x < mi1 {
161+
mi1, mi2 = x, mi1
162+
} else if x < mi2 {
163+
mi2 = x
164+
}
165+
if x > mx1 {
166+
mx1, mx2, mx3 = x, mx1, mx2
167+
} else if x > mx2 {
168+
mx2, mx3 = x, mx2
169+
} else if x > mx3 {
170+
mx3 = x
171+
}
172+
}
173+
return max(mi1*mi2*mx1, mx1*mx2*mx3)
63174
}
64175

65176
func max(a, b int) int {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public:
3+
int maximumProduct(vector<int>& nums) {
4+
const int inf = 1 << 30;
5+
int mi1 = inf, mi2 = inf;
6+
int mx1 = -inf, mx2 = -inf, mx3 = -inf;
7+
for (int x : nums) {
8+
if (x < mi1) {
9+
mi2 = mi1;
10+
mi1 = x;
11+
} else if (x < mi2) {
12+
mi2 = x;
13+
}
14+
if (x > mx1) {
15+
mx3 = mx2;
16+
mx2 = mx1;
17+
mx1 = x;
18+
} else if (x > mx2) {
19+
mx3 = mx2;
20+
mx2 = x;
21+
} else if (x > mx3) {
22+
mx3 = x;
23+
}
24+
}
25+
return max(mi1 * mi2 * mx1, mx1 * mx2 * mx3);
26+
}
27+
};
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
11
func maximumProduct(nums []int) int {
2-
n := len(nums)
3-
sort.Ints(nums)
4-
return max(nums[0]*nums[1]*nums[n-1], nums[n-1]*nums[n-2]*nums[n-3])
2+
const inf = 1 << 30
3+
mi1, mi2 := inf, inf
4+
mx1, mx2, mx3 := -inf, -inf, -inf
5+
for _, x := range nums {
6+
if x < mi1 {
7+
mi1, mi2 = x, mi1
8+
} else if x < mi2 {
9+
mi2 = x
10+
}
11+
if x > mx1 {
12+
mx1, mx2, mx3 = x, mx1, mx2
13+
} else if x > mx2 {
14+
mx2, mx3 = x, mx2
15+
} else if x > mx3 {
16+
mx3 = x
17+
}
18+
}
19+
return max(mi1*mi2*mx1, mx1*mx2*mx3)
520
}
621

722
func max(a, b int) int {
823
if a > b {
924
return a
1025
}
1126
return b
12-
}
27+
}

0 commit comments

Comments
 (0)