1. 程式人生 > >【LeetCode】215. Kth Largest Element in an Array

【LeetCode】215. Kth Largest Element in an Array

distinct class ted ++ bsp order algo max git

題目:

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;
}

【LeetCode】215. Kth Largest Element in an Array