Kth Largest Element II Lintcode

Find K-th largest element in an array. and N is much larger than k.

Notice

You can swap elements in the array

Example

In array [9,3,2,4,8], the 3rd largest element is 4.

In array [1,2,3,4,5], the 1st largest element is 52ndlargest element is 43rd largest element is 3 and etc.

这道题可以用快速排序/快速选择的思想来做。

快排也写了不止一次了,但还是不够熟练。。。希望经过这次练习可以熟练一些。

需要注意的是注释的地方是不需要右边向左移动的,因为需要右边的指针停在第一个比pivot大的数的位置上,这样才可以调换。

class Solution {
    /**
     * @param nums an integer unsorted array
     * @param k an integer from 1 to n
     * @return the kth largest element
     */
    public int kthLargestElement2(int[] nums, int k) {
        if (nums == null || nums.length == 0) {
            return -1;
        }
        if (nums.length == 1 && k == 1) {
            return nums[0];
        } else if (nums.length == 1) {
            return -1;
        }
        int result = helper(nums, 0, nums.length - 1, nums.length - 1, k);
        return result;
    }
    private int helper(int[] nums, int left, int right, int pivot, int k) {
        int l = left, r = right;
        while (l < r) {
            while (l < r && nums[l] > nums[pivot]) {
                l++;
            }
            while (l < r && nums[r] <= nums[pivot]) {
                r--;
            }
            swap(nums, l, r);
            l++;
            // r--;
        }
        swap(nums, r, pivot);
        if (r == k - 1) {
            return nums[r];
        } else if (r < k - 1) {
            return helper(nums, r + 1, right, pivot, k);
        }
        return helper(nums, left, r - 1, r - 1, k);
    }
    private void swap(int[] nums, int a, int b) {
        int tmp = nums[a];
        nums[a] = nums[b];
        nums[b] = tmp;
    }
}

还可以用heap来做,可能相对慢一点?因为要全部排序?但这个思路要反应一下。。。到时候回顾回顾?

class Solution {
    /**
     * @param nums an integer unsorted array
     * @param k an integer from 1 to n
     * @return the kth largest element
     */
    public int kthLargestElement2(int[] nums, int k) {
        PriorityQueue<Integer> pq = new PriorityQueue<>(k);
        for (int i = 0; i < nums.length; i++) {
            pq.offer(nums[i]);
            if (pq.size() > k) {
                pq.poll();
            }
        }
        return pq.peek();
    }
}
时间: 2024-12-14 18:29:07

Kth Largest Element II Lintcode的相关文章

Lintcode: Kth Largest Element 解题报告

Kth Largest Element Find K-th largest element in an array. Note You can swap elements in the array Example In array [9,3,2,4,8], the 3th largest element is 4 Challenge O(n) time, O(1) space 原题链接: http://www.lintcode.com/en/problem/kth-largest-element

Lintcode5 Kth Largest Element solution 题解

[题目描述] Find K-th largest element in an array. Notice:You can swap elements in the array 在数组中找到第k大的元素 注意:你可以交换数组中的元素的位置 [题目链接] http://www.lintcode.com/en/problem/kth-largest-element/ [题目解析] sort的方法:一开始看到这道题肯定觉得很简单,只要sort一下,然后return特定index的value就可以了,但是

215. Kth Largest Element in an Array

Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.For example,Given [3,2,1,5,6,4] and k = 2, return 5.Note:You may assume k is always valid, 1 ≤ k ≤ array's le

leetcode Kth Largest Element in an Array

题目连接 https://leetcode.com/problems/kth-largest-element-in-an-array/ Kth Largest Element in an Array Description Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct eleme

LeetCode 215 : Kth Largest Element in an Array

Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. For example,Given [3,2,1,5,6,4] and k = 2, return 5. Note: You may assume k is always valid, 1 ≤ k ≤ array's

Kth Largest Element in an Array

Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. For example, Given [3,2,1,5,6,4] and k = 2, return 5. Note: You may assume k is always valid, 1 ≤ k ≤ array'

LeetCode215:Kth Largest Element in an Array

Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. For example, Given [3,2,1,5,6,4] and k = 2, return 5. Note: You may assume k is always valid, 1 ≤ k ≤ array'

Java for LeetCode 215 Kth Largest Element in an Array【Coming Soon】

Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. For example, Given [3,2,1,5,6,4] and k = 2, return 5. 解题思路: 本题是<算法导论>原题,

【leetcode】Kth Largest Element in an Array (middle)☆

Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. For example,Given [3,2,1,5,6,4] and k = 2, return 5. 思路: 堆.讲解:二叉堆 class Solution { public: //新插入i结点 其父节点为(i