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. 在linux环境下搭建java web测试环境(非常详细!!)

    一.项目必备软件及基本思路 项目必备:虚拟机:VMware Workstation (已安装linux的 CentOS6.5版本) 项目:java web项目 (必须在本地部署编译后选择项目的webR ...

  2. Redis集群介绍

    http://mt.sohu.com/20160601/n452401108.shtml

  3. const在c和c++中的不同

    最近开始由学习c转到c++:从面向过程到面向对象的转变中,总是以面向过程的思路思考,发现有很多的不同,今天就稍微发表一下我的见解,如果那里出错,希望大神可以帮忙指出来. 首先是const : 在C语法 ...

  4. AngularJS–service(服务)

    点击查看AngularJS系列目录 转载请注明出处:http://www.cnblogs.com/leosx/ 服务 Angular的服务也是使用依赖注入(dependency injection ( ...

  5. 教育,创新,提升:Indiegogo和Kickstarter上受中国用户支持的10个众筹项目

    中国的经济正在迅速发展,已成为世界第二大经济体.中国家庭随着经济水平的提高,越来越多父母愿意将自己的子女送到海外留学. 家长们希望自己的子女可以有机会接受国外大学优质的教育, 以便他们将来可以学成归来 ...

  6. hdu4704 Sum 2013 Multi-University Training Contest 10 数论题

    Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Subm ...

  7. django ajax练习

    这几天遇到了django ajax请求出错的问题,总结一下 前端js:我这里创建的是一个字典格式的数据,前端js收到字典之后也是要用字典的形式去解包后台传送过来的数据,比如我下面的写法:data['s ...

  8. 一文为你详细讲解对象映射库【AutoMapper】所支持场景

    前言 在AutoMapper未出世前,对象与对象之间的映射,我们只能通过手动为每个属性一一赋值,时间长了不仅是我们而且老外也觉得映射代码很无聊啊.这个时候老外的所写的强大映射库AutoMapper横空 ...

  9. WordPress-基础设置之常规设置

    对于第一次使用Wordpress系统的朋友,请先别着急发布文章及进行其他操作,为了更加科学的使用及管理wordpress,应该需要对其进行相关设置,主要涉及3个部分,一.常规设置,二.阅读设置,三.固 ...

  10. 解决VMware下安装Ubuntu 16.04 不支持1920X1080分辨率的问题

    解决方法: flashmx@ubuntu:~$ cvt # 192.07M9) hsync: 67.16 kHz; pclk: 173.00 MHz Modeline -hsync +vsync fl ...