LeetCode 628. Maximum Product of Three Numbers (最大三数乘积)
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:
- The length of the given array will be in range [3,104] and all elements are in the range [-1000, 1000].
- 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 (最大三数乘积)的更多相关文章
- [LeetCode] 628. Maximum Product of Three Numbers 三个数字的最大乘积
Given an integer array, find three numbers whose product is maximum and output the maximum product. ...
- LeetCode 628. Maximum Product of Three Numbers三个数的最大乘积 (C++)
题目: Given an integer array, find three numbers whose product is maximum and output the maximum produ ...
- 【Leetcode_easy】628. Maximum Product of Three Numbers
problem 628. Maximum Product of Three Numbers 题意:三个数乘积的最大值: solution1: 如果全是负数,三个负数相乘还是负数,为了让负数最大,那么其 ...
- 628. Maximum Product of Three Numbers@python
Given an integer array, find three numbers whose product is maximum and output the maximum product. ...
- 【LeetCode】628. Maximum Product of Three Numbers 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:排序 日期 题目地址:https://lee ...
- [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. ...
- 628. Maximum Product of Three Numbers
Given an integer array, find three numbers whose product is maximum and output the maximum product. ...
- [LeetCode] 628. Maximum Product of Three Numbers_Easy
Given an integer array, find three numbers whose product is maximum and output the maximum product. ...
- [Array]628. Maximum Product of Three Numbers
Given an integer array, find three numbers whose product is maximum and output the maximum product. ...
随机推荐
- Play使用
play框架 打包命令: play war e:/codes/cn.ngmc.frontend -o f:/backup_ngmc/20160614frontend_001Dev; 即:p ...
- ActiveMQ_Windows版本的安装部署
1, 保证电脑上安装了jdk6以上版本的java,并配置了好环境变量 : 2, 官方下载地址:http://activemq.apache.org/download-archives.html ,这里 ...
- Java NIO vs IO
NIO :http://tutorials.jenkov.com/java-nio/index.html IO:http://tutorials.jenkov.com/java-io/index.ht ...
- SIT 和 UAT
在企业级软件的测试过程中,经常会划分为三个阶段--单元测试,SIT和UAT,如果开发人员足够,通常还会在SIT之前引入代码审查机制(Code Review)来保证软件符合客户需求且流程正确.下面简单介 ...
- JMeter关联(正则表达式提取器)
关联:与系统交互过程中,系统返回的内容,需要在接下来的交互中用到,如防止csrf攻击而生成的token. 从前一个请求中取,用Regular Expression Extractor 正则表达式提取器 ...
- 利用ASP.NET操作IIS (可以制作安装程序)
很多web安装程序都会在IIS里添加应用程序或者应用程序池,早期用ASP.NET操作IIS非常困难,不过,从7.0开始,微软提供了 Microsoft.Web.Administration 类,可以很 ...
- struts2一个和多个文件上传及下载
struts2的文件上传相比我们自己利用第三方jar包(commons-fileupload-1.2.1.jar commons-io-1.3.2.jar )要简单的多,当然struts2里面也是 ...
- CenOs 部署记录
1.安装APache.即 httpd 2.需要将80端口添加进iptable.外网才能访问.命令:iptables -I INPUT -p TCP --dport 80 -j ACCEPT
- hive自定义UDF
udf udaf udtf 使用方式 hiverc文件 1.jar包放到安装日录下或者指定目录下 2.${HIVE_HOME}/bin目录下有个.hiverc文件,它是隐藏文件. 3.把初始化语句加载 ...
- 机器学习实战K-近邻算法
今天开始学习机器学习,第一章是K-近邻算法,有不对的地方请指正 大概总结一下近邻算法写分类器步骤: 1. 计算测试数据与已知数据的特征值的距离,离得越近越相似 2. 取距离最近的K个已知数据的所属分类 ...