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

    否则按一下弹出的元素。

.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 ++程序查找数组中每个元素的最近最大邻居 (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个最大元素

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

  1. java查找第k大的数字_查找数组中第k大的数

    问题:  查找出一给定数组中第k大的数.例如[3,2,7,1,8,9,6,5,4],第1大的数是9,第2大的数是8-- 思考:1. 直接从大到小排序,排好序后,第k大的数就是arr[k-1]. 2. ...

  2. python查找第k大的数_寻找数组中第K大的数

    给定一个数组A,要求找到数组A中第K大的数字.对于这个问题,解决方案有不少,此处我只给出三种: 方法1: 对数组A进行排序,然后遍历一遍就可以找到第K大的数字.该方法的时间复杂度为O(N*logN) ...

  3. java查找第k大的数字_[经典算法题]寻找数组中第K大的数的方法总结

    今天看算法分析是,看到一个这样的问题,就是在一堆数据中查找到第k个大的值. 名称是:设计一组N个数,确定其中第k个最大值,这是一个选择问题,当然,解决这个问题的方法很多,本人在网上搜索了一番,查找到以 ...

  4. java求数组中满足给定和的数对_关于数组的几道面试题 - zdd - 博客园

    2011年2月15日更新,加入找出绝对值最小的元素一题 数组是最基本的数据结构,关于数组的面试题也屡见不鲜,本文罗列了一些常见的面试题,仅供参考,如果您有更好的题目或者想法,欢迎留言讨论.目前有以下1 ...

  5. frame中src怎么设置成一个变量_在 Figma 中如何选择 group(组)或 frame(画框)?...

    导读:在 Figma 中可以使用 group(组)或 frame(画框)来组织元素,但它们又有一些不同.合理地选择如何使用它们,能够帮助我们更好地设计.本文来自 Figma 官方博客,由卓浩翻译. 你 ...

  6. hashmap中的key是有序的么_深入理解HashMap遍历元素的顺序

    HashMap遍历元素的顺序. 一,HashMap元素的底层存储顺序 我们都知道HashMap是"无序"的,也就是说不能保证插入顺序.但是,HashMap其实也是有序的,一组相同的 ...

  7. 以下选项中不是python数据类型的是_以下选项中,是Python数据类型的是()。

    以下选项中,是Python数据类型的是(). 在Excel中,函数SUM(A1:A4)等价于()A:SUM(A1/A4)B:SUM(A1+A2+A3+A4)C:SUM(A1:A4)D:SUM(A1A2 ...

  8. 怎么修改数组中指定元素_求数组中第K大的元素

    问题描述 求无序数组int[] nums中第K大的元素. 例如 输入:nums[] = {9,5,8},k = 2 输出:8 输入:nums[] = {3,1,2,4,5,5,6},k = 4 输出: ...

  9. scala 获取数组中元素_从Scala中的元素列表中获取随机元素

    scala 获取数组中元素 We can access a random element from a list in Scala using the random variable. To use ...

最新文章

  1. java收费对.net_网上订货商城系统是怎么收费的?大概需要多少费用?
  2. C++ Primer 5th笔记(chap 19 特殊工具与技术)运行时类型识別RTTI
  3. IDEA是否会嫌导jar包麻烦吗???赶快来学习maven吧,冲冲冲
  4. C++异常处理机制详解
  5. bzoj 2121 DP
  6. np.c_和np.r_的用法解析
  7. java excel工程_java工程積累——向office致敬:有一種依賴叫excel
  8. 模版 ----- 一维指数型枚举 排列型枚举 组合型枚举
  9. python基础补漏-03-函数
  10. linux驱动目录在哪里,详细讲解Linux驱动程序
  11. python list的+=操作
  12. 使用hping3进行DoS攻击
  13. idea回到上一个光标位置
  14. 小朋友把游戏藏在计算机里,给两、三岁宝宝的60个超简单家庭早教游戏
  15. 自动化测试平台化[v1.0.0][自动化测试基本需求]
  16. 漫画 | 抱歉,你可能看到了假的通信史!
  17. 电气工程系毕业设计大全
  18. 助力提升研发效能的“黄金三角”
  19. Matlab绘制误差棒图----errorbar函数的使用
  20. idea中web文件没有小蓝点问题解决

热门文章

  1. 学python哪个网站好-有哪些值得推荐的Python学习网站|
  2. 获取html内标题,通过html敏捷包获取标题标签
  3. Java集合:Set集合
  4. Loadrunner的基本概念
  5. Problem D: 栈的基本运算(栈和队列)
  6. vscode gcc debug dbg gdb c cpp c++ cuckoo monitor
  7. [.Net线程处理系列]专题五:线程同步——事件构造
  8. MDK C++中对内联的极度优化
  9. scrapy抓取淘宝女郎
  10. AP 1532E register   Cisco 2504 AP注册WLC