当前位置:网站首页>628. Maximum product of three numbers
628. Maximum product of three numbers
2022-07-06 16:07:00 【mrbone9】
Address :
Power button
https://leetcode-cn.com/problems/maximum-product-of-three-numbers/
subject :
Here's an integer array nums , Find the largest product of three numbers in the array , And output this product .
Example 1:
| Input :nums = [1,2,3] Output :6 |
Example 2:
| Input :nums = [1,2,3,4] Output :24 |
Example 3:
| Input :nums = [-1,-2,-3] Output :-6 |
Tips :
| 3 <= nums.length <= 104 -1000 <= nums[i] <= 1000 |
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/maximum-product-of-three-numbers
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Ideas :
1. All positive numbers , Take the three largest numbers
2. All negative numbers , There will be no positive results , Also take the three largest numbers
3. There are positive and negative
3.1 Only 1 Negative numbers Take the three largest numbers
3.2 redundant 2 Negative numbers 2 Maximum negative numbers + Maximum integer , Three largest integers , Taking the maximum
So the last thing is to see which is the maximum :
Minimum two numbers + The maximum number , Three maximum numbers
Method 1 、 Sort
#define max(a,b) ( (a) > (b) ? (a) : (b) )
int cmp(const void *a, const void *b)
{
return *(int *)a - *(int *)b;
}
int maximumProduct(int* nums, int numsSize){
qsort(nums, numsSize, sizeof(int), cmp);
return max(nums[0] * nums[1] * nums[numsSize-1], nums[numsSize-1] * nums[numsSize-2] * nums[numsSize-3]);
}Method 2 、 Linear search
Sorting is lossy , If you can find what you need without sorting 5 Elements :2 Minimum ,3 The biggest
Then the performance will be improved
The title gives a numerical range :-1000 <= nums[i] <= 1000
So it can be assumed that :
1. The minimum value is initialized to 1000( Maximum )
2. The maximum value is initialized to -1000( minimum value )
If you find something smaller than the minimum value during traversal , Then update the minimum ; Similar to several other elements
#define MAXVALUE 1000
#define MINVALUE -1000
#define max(a,b) ( (a) > (b) ? (a) : (b) )
int maximumProduct(int* nums, int numsSize){
int min1 = MAXVALUE, min2 = MAXVALUE;
int max1 = MINVALUE, max2 = MINVALUE, max3 = MINVALUE;
for(int i=0; i<numsSize; i++)
{
if(min1 > nums[i])
{
min2 = min1;
min1 = nums[i];
}
else if(min2 > nums[i])
{
min2 = nums[i];
}
if(max1 < nums[i])
{
max3 = max2;
max2 = max1;
max1 = nums[i];
}
else if(max2 < nums[i])
{
max3 = max2;
max2 = nums[i];
}
else if(max3 < nums[i])
{
max3 = nums[i];
}
}
return max(min1 * min2 * max1, max1 * max2 * max3);
}The method of linear search should be mastered
边栏推荐
- 渗透测试 ( 5 ) --- 扫描之王 nmap、渗透测试工具实战技巧合集
- [exercise-3] (UVA 442) matrix chain multiplication
- 信息安全-威胁检测-flink广播流BroadcastState双流合并应用在过滤安全日志
- VS2019初步使用
- Opencv learning log 33 Gaussian mean filtering
- Opencv learning log 31 -- background difference
- [exercise-6] (PTA) divide and conquer
- China exterior wall cladding (EWC) market trend report, technical dynamic innovation and market forecast
- 信息安全-史诗级漏洞Log4j的漏洞机理和防范措施
- 洛谷P1102 A-B数对(二分,map,双指针)
猜你喜欢

Matlab comprehensive exercise: application in signal and system

渗透测试 ( 1 ) --- 必备 工具、导航
![[teacher Gao UML software modeling foundation] collection of exercises and answers for level 20 cloud class](/img/57/bc6eda91f7263acda38b9ee8732318.png)
[teacher Gao UML software modeling foundation] collection of exercises and answers for level 20 cloud class

Determine the Photo Position

Analyse du format protobuf du rideau en temps réel et du rideau historique de la station B

Information security - threat detection - detailed design of NAT log access threat detection platform

【练习-7】Crossword Answers

基于web的照片数码冲印网站

信息安全-威胁检测-NAT日志接入威胁检测平台详细设计

STM32 how to use stlink download program: light LED running light (Library version)
随机推荐
Opencv learning log 19 skin grinding
Truck History
[exercise -11] 4 values why sum is 0 (and 4 values of 0)
VS2019初步使用
Opencv learning log 14 - count the number of coins in the picture (regardless of overlap)
Opencv learning log 30 -- histogram equalization
If you want to apply for a programmer, your resume should be written like this [essence summary]
Nodejs+vue online fresh flower shop sales information system express+mysql
信息安全-安全编排自动化与响应 (SOAR) 技术解析
基于web的照片数码冲印网站
b站 實時彈幕和曆史彈幕 Protobuf 格式解析
[exercise -10] unread messages
E. Breaking the Wall
栈的经典应用—括号匹配问题
最全编程语言在线 API 文档
Shell脚本编程
MySQL授予用户指定内容的操作权限
Luogu P1102 A-B number pair (dichotomy, map, double pointer)
Determine the Photo Position
[exercise-2] (UVA 712) s-trees