Microsoft VO 面试真题解析:Maximum Product of Three Numbers 数组乘积

38次阅读
没有评论

Given an array of integers, find the maximum possible product of any three numbers in the array.

这道题的核心是不要只盯着最大的三个数,因为两个负数相乘可能变成正数,从而让结果更大。常见做法是一次遍历维护数组中的三个最大值和两个最小值,最后比较「三个最大值的乘积」与「最大值乘以两个最小值的乘积」这两种情况,取更大者即可。这样可以在 O(n) 时间、O(1) 额外空间内完成,适合面试中快速实现。

正文完
 0