`

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 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) space

 

public int kthLargestElement(int k, ArrayList<Integer> numbers) {
    int N = numbers.size();
    Integer[] A = new Integer[N];
    numbers.toArray(A);
    k = N - k; // 0 based kth smallest index
    return kthSmallest(A, 0, N-1, k);
}

public int kthSmallest(Integer[] A, int lo, int hi, int k) {
    if(k<lo || k>hi) return -1;
    int p = partition(A, lo, hi);
    if(p == k) return A[p];
    if(p < k) {
        return kthSmallest(A, p+1, hi, k);
    } else {
        return kthSmallest(A, lo, p-1, k);
    }
}

private int partition(Integer[] A, int lo, int hi) {
    int pivot = A[hi];
    int p = lo;
    for(int i=lo; i<hi; i++) {
        if(A[i] < pivot) {
            swap(A, i, p++);
        }
    }
    swap(A, p, hi);
    return p;
}

private void swap(Integer[] A, int i, int j) {
    Integer tmp = A[i];
    A[i] = A[j];
    A[j] = tmp;
}

 

References:

http://www.algorithmsandme.com/2013/08/find-kth-smallest-element-application.html

http://www.geeksforgeeks.org/kth-smallestlargest-element-unsorted-array/

  • 大小: 17.7 KB
分享到:
评论

相关推荐

    703.Kth Largest Element in a Stream数据流中的第K大元素【LeetCode单题讲解系列】

    703.Kth_Largest_Element_in_a_Stream数据流中的第K大元素【LeetCode单题讲解系列】

    Coding Interview In Java

    8 Kth Largest Element in an Array 35 9 Wildcard Matching 37 10 Regular Expression Matching in Java 39 11 Merge Intervals 43 12 Insert Interval 45 13 Two Sum 47 14 Two Sum II Input array is sorted 49 ...

    算法上机!!

    Give an O(lg m + lgn) time algorithm for computing the kth largest element in the union of the two lists. (For simplicity, you can assume that the elements of the two lists are distinct). Practice 2 ...

    cpp-算法精粹

    Kth Largest Element in an Array 桶排序 First Missing Positive 计数排序 H-Index 基数排序 Maximum Gap 其他 Largest Number 小结 查找 Search for a Range Search Insert Position Search in Rotated Sorted ...

    leetcode装最多水-Leetcode:解决了leetcode问题

    largest element from an unsorted array in linear time. (1) If the number of elements in an array is large .Divide the array into arrays each with 5 elements. n/5 arrays. (2) Compute Median of these 5 ...

    lrucacheleetcode-LeetCodeSheet:记录自己Leetcode之旅

    Element Leetcode 4. Median of Two Sorted Arrays 注意:后两题是与快速排序非常相似的快速选择(Quick Select)算法,面试中很常考 链表类(Linked List): 基础知识:链表如何实现,如何遍历链表。链表可以保证...

    javalruleetcode-reverie:找工作

    Largest Element in an Array(方法1:堆 方法二:快速排序(推荐)) (面试题40:最小的k个数) LeetCode 347 Top K Frequent Elements(堆排序、桶排序) LintCode 532 Reverse Pairs(归并排序的应用)(面试题...

    LeetCode判断字符串是否循环-lc:液晶显示器

    kth largest element in an array . use heap, use quick select. maximal square. Use dynamic programming. use just O(n) space. The extra space equals matrix col length. majority element ii . 使用 "摩尔...

    lrucacheleetcode-leetcode:leetcode

    lru缓存leetcode 力码 大批 152-最大乘积子阵列,169-多数元素,189-...215-Kth Largest element in an Array(Heap Sort), HARD: 218-The Skyline Problem(Mergesort) ) 动态规划 HARD: 174-Dungeon Game, HARD: 188

    LeetCode最全代码

    27 | [Remove Element](https://leetcode.com/problems/remove-element/) | [C++](./C++/remove-element.cpp) [Python](./Python/remove-element.py) | _O(n)_ | _O(1)_ | Easy || 31 | [Next Permutation]...

    lrucacheleetcode-Exercise_LeetCode:练习_LeetCode

    lru cache leetcode LeetCode Solution 双指针 Two Pointers 同向双指针 快慢双指针 Find Kth largest/smallest element Binary Search 二分法 Other

    leetcode分类-DataStructures:室内建筑的简单介绍和解释

    largest element in array 4. Improvements in Java 8 - For HashMap if hashcode collision count is more than 8 in a row , then its internal structure is changed from linked list to tree 渐近分析 big- \...

    leetcode中国-DP:DP

    "Kth" max and min element of an array 4. Given an array which consists of only 0, 1 and 2. Sort the array without using any sorting algo 5. Move all the negative elements to one side of the array 6. ...

    leetcode中国-effective-learning:有效学习

    https://leetcode-cn.com/problems/kth-largest-element-in-an-array/ Effective Learning 快速学习法:一年搞定MIT计算机课程 斯考特的FAQ页面 计划 表头 1 2 3 4 运动 健身 刷脂 篮球 羽毛球 游泳、跑步 拳击 学习...

    leetcode中国-Final450_Data-Structures:Final450_数据结构

    "Kth" max and min element of an array *Given an array which consists of only 0, 1 and 2. Sort the array with *Move all the negative elements to one side of the array *Find the Union and Intersection ...

Global site tag (gtag.js) - Google Analytics