生产领料、退料频繁
Problem statement:
问题陈述:
Given an array of integers, find and print the maximum number of integers you can select from the array such that the absolute difference between any two of the chosen integers is less than or equal to 1.
给定一个整数数组,查找并打印可以从数组中选择的最大整数数,以使任意两个选定整数之间的绝对差小于或等于1。
Examples:
例子:
Input array:
6, 4, 6, 8, 5, 9, 9, 9, 10, 3
Output:
4
Example with explanation:
带有说明的示例:
The input array is: 6, 4, 6, 8, 5, 9, 9, 9, 10, 3
输入数组为:6,4,6,8,5,9,9,9,10,3
No we can pick up different sets for which the absolute difference between any two numbers in the set is 1.
不,我们不能选择集合中任意两个数字的绝对差为1的不同集合。
Possible sets are:
可能的设置有:
{6, 6, 5}
{4, 5}
{8, 9, 9, 9}
{9, 9, 9, 10}
{4, 3}
Thus maximum no of picked up elements is: 4
因此,拾取元素的最大数量为: 4
Algorithm:
算法:
This problem can be implemented with help of map data structure. We have used STL for map implementation. (For details regarding STL map, C++ STL Map)
可以借助地图数据结构来实现此问题。 我们已经使用STL来实现地图。 (有关STL映射, C ++ STL映射的详细信息)
FUNCTION pickingNumbers(input array)
1. Declare map<int,int>m to store key with their frequencies;
2. Build the map.
For i=0:length of array
m[array[i]]++;
3. Declare max as INT_MIN;
4. Declare map<int,int>::iteratorit;
5. For(it=m.begin();it!=m.end();it++)
IF (it+1==m.end()) //last case
IF(it->second>max)
max=it->second;
END IF
ELSE IF(it->first+1==(it+1)->first){ //absolute difference is 1
IF((it->second +(it+1)->second)>max)
max=it->second +(it+1)->second;
END IF
ELSE
IF(it->second>max) //absolute difference 0 case
max=it->second;
END IF
END IF-ELSE
END FOR
6. return max;
END FUNCTION
Algorithm is pretty simple.
算法非常简单。
We first extract the unique numbers and store their frequencies. Then we simply check for two unique number's additive frequency or any one unique number's frequency itself and return the greater one.
我们首先提取唯一数字并存储其频率。 然后,我们只需检查两个唯一数字的加法频率或任何一个唯一数字的频率本身,然后返回较大的一个。
Let's solve the above example.
让我们解决以上示例。
The input array is: 6, 4, 6, 8, 5, 9, 9, 9, 10, 3
Map m:
Key Frequency
3 1
4 1
5 1
6 2
8 1
9 3
10 1
So if we do all the iterations then each iteration,
maxgets to be updated(or not, keeps last value)
From this map, we can see max is 4
1+3 //one 8 and three 9
3+1 //three 9 and one 10
Now lets think that we append six 12 to the array
Thus input is now: 6, 4, 6, 8, 5, 9, 9, 9, 10, 3, 12, 12, 12, 12, 12, 12
Map m:
Key Frequency
3 1
4 1
5 1
6 2
8 1
9 3
10 1
12 6
Now the max will be 6 //absolute difference 0 case
Since the subset will be {12, 12, 12, 12, 12, 12}
C++ implementation:
C ++实现:
#include <bits/stdc++.h>
using namespace std;
int pickingNumbers(vector<int> a)
{
map<int,int> m;
for(int i=0;i<a.size();i++)
m[a[i]]++;
int max=INT_MIN;
map<int,int>::iterator it;
for(it=m.begin();it!=m.end();it++){
//std::next(it) points to it+1
if((std::next(it))==m.end()){
if(it->second>max)
max=it->second;
}
else if(it->first+1==(std::next(it))->first){
if((it->second +(std::next(it))->second)>max)
max=it->second +(std::next(it))->second;
}
else{
if(it->second>max)
max=it->second;
}
}
return max;
}
int main(){
int n,item;
cout<<"Enter number of elements in the array\n";
cin>>n;
vector<int> a;
cout<<"enter numbers\n";
for(int i=0;i<n;i++){
cin>>item;
a.push_back(item);
}
cout<<"Maximum no of such numbers can be picked: "<<pickingNumbers(a)<<endl;
return 0;
}
Output
输出量
First run:
Enter number of elements in the array
10
enter numbers
6 4 6 8 5 9 9 9 10 3
Maximum no of such numbers can be picked: 4
Second run:
Enter number of elements in the array
16
enter numbers
6 4 6 8 5 9 9 9 10 3 12 12 12 12 12 12
Maximum no of such numbers can be picked: 6
翻译自: https://www.includehelp.com/icp/picking-numbers.aspx
生产领料、退料频繁