`

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

    // method 1: O(nlogn)
    public int findKthLargest1(int[] nums, int k) {
        Arrays.sort(nums);
        return nums[nums.length - k];
    }
    // method 2: quick select: standard recursive version
    public int findKthLargest2(int[] nums, int k) {
        quickSelect(nums, k, 0, nums.length - 1);
        return nums[nums.length - k];
    }
    private static final int CUTOFF = 10;
    public void quickSelect(int[] nums, int k, int left, int right) {
        if (left + CUTOFF <= right) {
            int pivot = median3(nums, left, right);
            int i = left, j = right - 1;
            while (true) {
                while(nums[++i] < pivot) {}
                while(nums[--j] > pivot) {}
                if (i < j) {
                    swap(nums, i, j);
                } else {
                    break;
                }
            }
            swap(nums, i, right - 1);
            if (nums.length - k < i)
                quickSelect(nums, left, i - 1, k);
            else if (nums.length - k > i)
                quickSelect(nums, i + 1, right, k);
        } else {
            insertionSort(nums, left, right);
        }
    }
    private int median3(int[] nums, int left, int right) {
        int mid = (left + right) / 2;
        if (nums[mid] < nums[left])
            swap(nums, left, mid);
        if (nums[mid] > nums[right])
            swap(nums, mid, right);
        if (nums[mid] < nums[left])
            swap(nums, left, mid);
        swap(nums, mid, right - 1);
        return nums[right - 1];
    }
    private void swap(int[] nums, int p1, int p2) {
        int tmp = nums[p1];
        nums[p1] = nums[p2];
        nums[p2] = tmp;
    }
    private void insertionSort(int[] nums, int left, int right) {
        for (int i = left + 1; i <= right; i++) {
            if (nums[i] < nums[i - 1]) {
                int tmp = nums[i];
                int j = i - 1;
                for (; j >= 0 && tmp < nums[j]; j--) {
                    nums[j + 1] = nums[j];
                }
                nums[j + 1] = tmp;
            }
        }
    }

 

分享到:
评论

相关推荐

    lrucacheleetcode-leetcode:leetcode

    lru缓存leetcode 力码 大批 152-最大乘积子阵列,169-多数元素,189-旋转阵列,198-房屋强盗 二分法 153-在旋转排序数组(II)中查找最小值,162-查找峰值元素 结构 155 分钟堆栈,173 二进制搜索树迭代器,HARD:...

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

    javalruleetcode-reverie:找工作

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

    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_数据结构

    leetcode中国Final450_数据结构 实际上,这个存储库包含成为优秀竞争程序员最重要的数据结构和算法。 他们的名字是: Questions ----------- *Reverse the array *Find the maximum and minimum element in an array...

    LeetCode最全代码

    421 | [Maximum XOR of Two Numbers in an Array](https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/) | [C++](./C++/maximum-xor-of-two-numbers-in-an-array.cpp) [Python](./Python/...

    leetcode中国-DP:DP

    leetcode中国大批 0. Count Prime 1. Reverse the array 2. Find the maximum and minimum element in an array 3. Find the "Kth" max and min element of an array 4. Given an array which consists of only 0, 1...

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

    leetcode装最大水力码 解决了leetcode问题 1 . 2 . 3 . 4 . 5 . 6 . 它有 3 个解决方案,解释了 2 个解决方案,并且可以使用虚拟变量来解释第 3 个解决方案 7 . 8 . 9 . 必须解决树上的各种问题。 10 . 11 . 12 . ...

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

    LeetCode判断字符串是否循环 LC 算法练习 LeetCode需要重点关注的题目 简单题型 中等难度题型 kth largest element in an array . use heap, use quick select. maximal square. Use dynamic programming. use just ...

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

    leetcode 分类数据结构 a brief introduction and explanation on interior architecture DataStructures 及其有趣的事实 1. TreeMap is created using RedBlack Tree 2. Autocomplete feature can be done through ...

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

Global site tag (gtag.js) - Google Analytics