Skip to content

Commit be1901f

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

File tree

5 files changed

+188
-0
lines changed

5 files changed

+188
-0
lines changed

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

+73
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,79 @@ func maxProduct(nums []int) int {
227227
}
228228
```
229229

230+
### **C**
231+
232+
```c
233+
int maxProduct(int* nums, int numsSize){
234+
int max = 0;
235+
int submax = 0;
236+
for (int i = 0; i < numsSize; i++) {
237+
int num = nums[i];
238+
if (num > max) {
239+
submax = max;
240+
max = num;
241+
} else if (num > submax) {
242+
submax = num;
243+
}
244+
}
245+
return (max - 1) * (submax - 1);
246+
}
247+
```
248+
249+
### **TypeScript**
250+
251+
```ts
252+
function maxProduct(nums: number[]): number {
253+
const n = nums.length;
254+
for (let i = 0; i < 2; i++) {
255+
let maxIdx = i;
256+
for (let j = i + 1; j < n; j++) {
257+
if (nums[j] > nums[maxIdx]) {
258+
maxIdx = j;
259+
}
260+
}
261+
[nums[i], nums[maxIdx]] = [nums[maxIdx], nums[i]];
262+
}
263+
return (nums[0] - 1) * (nums[1] - 1);
264+
}
265+
```
266+
267+
```ts
268+
function maxProduct(nums: number[]): number {
269+
let max = 0;
270+
let submax = 0;
271+
for (const num of nums) {
272+
if (num > max) {
273+
submax = max;
274+
max = num;
275+
} else if (num > submax) {
276+
submax = num;
277+
}
278+
}
279+
return (max - 1) * (submax - 1);
280+
}
281+
```
282+
283+
### **Rust**
284+
285+
```rust
286+
impl Solution {
287+
pub fn max_product(nums: Vec<i32>) -> i32 {
288+
let mut max = 0;
289+
let mut submax = 0;
290+
for &num in nums.iter() {
291+
if num > max {
292+
submax = max;
293+
max = num;
294+
} else if num > submax {
295+
submax = num;
296+
}
297+
}
298+
(max - 1) * (submax - 1)
299+
}
300+
}
301+
```
302+
230303
### **...**
231304

232305
```

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

+73
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,79 @@ func maxProduct(nums []int) int {
202202
}
203203
```
204204

205+
### **C**
206+
207+
```c
208+
int maxProduct(int* nums, int numsSize){
209+
int max = 0;
210+
int submax = 0;
211+
for (int i = 0; i < numsSize; i++) {
212+
int num = nums[i];
213+
if (num > max) {
214+
submax = max;
215+
max = num;
216+
} else if (num > submax) {
217+
submax = num;
218+
}
219+
}
220+
return (max - 1) * (submax - 1);
221+
}
222+
```
223+
224+
### **TypeScript**
225+
226+
```ts
227+
function maxProduct(nums: number[]): number {
228+
const n = nums.length;
229+
for (let i = 0; i < 2; i++) {
230+
let maxIdx = i;
231+
for (let j = i + 1; j < n; j++) {
232+
if (nums[j] > nums[maxIdx]) {
233+
maxIdx = j;
234+
}
235+
}
236+
[nums[i], nums[maxIdx]] = [nums[maxIdx], nums[i]];
237+
}
238+
return (nums[0] - 1) * (nums[1] - 1);
239+
}
240+
```
241+
242+
```ts
243+
function maxProduct(nums: number[]): number {
244+
let max = 0;
245+
let submax = 0;
246+
for (const num of nums) {
247+
if (num > max) {
248+
submax = max;
249+
max = num;
250+
} else if (num > submax) {
251+
submax = num;
252+
}
253+
}
254+
return (max - 1) * (submax - 1);
255+
}
256+
```
257+
258+
### **Rust**
259+
260+
```rust
261+
impl Solution {
262+
pub fn max_product(nums: Vec<i32>) -> i32 {
263+
let mut max = 0;
264+
let mut submax = 0;
265+
for &num in nums.iter() {
266+
if num > max {
267+
submax = max;
268+
max = num;
269+
} else if num > submax {
270+
submax = num;
271+
}
272+
}
273+
(max - 1) * (submax - 1)
274+
}
275+
}
276+
```
277+
205278
### **...**
206279

207280
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
int maxProduct(int* nums, int numsSize){
2+
int max = 0;
3+
int submax = 0;
4+
for (int i = 0; i < numsSize; i++) {
5+
int num = nums[i];
6+
if (num > max) {
7+
submax = max;
8+
max = num;
9+
} else if (num > submax) {
10+
submax = num;
11+
}
12+
}
13+
return (max - 1) * (submax - 1);
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
impl Solution {
2+
pub fn max_product(nums: Vec<i32>) -> i32 {
3+
let mut max = 0;
4+
let mut submax = 0;
5+
for &num in nums.iter() {
6+
if num > max {
7+
submax = max;
8+
max = num;
9+
} else if num > submax {
10+
submax = num;
11+
}
12+
}
13+
(max - 1) * (submax - 1)
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function maxProduct(nums: number[]): number {
2+
let max = 0;
3+
let submax = 0;
4+
for (const num of nums) {
5+
if (num > max) {
6+
submax = max;
7+
max = num;
8+
} else if (num > submax) {
9+
submax = num;
10+
}
11+
}
12+
return (max - 1) * (submax - 1);
13+
}

0 commit comments

Comments
 (0)