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. ...
随机推荐
- app的描述-软件的描述
app的描述=需求文档+接口文档+程序架构+工程结构. 程序架构:类结构图: 需求文档:业务逻辑-->时序图.
- Netty入门(四)ByteBuf 字节级别的操作
Netty 中使用 ByteBuf 代替 Java NIO 提供的 ByteBuffer 作为字节的容器. 一.索引 ByteBuf 提供两个指针变量支持读和写操作,读操作使用 readerInde ...
- BZOJ1121:[POI2008]激光发射器SZK(乱搞)
Description 多边形相邻边垂直,边长为整数,边平行坐标轴.要在多边形的点上放一些激光发射器和接收器.满足下列要求: 1发射器和接收器不能放置在同一点: 2发射器发出激光可以沿壁反射,最终到达 ...
- android与JS交互,互相调用方法,跳转到网页
在main下面New - Folder - Assets Folder,在Assets下面新建一个js_android.html <html><head> <meta h ...
- browerify初步了解
之前在写Signature Request Warnings & eth_sign学习的时候在里的signing examples时了解到browserify工具,可以通过这个例子学习如何使用 ...
- linux问题解答
1.Linux如何查询进程?杀死一个进程? ps命令提供进程的一次性查看(瞬时信息),结果不是动态的:top对命令实时监控 ps只是查看进程,而top还可以监视系统性能,如平均负载,cpu和内存的消耗 ...
- 2018-2019-2 20165302 《网络对抗技术》Exp4 恶意代码分析
实验要求 1.监控你自己系统的运行状态,看有没有可疑的程序在运行 2.分析一个恶意软件,就分析Exp2或Exp3中生成后门软件:分析工具尽量使用原生指令或sysinternals,systracer套 ...
- opencv中的bitwise_not,bitwise_xor,bitwise_or,bitwise_and的使用方法与效果。
1.将二指图片的效果反转既黑色变白色,白色变黑色. 使用 bitwise_not(InputArray src, OutputArray dst, InputArray mask = noArray( ...
- 八款开源 Android 游戏引擎 (巨好的资源)
转载地址:http://software.intel.com/zh-cn/blogs/2012/01/13/android-4 初学Android游戏开发的朋友,往往会显得有些无所适从,他们常常不知道 ...
- 使用Android绘图技术绘制一个椭圆形,然后通过触摸事件让该椭圆形跟着手指移动
引言:在图形绘制中,控制一个图形(圆形,椭圆形,矩形,三角形)移动时,其实计算的都是该图形的中心点移动.在绘制过程中,首先计算出中心点,然后根据中心点的位置计算重图形的上下左右位置.我们假设图形左边为 ...