Given an integer array, find three numbers whose product is maximum and output the maximum product.

Example 1:

Input: [1,2,3]
Output: 6

Example 2:

Input: [1,2,3,4]
Output: 24

Note:

  1. The length of the given array will be in range [3,104] and all elements are in the range [-1000, 1000].
  2. Multiplication of any three numbers in the input won't exceed the range of 32-bit signed integer.

题目标签:Array, Math

  题目给了我们一个nums array,要我们找到最大的三数乘积。

  先让我们来看一下几个例子,假设下面的数字是从小到大排序,- 代表负数,+ 代表正数

  a. + + + + +

    答案:max1 * max2 * max3  最大的三个数字乘积

  b. - - + + +   

    答案:在 max1 * max2 * max3 和 min1 * min2 * max1 里取大的,这里就需要比较一下两边的 2种情况了。

  c. - - 0 + +

    答案:min1 * min2 * max1, 这里包括0,其实0并不会影响我们的算法。

  d. - - - - +

    答案:min1 * min2 * max1

  e. - - - - -

    答案:max1 * max2 * max3, 这里全部都是负数,答案反而变成了三个最大的数字乘积,因为这个情况下,总会是负数,所以要挑最右边三个数字。

  这样我们就发现了,最大三个数字乘积,就发生在 最大的三个数字乘积最小的2个数字 * 最大的数字 中。只要维护更新max1, max2, max3, min1, min2 取大的那种情况就可以。

Java Solution:

Runtime beats 91.55%

完成日期:10/18/2017

关键词:Array, Math

关键点:维护更新max1, max2, max3, min1, min2

 class Solution
{
public int maximumProduct(int[] nums)
{
int max1 = Integer.MIN_VALUE;
int max2 = max1;
int max3 = max1; int min1 = Integer.MAX_VALUE;
int min2 = min1; for(int num: nums)
{
// take care max
if(num > max1)
{
max3 = max2;
max2 = max1;
max1 = num;
}
else if(num > max2)
{
max3 = max2;
max2 = num;
}
else if(num > max3)
max3 = num; // take care min
if(num < min1)
{
min2 = min1;
min1 = num;
}
else if(num < min2)
min2 = num;
} return Math.max(max1 * max2 * max3, min1 * min2 * max1);
}
}

参考资料:n/a

LeetCode 题目列表 - LeetCode Questions List

题目来源:https://leetcode.com/

LeetCode 628. Maximum Product of Three Numbers (最大三数乘积)的更多相关文章

  1. [LeetCode] 628. Maximum Product of Three Numbers 三个数字的最大乘积

    Given an integer array, find three numbers whose product is maximum and output the maximum product. ...

  2. LeetCode 628. Maximum Product of Three Numbers三个数的最大乘积 (C++)

    题目: Given an integer array, find three numbers whose product is maximum and output the maximum produ ...

  3. 【Leetcode_easy】628. Maximum Product of Three Numbers

    problem 628. Maximum Product of Three Numbers 题意:三个数乘积的最大值: solution1: 如果全是负数,三个负数相乘还是负数,为了让负数最大,那么其 ...

  4. 628. Maximum Product of Three Numbers@python

    Given an integer array, find three numbers whose product is maximum and output the maximum product. ...

  5. 【LeetCode】628. Maximum Product of Three Numbers 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:排序 日期 题目地址:https://lee ...

  6. [LeetCode&Python] Problem 628. Maximum Product of Three Numbers

    Given an integer array, find three numbers whose product is maximum and output the maximum product. ...

  7. 628. Maximum Product of Three Numbers

    Given an integer array, find three numbers whose product is maximum and output the maximum product. ...

  8. [LeetCode] 628. Maximum Product of Three Numbers_Easy

    Given an integer array, find three numbers whose product is maximum and output the maximum product. ...

  9. [Array]628. Maximum Product of Three Numbers

    Given an integer array, find three numbers whose product is maximum and output the maximum product. ...

随机推荐

  1. FreeMarker模板使用小结

    手册写的不错,忘记的时候可以翻翻n(*≧▽≦*)n --------------------------------------------分割线--------------------------- ...

  2. 前端基础之css

    一.form表单 在form表单有两个重要的属性分别是:               关于表单两个属性: name: 作为发送server端的数据的键                          ...

  3. websphere部署--web应用-以自己的项目为例

    启动websphere: 1) 启动Manager: /home/wasadmin/IBM/WebSphere/AppServer/bin/startManager.sh 2) 启动Node:     ...

  4. python新增nonlocal关键字

    def fa(a): b = 2 def fb(): nonlocal b print(b) return fbc = 2fa(22)()# python作用域:LEGB

  5. JVM菜鸟进阶高手之路四

    转载请注明原创出处,谢谢! 由于很多的jvm分析最好是基于gc日志的,所以添加参数如下即可: -verbose:gc -XX:+HeapDumpOnOutOfMemoryError -XX:+Prin ...

  6. spring依赖注入中接口的问题

    问题描述:一个接口,有俩个实现类当注入时候名字不同时,会出现不同的情况 action层: @Controller("userAction") @Scope("protot ...

  7. xcode7.3 iTunes Store operation failed解决

    使用apploader上传程序 提示:如果您安装了XCode开发环境.在/Applications/XCode.app/Contents/Applications目录中可以找到Application ...

  8. Java 中与(&)短路与(&&)以及 或(|)短路或(||)的关系

    一.逻辑运算符的使用 1)逻辑运算符的连接的是布尔表达式,要与位运算符做区分. 2)使用方法: public class Test {  public static void main(String[ ...

  9. ThinkPHP中:用户登录权限验证类

    使用CommonAction.class.php公共类,统一判断用户是否登录 <?php //后台登录页 Class CommonAction extends Action{ //后台登录页面 ...

  10. C语言 printf 格式化输出函数

    用 法: int printf(const char *format,[argument]); format 参数输出的格式,定义格式为: %[flags][width][.perc] [F|N|h| ...