题目:

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. Resharper安装使用手册

    今天在博客园上看到一位大牛写了一遍关于.NET代码优化的文章,其中提到了Resharper这个工具,以前没使用过这个工具,突然想用这个工具来检查一下自己之前代码的规范程度,也是为了写出更规范的代码. ...

  2. 移动端真机调试抓包,fiddler web debugger

    小白一枚,在公司大神指导下加之找了好多资料才勉强将fiddler的使用摸透,果然很好用. 一.设置手机 二.设置fiddler

  3. 【Alpha 冲刺】 11/12

    今日任务总结 人员 今日原定任务 完成情况 遇到问题 贡献值 胡武成 完成app端api编写 未完成 文件上传api还没完成 孙浩楷 1. 与后端交接, 2. 完成图片在线编辑插件引入 未完成 陷入僵 ...

  4. day4-课堂代码

    # ---------------------------------------------------------------------- # def my_function1(name, ad ...

  5. PNS settings

    Before PNS,some settings should be ready: firstly,make sure that the power/ground nets exist,if not, ...

  6. Python2.7-copy

    copy 模块,python 中的‘=’是使左边的对象成为右边对象的一个引用,对不可变对象(如数字,字符串等)使用‘=’一般不会出现问题,但当对字典,列表等可变对象进行‘=’操作时,要注意修改其中一个 ...

  7. Win7系统的任务管理器显示不全

    Win7系统的任务管理器显示不全:如下图所示 首先要说明的是:出现这种现象是正常的,因为这种样式是任务管理器的精简模式. 还原方法:双击任务管理器四周的空白边框,即可恢复初始模式. 效果图如下:

  8. 609E- Minimum spanning tree for each edge

    Connected undirected weighted graph without self-loops and multiple edges is given. Graph contains n ...

  9. chrome调试如何禁用浏览器缓存

    0.写在前面的话 遇到过很多很多次,修改了页面代码,但是程序始终没有按照设想的方向走,有时候折腾了几个小时,发现问题最后却是莫名其妙恢复的.后来进一步调试发现,自己已经修改了如js代码,但是前端在载入 ...

  10. jQueryMobile的按钮样式

    好吧,已经学了jQueryMobile一年了,今天心血来潮,想要写一篇关于jQueryMobile的博客文章,记得去年暑假在公司实习jQueryMobile,想一想真是怀念当时还是菜鸟的自己,年轻就是 ...