日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

在数组中查找第k个最大元素_查找数组中每个元素的最近最大邻居

發(fā)布時(shí)間:2025/3/11 编程问答 70 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在数组中查找第k个最大元素_查找数组中每个元素的最近最大邻居 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

在數(shù)組中查找第k個(gè)最大元素

Problem statement:

問(wèn)題陳述:

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).

給定一個(gè)元素?cái)?shù)組,請(qǐng)找到該數(shù)組中每個(gè)元素中最近(最右邊)的最大元素。 (最接近的最大表示右側(cè)最接近的最大一個(gè))。

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.

一種簡(jiǎn)單的蠻力方法是掃描每個(gè)元素的整個(gè)右側(cè)部分,并找到最接近的最上位。 這種方法的計(jì)算復(fù)雜度為O(n 2 ),不是線性的。

A better solution exists which can be done using stack data structure.

存在更好的解決方案,可以使用堆棧數(shù)據(jù)結(jié)構(gòu)來(lái)完成。

使用堆棧的更好方法 (Better approach using stack)

  • Create a stack.

    創(chuàng)建一個(gè)堆棧。

  • Push the first element to the stack.

    將第一個(gè)元素推入堆棧。

    For rest of the elements

    對(duì)于其余元素

  • Set the variable nextNearestGreater to the current element.

    將變量nextNearestGreater設(shè)置為當(dāng)前元素。

  • If stack is not empty, pop an element from the stack and compare it to the variable nextNearestGreater.

    如果stack不為空,則從堆棧中彈出一個(gè)元素,并將其與變量nextNearestGreater進(jìn)行比較。

  • 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是彈出元素的下一個(gè)更大元素。 打印它。 繼續(xù)彈出堆棧,直到彈出元素小于nextNearestGreater (直到堆棧不為空)為止。 nextNearestGreateris所有彈出元素的下一個(gè)更大元素。

  • Else push the popped element.

    否則按一下彈出的元素。

  • .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}} .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}}

    C ++程序查找數(shù)組中每個(gè)元素的最近最大鄰居 (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

    在數(shù)組中查找第k個(gè)最大元素

    總結(jié)

    以上是生活随笔為你收集整理的在数组中查找第k个最大元素_查找数组中每个元素的最近最大邻居的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。