在数组中查找第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)

  1. Create a stack.

    创建一个堆栈。

  2. Push the first element to the stack.

    将第一个元素推入堆栈。

    For rest of the elements

    对于其余元素

  3. Set the variable nextNearestGreater to the current element.

    将变量nextNearestGreater设置为当前元素。

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

    如果stack不为空,则从堆栈中弹出一个元素,并将其与变量nextNearestGreater进行比较。

  5. 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所有弹出元素的下一个更大元素。

  6. 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个最大元素

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/546031.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

6种快速统计代码执行时间的方法,真香!(史上最全)

我们在日常开发中经常需要测试一些代码的执行时间&#xff0c;但又不想使用向 JMH&#xff08;Java Microbenchmark Harness&#xff0c;Java 微基准测试套件&#xff09;这么重的测试框架&#xff0c;所以本文就汇总了一些 Java 中比较常用的执行时间统计方法&#xff0c;总共…

fltk 库

fltk是一个小型、开源、支持OpenGL 、跨平台&#xff08;windows,linux,mac OSX)的GUI库&#xff0c;它兼容xforms 图形库&#xff08;unix/linux下的一个C语言图形库)&#xff0c;所以可以用来开发模块化的程序&#xff0c;同时也可以使用面向对象开发程序&#xff0c;使用起来…

人工智能ai 学习_人工智能中强化学习的要点

人工智能ai 学习As discussed earlier, in Reinforcement Learning, the agent takes decisions in order to attain maximum rewards. These rewards are the reinforcements through which the agent learns in this type of agent. 如前所述&#xff0c;在“ 强化学习”中 &…

第四章 计算机软件安装与调试

*(%)^*&!*第一讲 系统BIOS和CMOS参数设置&#xff08;1&#xff09;一、BIOS、CMOS的基本概念1.BIOS的含义BIOS是只读存储器基本输入/输出系统的简写&#xff0c;是被雇花道计算机主板ROM芯片上的一组程序&#xff0c;为计算机提供最低级、最直接的硬件控制。2.CMOS的含义C…

连夜整理了几个开源项目,毕设/练手/私活一条龙!

一直以来&#xff0c;总有小伙伴问说&#xff1a;诶&#xff0c;有没有什么好的项目推荐啊&#xff0c;想参考使用。一般用途无非如下几种情况&#xff1a;自学练手&#xff1a;从书本和博客的理论学习&#xff0c;过渡到实践练手吸收项目经验&#xff0c;找工作写简历时能参考…

MPI编程简单介绍

第三章 MPI编程 3.1 MPI简单介绍 多线程是一种便捷的模型&#xff0c;当中每一个线程都能够訪问其他线程的存储空间。因此&#xff0c;这样的模型仅仅能在共享存储系统之间移植。一般来讲&#xff0c;并行机不一定在各处理器之间共享存储&#xff0c;当面向非共享存储系统开发…

英语电视节目网站

最近想练习一下英语听力&#xff0c;看到了一个网站&#xff0c;感觉好像还不错&#xff0c;播放比较流畅&#xff0c;语速相对来说比较慢&#xff0c;发音比较清晰。 链接&#xff1a;CSPAN 还有更多网站见&#xff1a;Broadband-Television BON CNC 英文在线广播&#x…

三位bcd加法计数器_两个8位BCD编号的加法| 8085微处理器

三位bcd加法计数器Problem statement: 问题陈述&#xff1a; To perform addition operation between two 8-bit BCD numbers using 8085 microprocessor. 使用8085微处理器在两个8位BCD编号之间执行加法运算。 Algorithm: 算法&#xff1a; Load the two numbers in HL pai…

第五章 计算机故障诊断与排除

*(%)^*&!*第一讲 计算机故障基础及电源类故障诊断和维护一、计算机故障的分类1.硬件故障硬件故障是指用户使用不当或由于电子元件故障而引起计算机硬件不能正常运行的故障。常见的硬件故障现象包括&#xff1a;&#xff08;1&#xff09;电源故障&#xff0c;导致没有供电或…

图灵奖演讲稿

刚刚读温伯格 的《理解专业程序员》&#xff0c;书中提到Floyd 图灵奖演讲中关于编程范式(programming paradigm )&#xff08;also see here )的演讲稿值得每个与编程有关的人一读&#xff0c;所以搜索了一些图灵奖相关的一些网络资源。 图灵奖主页 部分图灵奖演讲稿 其他资…

最简单的6种防止数据重复提交的方法!(干货)

有位朋友&#xff0c;某天突然问磊哥&#xff1a;在 Java 中&#xff0c;防止重复提交最简单的方案是什么&#xff1f;这句话中包含了两个关键信息&#xff0c;第一&#xff1a;防止重复提交&#xff1b;第二&#xff1a;最简单。于是磊哥问他&#xff0c;是单机环境还是分布式…

JavaScript匿名函数与托付

<1> <html xmlns"http://www.w3.org/1999/xhtml"> <head><!-- C#匿名函数--><title></title><script type"text/javascript">var f1 function (x, y) { //【1】 定义一个匿名函数&#xff0c;用变量f1来指向它…

第六章 计算机性能测试

*(%)^*&!*第一讲 系统优化一、Windows XP系统的优化功能1.启动速度加速选择“开始→运行”选项&#xff0c;再出现的对话框中输入“msconfig”&#xff0c;然后单击“确定”按钮&#xff0c;弹出“系统配置实用程序”对话框。在启动选项卡中将不需要加载启动的程序前面的对…

c#读取整数空格_C ++程序声明,读取和打印动态整数数组

c#读取整数空格Prerequisite: new and delete operator in C 先决条件&#xff1a; C 中的new和delete运算符 Here, we will learn how to declare, read and print dynamically allocated array? 在这里&#xff0c;我们将学习如何声明&#xff0c;读取和打印动态分配的数组…

math for programmers(转载)

英文内容&#xff0c;来自http://steve-yegge.blogspot.com/2006/03/math-for-programmers.html 翻译版见这里 相关内容见c2.com 原文内容如下&#xff1a; Ive been working for the past 15 months on repairing my rusty math skills, ever since I read a biography o…

漫画:如何证明sleep不释放锁,而wait释放锁?

wait 加锁示例public class WaitDemo {private static Object locker new Object();public static void main(String[] args) throws InterruptedException {WaitDemo waitDemo new WaitDemo();// 启动新线程&#xff0c;防止主线程被休眠new Thread(() -> {try {waitDemo…

设计模式 之 建造者

建造者模式&#xff08;Builder Pattern&#xff09; 一听这个名字&#xff0c;你可能就会猜到一二分了。建造者简单理解就是造东西&#xff0c;仅仅只是建造者模式建造的不是一个简单的东西&#xff0c;是一个比較复杂的东西。就好像盖房子&#xff0c;须要打地基、砌墙、灌…

stl string 函数_使用C ++ STL中的string :: append()函数将文本追加到字符串

stl string 函数append() is a library function of <string> header, it is used to append the extra characters/text in the string. append()是<string>标头的库函数&#xff0c;用于在字符串中附加多余的字符/文本。 Syntax: 句法&#xff1a; string&…

ABCDE类IP地址的解释

A类地址第1字节为网络地址&#xff0c;其它3个字节为主机地址。另外第1个字节的最高位固定为0。 A类地址范围&#xff1a;1.0.0.0到127.255.255.255。 A类地址中的私有地址和保留地址&#xff1a; 10.0.0.0到10.255.255.255是私有地址&#xff08;所谓的私有地址就是在互联网上…

身体健康小窍门

身体健康对于每个人来说都是第一重要的&#xff0c;找到的一些健康小窍门&#xff0c;可能有帮助&#xff1a; 1&#xff09;单鼻呼吸&#xff08; 来源 &#xff09;&#xff1a;中午以前最好常练习用左鼻子呼吸&#xff0c;没事的时候你就用手很自然的托住右边面颊&#xf…