Skip to content

Commit 616ded2

Browse files
committed
feat: add solutions to lc problem: No.1464
No.1464.Maximum Product of Two Elements in an Array
1 parent b4c857b commit 616ded2

File tree

6 files changed

+340
-61
lines changed

6 files changed

+340
-61
lines changed

solution/1400-1499/1464.Maximum Product of Two Elements in an Array/README.md

+157-21
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,21 @@
4545

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

48-
找出数组中最大的两个元素的下标 i、j,然后计算 `(nums[i]-1)*(nums[j]-1)` 即可。
48+
**方法一:暴力枚举**
49+
50+
双重循环,枚举所有的下标对,求出 $(nums[i]-1) \times (nums[j]-1)$ 的最大值。其中 $i \neq j$。
51+
52+
时间复杂度 $O(n^2)$。
53+
54+
**方法二:排序**
55+
56+
对 $nums$ 进行排序,取最后两个元素,计算乘积 $(nums[n-1]-1) \times (nums[n-2]-1)$ 即可。
57+
58+
时间复杂度 $O(nlogn)$。
59+
60+
**方法三:一次遍历**
61+
62+
遍历 $nums$,维护最大值 $a$ 和次大值 $b$。遍历结束,返回 $(a-1) \times (b-1)$。
4963

5064
<!-- tabs:start -->
5165

@@ -56,15 +70,30 @@
5670
```python
5771
class Solution:
5872
def maxProduct(self, nums: List[int]) -> int:
59-
i = 0 if nums[0] > nums[1] else 1
60-
j = 1 - i
61-
for k in range(2, len(nums)):
62-
if nums[k] > nums[i]:
63-
j = k
64-
i, j = j, i
65-
elif nums[k] > nums[j]:
66-
j = k
67-
return (nums[i] - 1) * (nums[j] - 1)
73+
ans = 0
74+
for i, a in enumerate(nums):
75+
for b in nums[i + 1:]:
76+
ans = max(ans, (a - 1) * (b - 1))
77+
return ans
78+
```
79+
80+
```python
81+
class Solution:
82+
def maxProduct(self, nums: List[int]) -> int:
83+
nums.sort()
84+
return (nums[-1] - 1) * (nums[-2] - 1)
85+
```
86+
87+
```python
88+
class Solution:
89+
def maxProduct(self, nums: List[int]) -> int:
90+
a = b = 0
91+
for v in nums:
92+
if v > a:
93+
a, b = v, a
94+
elif v > b:
95+
b = v
96+
return (a - 1) * (b - 1)
6897
```
6998

7099
### **Java**
@@ -74,23 +103,130 @@ class Solution:
74103
```java
75104
class Solution {
76105
public int maxProduct(int[] nums) {
77-
int i = nums[0] > nums[1] ? 0 : 1;
78-
int j = 1 - i;
79-
for (int k = 2; k < nums.length; ++k) {
80-
if (nums[k] > nums[i]) {
81-
j = k;
82-
int t = i;
83-
i = j;
84-
j = t;
85-
} else if (nums[k] > nums[j]) {
86-
j = k;
106+
int ans = 0;
107+
int n = nums.length;
108+
for (int i = 0; i < n; ++i) {
109+
for (int j = i + 1; j < n; ++j) {
110+
ans = Math.max(ans, (nums[i] - 1) * (nums[j] - 1));
87111
}
88112
}
89-
return (nums[i] - 1) * (nums[j] - 1);
113+
return ans;
90114
}
91115
}
92116
```
93117

118+
```java
119+
class Solution {
120+
public int maxProduct(int[] nums) {
121+
Arrays.sort(nums);
122+
int n = nums.length;
123+
return (nums[n - 1] - 1) * (nums[n - 2] - 1);
124+
}
125+
}
126+
```
127+
128+
```java
129+
class Solution {
130+
public int maxProduct(int[] nums) {
131+
int a = 0, b = 0;
132+
for (int v : nums) {
133+
if (v > a) {
134+
b = a;
135+
a = v;
136+
} else if (v > b) {
137+
b = v;
138+
}
139+
}
140+
return (a - 1) * (b - 1);
141+
}
142+
}
143+
```
144+
145+
### **C++**
146+
147+
```cpp
148+
class Solution {
149+
public:
150+
int maxProduct(vector<int>& nums) {
151+
int ans = 0;
152+
int n = nums.size();
153+
for (int i = 0; i < n; ++i) {
154+
for (int j = i + 1; j < n; ++j) {
155+
ans = max(ans, (nums[i] - 1) * (nums[j] - 1));
156+
}
157+
}
158+
return ans;
159+
}
160+
};
161+
```
162+
163+
```cpp
164+
class Solution {
165+
public:
166+
int maxProduct(vector<int>& nums) {
167+
sort(nums.rbegin(), nums.rend());
168+
return (nums[0] - 1) * (nums[1] - 1);
169+
}
170+
};
171+
```
172+
173+
```cpp
174+
class Solution {
175+
public:
176+
int maxProduct(vector<int>& nums) {
177+
int a = 0, b = 0;
178+
for (int v : nums) {
179+
if (v > a) {
180+
b = a;
181+
a = v;
182+
} else if (v > b) {
183+
b = v;
184+
}
185+
}
186+
return (a - 1) * (b - 1);
187+
}
188+
};
189+
```
190+
191+
### **Go**
192+
193+
```go
194+
func maxProduct(nums []int) int {
195+
ans := 0
196+
for i, a := range nums {
197+
for _, b := range nums[i+1:] {
198+
t := (a - 1) * (b - 1)
199+
if ans < t {
200+
ans = t
201+
}
202+
}
203+
}
204+
return ans
205+
}
206+
```
207+
208+
```go
209+
func maxProduct(nums []int) int {
210+
sort.Ints(nums)
211+
n := len(nums)
212+
return (nums[n-1] - 1) * (nums[n-2] - 1)
213+
}
214+
```
215+
216+
```go
217+
func maxProduct(nums []int) int {
218+
a, b := 0, 0
219+
for _, v := range nums {
220+
if v > a {
221+
b, a = a, v
222+
} else if v > b {
223+
b = v
224+
}
225+
}
226+
return (a - 1) * (b - 1)
227+
}
228+
```
229+
94230
### **...**
95231

96232
```

solution/1400-1499/1464.Maximum Product of Two Elements in an Array/README_EN.md

+142-20
Original file line numberDiff line numberDiff line change
@@ -47,39 +47,161 @@ Given the array of integers <code>nums</code>, you will choose two different ind
4747
```python
4848
class Solution:
4949
def maxProduct(self, nums: List[int]) -> int:
50-
i = 0 if nums[0] > nums[1] else 1
51-
j = 1 - i
52-
for k in range(2, len(nums)):
53-
if nums[k] > nums[i]:
54-
j = k
55-
i, j = j, i
56-
elif nums[k] > nums[j]:
57-
j = k
58-
return (nums[i] - 1) * (nums[j] - 1)
50+
ans = 0
51+
for i, a in enumerate(nums):
52+
for b in nums[i + 1:]:
53+
ans = max(ans, (a - 1) * (b - 1))
54+
return ans
55+
```
56+
57+
```python
58+
class Solution:
59+
def maxProduct(self, nums: List[int]) -> int:
60+
nums.sort()
61+
return (nums[-1] - 1) * (nums[-2] - 1)
62+
```
63+
64+
```python
65+
class Solution:
66+
def maxProduct(self, nums: List[int]) -> int:
67+
a = b = 0
68+
for v in nums:
69+
if v > a:
70+
a, b = v, a
71+
elif v > b:
72+
b = v
73+
return (a - 1) * (b - 1)
5974
```
6075

6176
### **Java**
6277

6378
```java
6479
class Solution {
6580
public int maxProduct(int[] nums) {
66-
int i = nums[0] > nums[1] ? 0 : 1;
67-
int j = 1 - i;
68-
for (int k = 2; k < nums.length; ++k) {
69-
if (nums[k] > nums[i]) {
70-
j = k;
71-
int t = i;
72-
i = j;
73-
j = t;
74-
} else if (nums[k] > nums[j]) {
75-
j = k;
81+
int ans = 0;
82+
int n = nums.length;
83+
for (int i = 0; i < n; ++i) {
84+
for (int j = i + 1; j < n; ++j) {
85+
ans = Math.max(ans, (nums[i] - 1) * (nums[j] - 1));
86+
}
87+
}
88+
return ans;
89+
}
90+
}
91+
```
92+
93+
```java
94+
class Solution {
95+
public int maxProduct(int[] nums) {
96+
Arrays.sort(nums);
97+
int n = nums.length;
98+
return (nums[n - 1] - 1) * (nums[n - 2] - 1);
99+
}
100+
}
101+
```
102+
103+
```java
104+
class Solution {
105+
public int maxProduct(int[] nums) {
106+
int a = 0, b = 0;
107+
for (int v : nums) {
108+
if (v > a) {
109+
b = a;
110+
a = v;
111+
} else if (v > b) {
112+
b = v;
76113
}
77114
}
78-
return (nums[i] - 1) * (nums[j] - 1);
115+
return (a - 1) * (b - 1);
79116
}
80117
}
81118
```
82119

120+
### **C++**
121+
122+
```cpp
123+
class Solution {
124+
public:
125+
int maxProduct(vector<int>& nums) {
126+
int ans = 0;
127+
int n = nums.size();
128+
for (int i = 0; i < n; ++i) {
129+
for (int j = i + 1; j < n; ++j) {
130+
ans = max(ans, (nums[i] - 1) * (nums[j] - 1));
131+
}
132+
}
133+
return ans;
134+
}
135+
};
136+
```
137+
138+
```cpp
139+
class Solution {
140+
public:
141+
int maxProduct(vector<int>& nums) {
142+
sort(nums.rbegin(), nums.rend());
143+
return (nums[0] - 1) * (nums[1] - 1);
144+
}
145+
};
146+
```
147+
148+
```cpp
149+
class Solution {
150+
public:
151+
int maxProduct(vector<int>& nums) {
152+
int a = 0, b = 0;
153+
for (int v : nums) {
154+
if (v > a) {
155+
b = a;
156+
a = v;
157+
} else if (v > b) {
158+
b = v;
159+
}
160+
}
161+
return (a - 1) * (b - 1);
162+
}
163+
};
164+
```
165+
166+
### **Go**
167+
168+
```go
169+
func maxProduct(nums []int) int {
170+
ans := 0
171+
for i, a := range nums {
172+
for _, b := range nums[i+1:] {
173+
t := (a - 1) * (b - 1)
174+
if ans < t {
175+
ans = t
176+
}
177+
}
178+
}
179+
return ans
180+
}
181+
```
182+
183+
```go
184+
func maxProduct(nums []int) int {
185+
sort.Ints(nums)
186+
n := len(nums)
187+
return (nums[n-1] - 1) * (nums[n-2] - 1)
188+
}
189+
```
190+
191+
```go
192+
func maxProduct(nums []int) int {
193+
a, b := 0, 0
194+
for _, v := range nums {
195+
if v > a {
196+
b, a = a, v
197+
} else if v > b {
198+
b = v
199+
}
200+
}
201+
return (a - 1) * (b - 1)
202+
}
203+
```
204+
83205
### **...**
84206

85207
```

0 commit comments

Comments
 (0)