在数组中查找第k个最大元素_查找数组中每个元素的最近最大邻居
在數組中查找第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 stlint e,nextNearestGreater; //create variable nextNearestGreater s.push(a[0]); // push the first elementfor(int i=1;i<n;i++){ // for the rest of the array// set nextNearestGreater to the current elementnextNearestGreater=a[i]; //if stack is not emptyif(!s.empty()){ e=s.top();// pop from stacks.pop(); // if nextNearestGreater is greater than popped elementwhile(e<nextNearestGreater){ // replacing the current element with nextNearestGreater //as it's the next greater elementcout<<"nearest greater element of "<<e<<" is "<<nextNearestGreater<<endl; if(s.empty())break;e=s.top();// continue popping till nextNearestGreater is greaters.pop(); }// if popped element is greater than nextNearestGreater then push to stackif(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 lengthcout<<"enter no of elements\n"; cin>>n;int* a=(int*)(malloc(sizeof(int)*n));//fill the arraycout<<"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個最大元素
總結
以上是生活随笔為你收集整理的在数组中查找第k个最大元素_查找数组中每个元素的最近最大邻居的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java StackTraceEleme
- 下一篇: Java ObjectInputStre