public class Solution {
public int MaxProduct(int[] nums) {
var pq = new PriorityQueue<int, int>(nums.Select(x => (x, -x)));
return (pq.Dequeue() - 1) * (pq.Dequeue() - 1);
}
}^With a heap
Actually, no need for a heap.
public class Solution {
public int MaxProduct(int[] nums) {
int max1 = 0, max2 = 0; // constraints: nums[i] >= 1
foreach (int x in nums) {
if (x > max1) { max2 = max1; max1 = x; }
else if (x > max2) max2 = x;
}
return (max1 - 1) * (max2 - 1);
}
}