Same idea, but using two multiset instead.

Remove the smallest (first) element in "largest" multiset and largest (last) element in "smallest" multiest when their size becomes greater than 3 and 2 respectively.

int maximumProduct(vector<int>& nums) {
multiset<int> largest;
multiset<int> smallest;
for(int i: nums){
largest.insert(i);
smallest.insert(i);
if(largest.size() > 3)
largest.erase(largest.begin());
if(smallest.size() > 2)
smallest.erase(prev(smallest.end()));
}
int res1 = 1, res2 = *(largest.rbegin());
for(int i : largest)
res1 *= i;
for(int i : smallest)
res2 *= i;
return max(res1, res2);
}