zoukankan      html  css  js  c++  java
  • 【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 length.

    题解:

      分治怎么做?

    Solution 1 ()

    class Solution {
    public:
        int findKthLargest(vector<int>& nums, int k) {
            int n = nums.size();
            sort(nums.begin(), nums.end());
            return nums[n-k];
        }
    };

      from here

    Solution 2 ()

    class Solution {
    public:
        int partition(vector<int>& nums, int left, int right) {
            int pivot = nums[left];
            int l = left + 1, r = right;
            while (l <= r) {
                if (nums[l] < pivot && nums[r] > pivot){
                    swap(nums[l++], nums[r--]);
                }
                if (nums[l] >= pivot) l++;
                if (nums[r] <= pivot) r--;
            }
            swap(nums[left], nums[r]);
            return r;
        }
        int findKthLargest(vector<int>& nums, int k) {
            int left = 0, right = nums.size() - 1;
            while (true) {
                int pos = partition(nums, left, right);
                if (pos == k - 1){ 
                    return nums[pos];
                }
                if (pos > k - 1) {
                    right = pos - 1;
                }else{
                    left = pos + 1;  
                }  
            }
        }
    };

    附:partition

    class Solution {
    public:
        int partition(vector<int>& nums, int left, int right) {
            int pivot = nums[left];
            int l = left, r = right;
            while(l < r) {
             //因为pivot放在最前面,所以r先走,若是将pivot放在最后,则l先走。不同写法需要考虑复杂的边界条件
                while(l<r && nums[r] <= pivot) r--;
                while(l<r && nums[l] >= pivot) l++;
                if(l<r) swap(nums[l], nums[r]);
            }
            swap(nums[r], nums[left]);
            return r;
        }
        int findKthLargest(vector<int>& nums, int k) {
            int left = 0, right = nums.size() - 1;
            while (true) {
                int pos = partition(nums, left, right);
                if(pos == k-1) return nums[pos];
                if(pos > k-1) right = pos - 1;
                else left = pos + 1; 
            }
        }
    };

    Solution 4 ()

    class Solution {
    public:
        int findKthLargest(vector<int>& nums, int k) {
            multiset<int> mset;
            int n = nums.size();
            for (int i = 0; i < n; i++) { 
                mset.insert(nums[i]);
                if (mset.size() > k)
                    mset.erase(mset.begin());
            }
            return *mset.begin();
        }
    };

    Solution 5 ()

    class Solution {
    public:
        int findKthLargest(vector<int>& nums, int k) {
            priority_queue<int> pq(nums.begin(), nums.end());
            for (int i = 0; i < k - 1; i++)
                pq.pop(); 
            return pq.top();
        }
    }; 

    Solution 6 ()

    class Solution {
    public:   
        inline int left(int idx) {
            return (idx << 1) + 1;
        }
        inline int right(int idx) {
            return (idx << 1) + 2;
        }
        void max_heapify(vector<int>& nums, int idx) {
            int largest = idx;
            int l = left(idx), r = right(idx);
            if (l < heap_size && nums[l] > nums[largest]) largest = l;
            if (r < heap_size && nums[r] > nums[largest]) largest = r;
            if (largest != idx) {
                swap(nums[idx], nums[largest]);
                max_heapify(nums, largest);
            }
        }
        void build_max_heap(vector<int>& nums) {
            heap_size = nums.size();
            for (int i = (heap_size >> 1) - 1; i >= 0; i--)
                max_heapify(nums, i);
        }
        int findKthLargest(vector<int>& nums, int k) {
            build_max_heap(nums);
            for (int i = 0; i < k; i++) {
                swap(nums[0], nums[heap_size - 1]);
                heap_size--;
                max_heapify(nums, 0);
            }
            return nums[heap_size];
        }
    private:
        int heap_size;
    }
  • 相关阅读:
    java冒泡算法
    java时间操作
    Java重写构造方法
    正则Sub用法
    Python正则反向引用
    Django发送邮件
    Django导出excel
    Nginx编译安装
    年薪20万Python工程师进阶(7):Python资源大全,让你相见恨晚的Python库
    Go语言学习笔记
  • 原文地址:https://www.cnblogs.com/Atanisi/p/6804206.html
Copyright © 2011-2022 走看看