Skip to content

Commit 3985d27

Browse files
authored
Create 1464_Maximum_Product_of_Two_Elements_in_an_Array.java
1 parent 10b8107 commit 3985d27

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// id: 1464
2+
// Name: Maximum Product of Two Elements in an Array
3+
// link: https://leetcode.com/problems/maximum-product-of-two-elements-in-an-array/
4+
// Difficulty: Easy
5+
6+
class Solution {
7+
public int maxProduct(int[] nums) {
8+
int max = 0;
9+
int secondMax = 0;
10+
for (int n: nums) {
11+
if (n > max) {
12+
secondMax = max;
13+
max = n;
14+
} else if (n > secondMax) {
15+
secondMax = n;
16+
}
17+
}
18+
return (max-1) * (secondMax-1);
19+
}
20+
}

0 commit comments

Comments
 (0)