5. Kth Largest Element

本文介绍了一种使用快速排序的思想来查找数组中第K大的元素的方法。主要思想是通过分区操作,避免了全排序的时间成本。关键在于递归地进行分区,直到找到目标元素的位置。此外,还详细解释了代码实现,包括主函数、辅助函数和分区函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

5. Kth Largest Element

Tag:

Quick Sort, Sort

Description:

Find K-th largest element in an array.

Main Idea:

It’s not recommended to sort the array, because it’s time-costing.

Partition problem. In this problem, we only need to find the kth large number, partition would be our tool. If this problem wants to find the first k large numbers, then we need to use priority queue structure.

At the first, write the partition template. Then, compare the k with the position of the partition. Keep partition until it’s get the target result.

Code explain:
  1. In this problem, though there are three functions: main(), helper, and partition, the structure is simple.
  2. Partition function is template, which is tagged at the end.
  3. The main() function is to check is corner case and call the helper function.
  4. The helper function is the key point. Beside check the pointers(l ,r), IF the position is at the k place, return the result; ELSE IF position is larger than k, partition the former part; ELSE partition the latter part.

Tips/Notes:

  1. Notice in helper function’s input, k is not the index, while is position is.
  2. Don’t mess up the partition part after comparison. If position+1 > k, partition [l, position-1]; position+1 < k, partition [position + 1, r].

Time/Space Cost:

Time Cost:   O ( n ) \ O(n)  O(n)

Code:

class Solution {
public:
    /**
     * @param n: An integer
     * @param nums: An array
     * @return: the Kth largest element
     */
    int kthLargestElement(int n, vector<int> &nums) {
        // write your code here
        int len = nums.size();
        if(n < 0 || n > len || len == 0){
            return 0;
        }
        
        return helper(len - n + 1, nums, 0, len-1);
        
    }
    
    int helper(int k, vector<int> &nums, int l, int r){
        if(l == r)
            return nums[l];
            
        int position = partition(nums, l, r);
        // k is the kth large nums, i is the index.
        if(position + 1 == k){
            return nums[position];
        }
        else if(position + 1 < k){
            return helper(k, nums, position + 1, r);
        }
        else {
            return helper(k, nums, l, position - 1);
        }
    }
    
    int partition(vector<int> &nums, int l, int r){
        int left = l, right = r;
        int pivot = nums[left];
        
        while(left < right){
            while(left < right && nums[right] >= pivot){
                right--;
            }
            nums[left] = nums[right];
            
            while(left < right && nums[left] <= pivot){
                left++;
            }
            nums[right] = nums[left];
            
        }
        
        nums[left] = pivot;
        return left;
    }
};

Partition Template:

class Solution {
public:
    int partition(vector<int> &nums, int l, int r){
        int left = l, right = r;
        int pivot = nums[left];
        
        while(left < right){
            while(left < right && nums[right] >= pivot){
                right--;
            }
            nums[left] = nums[right];
            
            while(left < right && nums[left] <= pivot){
                left++;
            }
            nums[right] = nums[left];
        }
        nums[left] = pivot;
        return left;
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值