当前位置:网站首页>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
边栏推荐
- X-forwarded-for details, how to get the client IP
- b站 实时弹幕和历史弹幕 Protobuf 格式解析
- mysql导入数据库报错 [Err] 1273 – Unknown collation: ‘utf8mb4_0900_ai_ci’
- Matlab comprehensive exercise: application in signal and system
- Path problem before dynamic planning
- 7-1 understand everything (20 points)
- [exercise -10] unread messages
- 树莓派4B安装opencv3.4.0
- Differential (one-dimensional, two-dimensional, three-dimensional) Blue Bridge Cup three body attack
- 信息安全-威胁检测-NAT日志接入威胁检测平台详细设计
猜你喜欢

TCP's three handshakes and four waves

Borg maze (bfs+ minimum spanning tree) (problem solving report)

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

Information security - Epic vulnerability log4j vulnerability mechanism and preventive measures

Gartner: five suggestions on best practices for zero trust network access
Frida hook so layer, protobuf data analysis

Penetration test 2 --- XSS, CSRF, file upload, file inclusion, deserialization vulnerability
![[analysis of teacher Gao's software needs] collection of exercises and answers for level 20 cloud class](/img/3b/dc43564a36f82e73826b08f39c935e.png)
[analysis of teacher Gao's software needs] collection of exercises and answers for level 20 cloud class

【练习-4】(Uva 11988)Broken Keyboard(破损的键盘) ==(链表)
![mysql导入数据库报错 [Err] 1273 – Unknown collation: ‘utf8mb4_0900_ai_ci’](/img/e6/f4a696179282fe1f4193410c5a493a.png)
mysql导入数据库报错 [Err] 1273 – Unknown collation: ‘utf8mb4_0900_ai_ci’
随机推荐
Opencv learning log 12 binarization of Otsu method
[exercise-3] (UVA 442) matrix chain multiplication
Find 3-friendly Integers
Research Report on shell heater industry - market status analysis and development prospect forecast
Common configuration files of SSM framework
C basic grammar
【练习-9】Zombie’s Treasure Chest
Basic Q & A of introductory C language
Opencv learning log 15 count the number of solder joints and output
【练习-11】4 Values whose Sum is 0(和为0的4个值)
[exercise-9] Zombie's Treasury test
渗透测试 ( 8 ) --- Burp Suite Pro 官方文档
MySQL import database error [err] 1273 - unknown collation: 'utf8mb4_ 0900_ ai_ ci’
Essai de pénétration (1) - - outils nécessaires, navigation
Shell脚本编程
The concept of C language array
Gartner: five suggestions on best practices for zero trust network access
[exercise-7] crossover answers
Path problem before dynamic planning
Truck History