在数组中查找第k个最大元素
Problem statement:
问题陈述:
Given an array of elements, find the nearest (on the right) greatest element ofeach element in the array. (Nearest greatest means the immediate greatest one on the right side).
给定一个元素数组,请找到该数组中每个元素中最近(最右边)的最大元素。 (最接近的最大表示右侧最接近的最大一个)。
Solution:
解:
Brute force approach:
蛮力法:
One simple brute force approach is to scan the entire right part for each element and to find the nearest greatestone. Such approach has a computational complexity of O (n2) which is not linear.
一种简单的蛮力方法是扫描每个元素的整个右侧部分,并找到最接近的最上位。 这种方法的计算复杂度为O(n 2 ),不是线性的。
A better solution exists which can be done using stack data structure.
存在更好的解决方案,可以使用堆栈数据结构来完成。
使用堆栈的更好方法 (Better approach using stack)
Create a stack.
创建一个堆栈。
Push the first element to the stack.
将第一个元素推入堆栈。
For rest of the elements
对于其余元素
Set the variable nextNearestGreater to the current element.
将变量nextNearestGreater设置为当前元素。
If stack is not empty, pop an element from the stack and compare it to the variable nextNearestGreater.
如果stack不为空,则从堆栈中弹出一个元素,并将其与变量nextNearestGreater进行比较。
If nextNearestGreateris greater than the popped element, then nextNearestGreateris the next greater element for the popped element. Print it. Keep popping up the stack till popping elementsare smaller than nextNearestGreater (till stack is not empty). nextNearestGreateris next greater element for all the popped elements.
如果nextNearestGreateris大于弹出元素,则nextNearestGreateris是弹出元素的下一个更大元素。 打印它。 继续弹出堆栈,直到弹出元素小于nextNearestGreater (直到堆栈不为空)为止。 nextNearestGreateris所有弹出元素的下一个更大元素。
Else push the popped element.
否则按一下弹出的元素。
C ++程序查找数组中每个元素的最近最大邻居 (C++ program to Find Nearest Greatest Neighbours of each element in an array)
#include<bits/stdc++.h>
using namespace std;
void print(int* a,int n){
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<endl;
}
void replace(int* a,int n){
int i=0;
stack<int> s; //craeting a stack using stl
int e,nextNearestGreater; //create variable nextNearestGreater
s.push(a[0]); // push the first element
for(int i=1;i<n;i++){ // for the rest of the array
// set nextNearestGreater to the current element
nextNearestGreater=a[i];
//if stack is not empty
if(!s.empty()){
e=s.top();
// pop from stack
s.pop();
// if nextNearestGreater is greater than popped element
while(e<nextNearestGreater){
// replacing the current element with nextNearestGreater
//as it's the next greater element
cout<<"nearest greater element of "<<e<<" is "<<nextNearestGreater<<endl;
if(s.empty())
break;
e=s.top();
// continue popping till nextNearestGreater is greater
s.pop();
}
// if popped element is greater than nextNearestGreater then push to stack
if(e>nextNearestGreater)
s.push(e);
}
s.push(nextNearestGreater);
}
while(!s.empty()){
e=s.top();
s.pop();
cout<<"nearest greater element of "<<e<< " is "<<e<< " (no nearest greater number on the right side)"<<endl; //since no number is greater in right of e
}
}
int main(){
int n;
// enter array length
cout<<"enter no of elements\n";
cin>>n;
int* a=(int*)(malloc(sizeof(int)*n));
//fill the array
cout<<"enter elements................\n";
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
cout<<"finding nearest greater numbers for each elements.............\n"<<endl;
replace(a,n);
//print(a,n);
return 0;
}
Output
输出量
enter no of elements
8
enter elements................
12
10
8
15
17
16
20
2
finding nearest greater numbers for each elements.............
nearest greater element of 8 is 15
nearest greater element of 10 is 15
nearest greater element of 12 is 15
nearest greater element of 15 is 17
nearest greater element of 16 is 20
nearest greater element of 17 is 20
nearest greater element of 2 is 2 (no nearest greater number on the right side)
nearest greater element of 20 is 20 (no nearest greater number on the right side)
翻译自: https://www.includehelp.com/algorithms/find-nearest-greatest-neighbours-of-each-element-in-an-array.aspx
在数组中查找第k个最大元素