LeetCode 628. Maximum Product of Three Numbers三个数的最大乘积 (C++)
题目:
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
分析:
给定一个数组,返回其中三个元素乘积的最大值。
注意的是,这道题是可以有负数出现,且是求三个数的乘积,所以我们需要考虑负数的情况。
最先想到的是利用排序解决,我们要比较最大的三个数的乘积和最大的数还有最小的两个数的乘积,也就是求max(max1*max2*max3,max1*min1*min2),因为如果最小的两个数是负数且他们的绝对值很大,这样的乘积也会是大的,要考虑这种情况。
因为实际上我们的答案仅需要5个数就可以解决,我们可以在遍历数组的时候求出来,从而节省排序的时间,降低时间复杂度。
程序:
// sort
class Solution {
public:
int maximumProduct(vector<int>& nums) {
sort(nums.begin(), nums.end(), greater<int>());
int n = nums.size();
return max(nums[]*nums[]*nums[], nums[]*nums[n-]*nums[n-]);
}
};
class Solution {
public:
int maximumProduct(vector<int>& nums) {
int min1 = INT_MAX, min2 = INT_MAX;
int max1 = INT_MIN ,max2 = INT_MIN, max3 = INT_MIN;
for(auto i:nums){
if(i > max1){
max3 = max2;
max2 = max1;
max1 = i;
}
else if(i > max2){
max3 = max2;
max2 = i;
}
else if(i > max3)
max3 = i;
if(i < min1){
min2 = min1;
min1 = i;
}
else if(i < min2)
min2 = i;
}
return max(max1*max2*max3, max1*min1*min2);
}
};
// class Solution {
// public:
// int maximumProduct(vector<int>& nums) {
// priority_queue<int, vector<int>, less<int> > p;
// priority_queue<int, vector<int>, greater<int> > q;
// for(auto i:nums){
// p.push(i);
// q.push(i);
// }
// int minn[2] = {0};
// int maxn[3] = {0};
// for(int i = 0; i < 3; ++i){
// maxn[i] = p.top();
// p.pop();
// }
// for(int i = 0; i < 2; ++i){
// minn[i] = q.top();
// q.pop();
// }
// return max(maxn[0]*maxn[1]*maxn[2], maxn[0]*minn[0]*minn[1]);
// }
// };
LeetCode 628. Maximum Product of Three Numbers三个数的最大乘积 (C++)的更多相关文章
- [LeetCode] 628. Maximum Product of Three Numbers 三个数字的最大乘积
Given an integer array, find three numbers whose product is maximum and output the maximum product. ...
- Leetcode628.Maximum Product of Three Numbers三个数的最大乘积
给定一个整型数组,在数组中找出由三个数组成的最大乘积,并输出这个乘积. 示例 1: 输入: [1,2,3] 输出: 6 示例 2: 输入: [1,2,3,4] 输出: 24 注意: 给定的整型数组长度 ...
- LeetCode 628. Maximum Product of Three Numbers (最大三数乘积)
Given an integer array, find three numbers whose product is maximum and output the maximum product. ...
- 【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] 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 解题报告(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. ...
- [Array]628. Maximum Product of Three Numbers
Given an integer array, find three numbers whose product is maximum and output the maximum product. ...
随机推荐
- maven的下载与安装
下载路径:http://maven.apache.org/download.cgi 选择打红线的进行下载,我用的版本是3.3.9, 下载后解压该文件,解压后的文件内容如下: 解压完成后配置maven的 ...
- U-Mail:如何实现EDM的个性化和定制化?
设想一下,一个上班族一天要接到多少垃圾邮件?据媒体报道,目前来往的邮件中,高达95%以上的是垃圾邮件,而且有些垃圾邮件还会故意占据着邮箱的最前列.同时,随着人们接受资讯越来越快捷便利,渠道越来越多,也 ...
- BeanDefinition及其实现类
[转自 http://blog.csdn.net/u011179993 ] 目录(?)[+] 一. BeanDefinition及其实现类 BeanDefinition接口 这个接口描述bea ...
- 金三银四求职季,前端面试题小梳理(HTML、CSS、JS)
好久没写学习记录,最近太多事,又到一年求职季,都说金三银四求职季,自己也做一下最近学习的一些前端面试题梳理,还是个小白,写的不对请指正,不胜感激. HTML篇 html语义化 用语义化的代码标签书写, ...
- mac下idea 13 在tomcat 7控制台乱码
在mac或linux下idea 13(可能其它版本也会出现乱码) tomcat 7在输出到控制台的日志中文乱码,解决方式 加一个environment variable, 在如图绿色位置添加 JA ...
- 2-8 R语言基础 日期与时间
#日期 Date > x<-date()> class(x)[1] "character" > x2 <- Sys.Date()> class( ...
- [luogu3980] 志愿者招募
题面 又一次考试网络流爆零...... 这一题一看就是网络流, 但是要怎么构图呢? 考虑到途中的一些因素, 首先, 每一种志愿者控制的区间范围为\(S_{i}\)到\(T_{i}\), 所以, ...
- Excel操作
区间范围计算 方法一:用IF函数 方法二:构建一个辅助区域,用VLOOKUP函数 方法一:用IF函数 在F3中输入:=IF(E3>=90%,5%,IF(E3>=80%,4%,IF(E3&g ...
- MSMQ消息队列总结
1.总体介绍: http://www.cnblogs.com/beniao/archive/2008/06/26/1229934.html 2.windows服务各项参数介绍及安装 https://w ...
- scrollIntoView()的用法
scrollIntoView是一个与页面(容器)滚动相关的API(官方解释),该API只有boolean类型的参数能得到良好的支持(firefox 36+都支持),所以在这里只讨论参数Boolean类 ...