当前位置:网站首页>7.2 brush two questions
7.2 brush two questions
2022-07-03 07:12:00 【Just a garbage runner】
The maximum product of three numbers
628. The maximum product of three numbers
Sort
After sorting, process according to the situation
- When all numbers are negative or positive , We just choose the largest last three digits to multiply .
- When there is only one negative number - Directly multiply the last three digits .
- When there are negative numbers but there are still three positive numbers . There are two cases, the largest of which is .
- Multiply the minimum value of two negative numbers by the maximum value of an integer
- Multiply by three integers
- When there are too many negative numbers to have three integer positive numbers
- Multiply the minimum value of two negative numbers by the maximum value of an integer
It can be seen from the above situation 3 The situation is the most judged . But the situation is 3 Yes, the maximum value is compatible to 124 So in fact, we only need to judge
- The minimum and minor values in negative numbers ( It is also the overall minimum ) Multiply by the maximum
- Maximum , Second largest value , Multiply next large value
In the above two cases, the value of that case is the largest .
class Solution {
public:
int maximumProduct(vector<int>& nums)
{
sort(nums.begin(),nums.end());
int s = nums.size()-1;
return max(nums[0]*nums[1]*nums[s],nums[s]*nums[s-1]*nums[s-2]);// There are only two cases in which the answer we want can directly return to the maximum .
}
};
Linear scanning
The idea is similar to the above question, but we don't get the value by sorting , We use linear scanning to get .
class Solution {
public:
int maximumProduct(vector<int>& nums)
{
int min1 = INT_MAX,min2 = INT_MAX;//min1 Is the minimum min2 Is the maximum
int max1 = INT_MIN,max2 = INT_MIN,max3 = INT_MIN;//max1 Is the maximum value. .....
for(int i =0;i<nums.size();i++)
{
if(nums[i]<min1)
{
min2 = min1;// Give Way min2 It's a small value
min1 = nums[i];//min1 Is the minimum
}
else if(nums[i]<min2)
{
min2 = nums[i];
}
if(nums[i]>max1)
{
max3 = max2;
max2 = max1;
max1 = nums[i];
}
else if(nums[i]>max2)
{
max3 = max2;
max2 = nums[i];
}
else if(nums[i]>max3)
{
max3 = nums[i];
}
}
return max(min1*min2*max1,max1*max2*max3);// Similar to the above idea, but these values do not come from sorting
}
};
How many numbers are smaller than the current number
1365. How many numbers are smaller than the current number
Hash thought
Mark it : I haven't learned hashing , The hashing idea of this title is actually that I have said after several times that this is hashing idea, so there is this title
Ideas :
Put the array ( Array 1 And the given array ) Put the number of in another array ( I become an array 2) in , Convert his value into an array 2 The subscript position of , And use arrays 2 Record the number of his appearances .
Then we just need to put the corresponding array 1 Convert the value of to an array 2 in , Add the contents of the subscript in front of the corresponding subscript of array two .
For example, array 1 There are elements in it 8, We just need to put the array 2 Add the first eight subscript values in (0,1,2,3,4,5,6,7).
class Solution {
public:
vector<int> smallerNumbersThanCurrent(vector<int>& nums)
{
vector<int> ret(nums.size());
int arr[101] = {
0};// Using hash ideas
for(int i = 0;i<nums.size();i++)
{
arr[nums[i]]++;
}
for(int i =0;i<nums.size();i++)
{
int tmp =0;
for(int j =0;j<nums[i];j++)
{
tmp += arr[j];
}
ret[i] = tmp;
}
return ret;
}
};
边栏推荐
- JMeter test result output
- [vscode - vehicle plug-in reports an error] cannot find module 'xxx' or its corresponding type declarations Vetur(2307)
- [plus de détails] dernière entrevue complète redis (50)
- Strategy mode
- My 2020 summary "don't love the past, indulge in moving forward"
- IC_ EDA_ All virtual machine (rich Edition): questasim, vivado, VCs, Verdi, DC, Pt, spyglass, icc2, synthesize, innovative, ic617, mmsim, process library
- 在 4EVERLAND 上存储 WordPress 媒体内容,完成去中心化存储
- 691. 立方体IV
- EasyExcel
- MySQL mistakenly deleted the root account and failed to log in
猜你喜欢
POI excel percentage
Notes on the core knowledge of Domain Driven Design DDD
La loi des 10 000 heures ne fait pas de vous un maître de programmation, mais au moins un bon point de départ
Software testing assignment - the next day
How to specify the execution order for multiple global exception handling classes
[solved] unknown error 1146
JMeter JSON extractor extracts two parameters at the same time
2022 - 06 - 23 vgmp - OSPF - Inter - Domain Security Policy - nat Policy (Update)
JMeter test result output
High concurrency memory pool
随机推荐
[set theory] equivalence classes (concept of equivalence classes | examples of equivalence classes | properties of equivalence classes | quotient sets | examples of quotient sets)*
[set theory] partition (partition | partition example | partition and equivalence relationship)
Book recommendation~
【已解决】SQLException: Invalid value for getInt() - ‘田鹏‘
How can I split a string at the first occurrence of “-” (minus sign) into two $vars with PHP?
File operation serialization recursive copy
2022-06-23 VGMP-OSPF-域間安全策略-NAT策略(更新中)
Flask Foundation
在 4EVERLAND 上存储 WordPress 媒体内容,完成去中心化存储
Class and object summary
Search engine Bing Bing advanced search skills
深度学习参数初始化(一)Xavier初始化 含代码
691. Cube IV
Deep learning parameter initialization (I) Xavier initialization with code
Unit test notes
The essence of interview
VMware virtual machine C disk expansion
【最详细】最新最全Redis面试大全(50道)
Setting up the development environment of dataworks custom function
3311. 最长算术