#5 Kth Largest Element

原创 2016年08月30日 13:20:17

题目描述:

Find K-th largest element in an array.

 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 5, 2nd largest element is 4, 3rd largest element is 3 and etc.

Challenge 

O(n) time, O(1) extra memory.

题目思路:

这题要达到题目要求,只能用quick sort的思想:不断找pivot,如果它在第k个位置,就返回nums[k - 1];不然就根据pivot的位置进行recursive查找。

Mycode(AC = 82ms):

class Solution {
public:
    /*
     * param k : description of k
     * param nums : description of array and index 0 ~ n-1
     * return: description of return
     */
    int kthLargestElement(int k, vector<int> nums) {
        // write your code here
        if (nums.size() == 0) return 0;
        
        kthLargestElement(k, nums, 0, nums.size() - 1);
        return nums[k - 1];
    }
    
    void kthLargestElement(int& k,
                           vector<int>& nums,
                           int start,
                           int end)
    {
        if (start >= end) return;
        
        int pivot = findPivot(nums, start, end);
        if (pivot > k) {
            kthLargestElement(k, nums, start, pivot - 1);
        }
        else if (pivot < k) {
            kthLargestElement(k, nums, pivot + 1, end);
        }
        else {
            return;
        }
    }
    
    int findPivot(vector<int>& nums, int start, int end) {
        if (start == end) return start;
        
        int pivot = nums[end];
        int l = start, r = end;
        while (l < r) {
            while (l < r && nums[l] >= pivot) {
                l++;
            } 
            nums[r] = nums[l];
            
            while (l < r && nums[r] <= pivot) {
                r--;
            }
            nums[l] = nums[r];
        }
        
        nums[l] = pivot;
        return l;
    }
};


版权声明:三千微尘里,吾宁爱与憎

相关文章推荐

5.Kth Largest Element-第k大元素(中等题)

第k大元素 题目 在数组中找到第k大的元素 注意事项 你可以交换数组中的元素的位置 样例 给出数组 [9,3,2,4,8],第三大的元素是 4 给出数组 [1,2,3,4,5],第一大...

《leetCode》: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 ...

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 ...

215. Kth Largest Element in an Array 暴力-堆排序-快速排序

1、来源:点击打开链接 2、题目: Find the kth largest element in an unsorted array. Note that it is the kth large...

LeetCode-215.Kth Largest Element in an Array

https://leetcode.com/problems/kth-largest-element-in-an-array/ Find the kth largest element in an ...

【leetcode】215. Kth Largest Element in an Array

【leetcode】215. Kth Largest Element in an ArrayFind the kth largest element in an unsorted array. Not...

【LeetCode-面试算法经典-Java实现】【215-Kth Largest Element in an Array(数组中第K大的数)】

【215-Kth Largest Element in an Array(数组中第K大的数)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】代码下载【https://github...

Leetcode 215. Kth Largest Element in an Array[medium]

题目: Find the kth largest element in an unsorted array. Note that it is the kth largest element in t...

Leetcode: Kth Largest Element in an Array

 Find the kth largest element in an unsorted array. Note that it is the kth largest element in th...

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 so...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)