题目:

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++)的更多相关文章

  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. Leetcode628.Maximum Product of Three Numbers三个数的最大乘积

    给定一个整型数组,在数组中找出由三个数组成的最大乘积,并输出这个乘积. 示例 1: 输入: [1,2,3] 输出: 6 示例 2: 输入: [1,2,3,4] 输出: 24 注意: 给定的整型数组长度 ...

  3. LeetCode 628. Maximum Product of Three Numbers (最大三数乘积)

    Given an integer array, find three numbers whose product is maximum and output the maximum product. ...

  4. 【Leetcode_easy】628. Maximum Product of Three Numbers

    problem 628. Maximum Product of Three Numbers 题意:三个数乘积的最大值: solution1: 如果全是负数,三个负数相乘还是负数,为了让负数最大,那么其 ...

  5. 628. Maximum Product of Three Numbers@python

    Given an integer array, find three numbers whose product is maximum and output the maximum product. ...

  6. [LeetCode] Maximum Product of Three Numbers 三个数字的最大乘积

    Given an integer array, find three numbers whose product is maximum and output the maximum product. ...

  7. 【LeetCode】628. Maximum Product of Three Numbers 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:排序 日期 题目地址:https://lee ...

  8. [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. ...

  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. 详解--从地址栏输入url到页面展现中间都发生了什么?

    这是一个综合性很强的问题,个人理解包含以下七个基本点: 1.在浏览器地址栏输入url并按下回车. 2.浏览器检查当前url是否存在缓存和缓存是否过期. 3.域名解析(DNS解析url对应的ip). 4 ...

  2. webservice 客户端调用

    /** * 通过webserevice下发工单 * @param url * @param method * @param requestMap * @return * @throws Service ...

  3. lambda函数详解

    lambda函数的作用就是可以编写内嵌的无名函数,而不必写成独立的函数: 结构:[]为开始,(参数){函数体} 例如: 1. auto lambdaFun1 = [](int a) {std::cou ...

  4. 为什么给GIT库打TAG不成功

    首先,右击文件夹,选择“TortoiseGit”,再选择“Create Tag...".   然后在Tag一栏中输入Tag名.   接下来点”OK“确定设置项.   最后PUSH到服务器上, ...

  5. Android 将拼接好并加上边框的图片保存到内存卡中

    通过前两篇文章,问们学会了怎样拼接图片.给拼接好的图片加上边框样式,但这还不够,忙活了大半天 终于拼接好并给图片美化了,但是程序一旦推出,之前做的工作都白费了.这时我们会想,能不能把拼接好的图片保存起 ...

  6. MVC bundle的使用总结

    在我们的项目里面充斥着很多静态文件,为了追求模块化.插件化很多静态文件都被设计成模块的方式或者被分解,在需要的时候在通过组合的方式在UI层上使用:这就带来一个问题,文件多了会影响浏览器加载页面的速度, ...

  7. 学会如何使用Github进行托管代码和用markdown撰写心得

    这次作业是学会如何使用Github进行托管代码和用markdown撰写心得. 1.掌握使用Git进行代码版本,使用github进行代码托管.(git使用,推荐imooc公开课: 公开课 ) 登录 gi ...

  8. shell编程的笔记

    关于历史命令 Shell相关 !! 是上一次执行的命令 !字符串  指执行最近一次以这个字符串开头的命令 !$ 取出上一条命令最后一个单词或命令 *匹配0个或多个字符         ?匹配一个字符 ...

  9. Pythoner使用的豆瓣pip源

    主要示例: sudo pip install -i http://pypi.douban.com/simple/  flask-script Flask的扩展: flask-script是一个可以在f ...

  10. 【转】python直接运行tcl脚本

    python中调用tcl是通过加载TkInter来实现的. from Tkinter import Tcl tcl = Tcl() tcl.eval('source tu.tcl') tcl.eval ...