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. 框架应用:Spring framework (五) - Spring MVC技术

    软件开发中的MVC设计模式 软件开发的目标是减小耦合,让模块之前关系清晰. MVC模式在软件开发中经常和ORM模式一起应用,主要作用是将(数据抽象,数据实体传输和前台数据展示)分层,这样前台,后台,数 ...

  2. 关于args的一个小bug

    我在开始学习Java的时候就有点疑惑,到底main方法中的args到底是什么?经过我的一些思考,然后结合代码写一点自己的看法. 下面来看一段代码: /** * @author 薛定谔的猫 * 关于ar ...

  3. Class类与Java反射

    1反射机制是什么 反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取的信息以及动态调用对象的方法的功能称为jav ...

  4. SublimeTest3设置【中文乱码】

    SublimeTest出现乱码! 使用Ctrl+`快捷键或者通过View->Show Console菜单打开命令行,粘贴如下代码 import urllib.request,os; pf = ' ...

  5. 工作总结--如何定位web系统前后台的bug,以及bug分析/测试感想

    对于web项目前台和后台bug定位分析:一. 系统整体了解 懒企鹅营销服务平台用的架构:web前端: Bootstrap 3.0 组件丰富,兼容性好,界面美观 Server端: jsp+Servlet ...

  6. System.getProperty()参数大全

    System.getProperty()获取Java各种配置属性,参数如下: Java.version Java 运行时环境版本 java.vendor Java 运行时环境供应商 java.vend ...

  7. 谈谈IT界8大恐怖预言!

    IT界的8大恐怖预言 本文字数:3276 建议阅读时间:你开心就好 第三次科技革命已经进入白热化阶段———信息技术革命作为其中最主要的一环已经奠定了其基本格局和趋势.OK大势已定,根据目前的形势,小编 ...

  8. Go语言基础知识

    Go语言的一般结构:basic_structure.go Go程序是通过package来组织的,只能同过package名称为main的包可以包含main函数(一个可执行程序只能有一个main包) 通过 ...

  9. js学习要点

    js 一.词法结构 1.区分大小写 2.注意 // 单行 /* 多行注释 */ 3.字面量(直接量 literal) 12 //数字 5.8 // 小数 "hello" 'hell ...

  10. java初学者(新手)应该如何选择学习教材与网站

    作者:天天向上 1.学习教材选择推荐<JAVA核心技术>,想多看点代码多练习可以找<java开发实战经典>&amp;amp;lt;img src="https ...