Maximum Product of Two Elements in an Array LeetCode solution in Java

1
class Solution {
2
public int maxProduct(int[] nums) {
3
int max = Integer.MIN_VALUE;
4
int maxi = -1;
5
for (int i = 0; i < nums.length; ++i) {
6
if (nums[i] > max) {
7
max = nums[i];
8
maxi = i;
9
}
10
}
11
nums[maxi] = Integer.MIN_VALUE;
12
int nextmax = Integer.MIN_VALUE;
13
for (int i = 0; i < nums.length; ++i) {
14
if (nums[i] > nextmax) nextmax = nums[i];
15
}
16
return max * nextmax - max - nextmax + 1;
17
}
18
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0