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. ...
随机推荐
- 如何为已有的类没有生成toString的方法增强生成toString方法
1:只要提到增强,我的第一思路就是代理,动态代理.但是仅仅是一个toString其实没必要使用代理模式了,有点大材小用了(动态代理其实也是最后通过反射生成toString的方法). 2:简单粗暴,可以 ...
- AddHandler php5-script .php\AddType text/html .php和AddType application/x-httpd-php .php的区别?
让apache支持php文件的解释,有2种方法配置,RPM装的默认配置是:AddHandler php5-script .phpAddType text/html .php网上很多人的配置方法是:Ad ...
- Jmeter核心-hc课堂笔记
自动化测试平台-Httprunner-接口.UI.协议. 平台语言-JAVA-UI-Selenium(java版的).Appium(java版的).接口-Httpclient.Jmeter.(Jmet ...
- 简单的表格json控件
简单的表格json控件 由于最近做的项目一直有表格的形式展示数据,所以想写个简单的关于表格方面的控件出来,想用JSON数据直接渲染出来,因为开发给到我们前端的字段可能会叫不同的名字,所以我们前端渲染页 ...
- 开关电源PCB设计中的布线技巧
开关电源PCB设计中的布线技巧关键字:布线 开关电源 走线 一.引言 开关电源是一种电压转换电路,主要的工作内容是升压和降压,广泛应用于现代电子产品.因为开关三极管总是工作在 “开” 和“关” 的状态 ...
- 有关java(初学笔记)
JAVA的主要优势:跨平台性,可以在Linux,windows,mac三个系统上运行. 跨平台的核心:JAVA虚拟机--JVM 原理就是将Java语言在这个系统上翻译.JAVA在jvm上运行,jvm进 ...
- 在 R 中估计 GARCH 参数存在的问题(基于 rugarch 包)
目录 在 R 中估计 GARCH 参数存在的问题(基于 rugarch 包) 导论 rugarch 简介 指定一个 \(\text{GARCH}(1, 1)\) 模型 模拟一个 GARCH 过程 拟合 ...
- PI monitor error process-RESOURCE_NOT_FOUND-转
事务:sxi_monitor 状态:system error 类型:Request Message Mapping 错误简要:RESOURCE_NOT_FOUND 错误详细信息: <?xml v ...
- HTTPUTILS
maven依赖 <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId&g ...
- 把Excel的数据导入到数据库
将Excel作为数据源,将数据导入数据库,是SSIS的一个简单的应用,下图是示例Excel,数据列是code和name 第一部分,Excel中的数据类型是数值类型 1,使用SSDT创建一个packag ...