30:00
Given an integer array nums, pick three numbers whose product is as large as possible and return that product.
3≤nums.length≤1043 \leq \text{nums.length} \leq 10^43≤nums.length≤104
−1000≤nums[i]≤1000-1000 \leq \text{nums}[i] \leq 1000−1000≤nums[i]≤1000
Run code to see results.
Only five numbers matter: the three largest and the two smallest. Start every slot empty and scan once.
one pass over nums · 0/3 read
0
1
2
three largest so far
max1
max2
max3
two smallest so far
min1
min2
the two candidates
max1 · max2 · max3
? · ? · ?
min1 · min2 · max1
Pick three numbers from an integer array so that their product is as large as possible. Negative numbers make it more than a top-three question, but only two candidate products can ever win.