Skip to content

Commit df6d556

Browse files
authored
Create 1464-maximum-product-of-two-elements-in-an-array.js
1 parent 86e71ae commit df6d556

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
const maxProduct = function(nums) {
6+
const n = nums.length
7+
let res = 0, m1 = 0, m2 = 0
8+
for(const e of nums) {
9+
if(e > m1) {
10+
m2 = m1
11+
m1 = e
12+
} else if(e > m2) {
13+
m2 = e
14+
}
15+
}
16+
17+
return (m1 - 1) * (m2 - 1)
18+
};

0 commit comments

Comments
 (0)