如何在数组中查找第k个最大/最小元素

If you are actively interviewing with companies, this is probably one of the most common problems out there. You can probably find this problem on leetcode, hackerrank and other websites with interview preparation.

如果您正在积极采访公司,这可能是最常见的问题之一。 通过面试准备,您可能会在leetcode,hackerrank和其他网站上发现此问题。

To understand how to solve this problem, reading my article about quicksort would be helpful:

要了解如何解决此问题,请阅读我有关quicksort的文章会有所帮助:

Alright, I know you probably don’t even understand why quick sort plays into this picture but I will explain in a minute.

好吧,我知道您可能甚至不理解为什么快速排序会出现在这张图片中,但我会在稍后解释。

So what do we mean by finding the kth largest/smallest element in the array?

那么,找到数组中第k个最大/最小元素意味着什么?

It means that with a given array(sorted or unsorted) I want to find 1st or 2nd largest/smallest element in the array, if k is 1 or 2.

这意味着对于给定的数组(排序或未排序),如果k为1或2,我想在数组中找到第1或第2个最大/最小元素。

For example, let’s say there is an array with the following values: [5,4,7,1,2,3,6]. If k is 2 then I will get the value of 2 because it is the 2nd smallest element. It would have been 6 if I was getting the 2nd largest element.

例如,假设有一个具有以下值的数组:[5,4,7,1,2,3,6]。 如果k为2,那么我将得到2的值,因为它是第二小的元素。 如果我得到第二大元素,它应该是6。

Generally, there are a few ways to solve this:

通常,有几种方法可以解决此问题:

  • Sort the array and return the element by indicing the array via k(arr[k])

    对数组进行排序,并通过k(arr [k])指示数组,返回元素
  • using min/maximum heap to find the kth element

    使用最小/最大堆来查找第k个元素
  • using quick select/partial quick sort to solve the problem.

    使用快速选择/部分快速排序来解决问题。

Let’s start off by using a default array to solve the problem:

让我们开始使用默认数组解决问题:

At first glance, sorting the function and then indicing the answer seems simple. However, this won’t be such a common interview question if that’s all it takes to solve.

乍一看,对函数进行排序,然后指出答案似乎很简单。 但是,如果要解决所有问题,这将不是一个常见的面试问题。

Performance complexity wise this problem will be o(n log n) because Arrays.sort is essentially o(n log n). The returned value is o(1).

在性能复杂度方面,此问题将是o(n log n),因为Arrays.sort本质上是o(n log n)。 返回值为o(1)。

Space complexity can really depend on its implementation. If it is just swapping all of its elements toward a sorted array, it is o(1). If it is returning a new sorted array via creating a new array then it will be o(n).

空间复杂性实际上可以取决于其实现。 如果它只是将其所有元素交换到已排序的数组,则为o(1)。 如果它通过创建新数组返回一个新的排序数组,则它将为o(n)。

You can also use a heap to solve this problem. A copy of the solution is posted here:https://www.geeksforgeeks.org/k-largestor-smallest-elements-in-an-array/.

您也可以使用堆来解决此问题。 解决方案的副本发布在此处: https : //www.geeksforgeeks.org/k-largestor-smallest-elements-in-an-array/

The third solution, quick select, is also a good solution to solve this problem.

第三个解决方案,即快速选择,也是解决此问题的好方法。

To understand quick select, we need to understand how quicksort works.

要了解快速选择,我们需要了解快速排序的工作原理。

Quicksort is done via partition, which at each partition splits an array into two halves: one half with values less than the pivot, another half with values greater than the pivot. The pivot will be placed at the right index, with the index indicating how many more elements it is greater too.

快速排序是通过分区完成的,分区将每个分区分为两半:一半的值小于枢轴,另一半的值大于枢轴。 枢轴将放置在正确的索引处,索引指示该元素也要多多少个元素。

Here is the implementation of quicksort:

这是quicksort的实现:

Okay…how does that help us solve the kth smallest or largest problem? Let’s say we want the 2nd smallest element in the array. Since pivot tells us how many more elements are greater/smaller than the pivot itself it also tells us where kth element lies.

好的,这如何帮助我们解决第k个最小或最大的问题? 假设我们想要数组中第二小的元素。 由于枢轴告诉我们比枢轴本身多多少少的元素,因此它也告诉我们第k个元素位于何处。

In another word, if the pivot(its an index, just for a reminder) equals to k, then arr[k] will be the kth smallest/largest element from arr. Otherwise, if the pivot is less than k is then we need to partition into the second halves of the array as the kth element lies in the second part of the subarray.

换句话说,如果数据透视表(其索引只是为了提醒)等于k,则arr [k]将是arr中第k个最小/最大的元素。 否则,如果枢轴小于k,则我们需要划分为数组的后半部分,因为第k个元素位于子数组的第二部分中。

On the other hand, if the pivot is greater than k then we need to find the kth element on the left side of the array for our answer.

另一方面,如果枢轴大于k,那么我们需要在数组的左侧找到第k个元素作为答案。

Here is an implementation of quick select:

这是快速选择的实现:

Again, please try to implement this without my guidance here on your own. That will help understand and how to solve the problem even better.

同样,请尝试在没有我的指导的情况下自行实施此操作。 这将有助于理解以及如何更好地解决问题。

Good luck.

祝好运。

翻译自: https://medium.com/weekly-webtips/how-to-find-kth-largest-smallest-element-in-an-array-6d91286af3c7

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值