1. 程式人生 > >[LeetCode 215]Kth Largest Element in an Array (分治法/快速排序)

[LeetCode 215]Kth Largest Element in an Array (分治法/快速排序)

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.

樸素的演算法是先對陣列排序,然後取第k個數,此即為第k大數,此時複雜度為O(nlogn)
但是可以想見,排序其實額外做了一些工作,所以複雜度還有優化的餘地。

先來想一想快速排序是怎麼做的。首先取一個主值pivot,然後以該值為界,將陣列分為大於pivot與小於pivot兩部分,然後分別對這兩部分遞迴的排序,以此得到一個良序的序列。反觀我們的問題,這是求解一個區域性的解,並不需要全域性良序,所以當我們將陣列分為兩部分後,雖然還不知道確切的值,但可以確定我們要求的解位於哪一個部分,然後只求解這一部分就可以了。

平均來說,每次遞迴將問題規模減半。假設第一層遞迴的複雜度為1,那麼第二層平均來說會是1/2,第三層1/4以此類推。最後求和1+1/2+1/4+…=2,所以總體複雜度為O(n)

PS:有樣例卡每次將nums[0]作為pivot的情況,所以建議另找一值作為pivot

程式碼如下:

#include <algorithm>
class Solution {
public:
    int findKthLargest(vector<int>& nums, int k) {
        int n = nums.size();
        return recFind(nums, k, 0, n);
    }
    int recFind(vector<int> &nums, int k, int lo, int
hi) { swap(nums[lo], nums[lo+(hi-lo)/2]); int pivot = nums[lo]; int i = lo, j = hi-1; while (i != j) { while (j > i && nums[j] > pivot) j--; swap(nums[i], nums[j]); while (i < j && nums[i] <= pivot) i++; swap(nums[i], nums[j]); } int right_size = hi-i; if (right_size == k) return nums[i]; if (right_size > k) return recFind(nums, k, i, hi); return recFind(nums, k-right_size, lo, i); } };

相關推薦

[LeetCode 215]Kth Largest Element in an Array 治法/快速排序

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

Leetcode 215: Kth Largest Element in an Array

arr start public code bsp private valid it is note Find the kth largest element in an unsorted array. Note that it is the kth largest ele

LN : leetcode 215 Kth Largest Element in an Array

kth .com distinct cnblogs des leetcode 轉化 solution n-k lc 215 Kth Largest Element in an Array 215 Kth Largest Element in an Array Find t

leetcode 215 Kth Largest Element in an Array : 快排

在無序的陣列中找到第k大的元素,也就是若長度為n的陣列從小到大排列時,下標為n-k的元素。 注意Example2:第4大的元素是4,也就是陣列中出現的兩個5分別是第2大和第3大的數字。                 &nb

LeetCode215. Kth Largest Element in an Array輸出第k大的數

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

各個排序演算法應用:求取陣列中第K大的數( 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.

C++ sort + vector 應用:求取陣列中第K大的數( 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.

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 the sorted order, not the kth dis

leetcode: 215. Kth Largest Element in an Array

Difficulty Medium. Problem Find the kth largest element in an unsorted array. Note that it is the kt

#Leetcode# 215. Kth Largest Element in an Array

turn san input not ref cto put ont size https://leetcode.com/problems/kth-largest-element-in-an-array/ Find the kth largest element in

Leetcode-215. Kth Largest Element in an Array

etc emp urn private art ems end index 地址 地址:https://leetcode.com/problems/kth-largest-element-in-an-array/ 描述: Find the kth largest ele

215. Kth Largest Element in an Arraypython+cpp

題目: 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 e

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

[Leetcode] Heap, Divide and conquer--215. Kth Largest Element in an Array

bsp sting arc push lec archive left divide discuss Find the kth largest element in an unsorted array. Note that it is the kth largest ele

LeetCode 題解之 215. Kth Largest Element in an Array

215. Kth Largest Element in an Array 題目描述和難度 題目描述: 在未排序的陣列中找到第 k 個最大的元素。請注意,你需要找的是陣列排序後的第 k 個最大的元素,而不是第 k 個不同的元素。 示例 1: 輸入

215. Kth Largest Element in an Array

ret span self div arr [] 如果 說明 ace 用heap解, 方法1. 維護一個 size = k 的最小堆。當前元如果大於堆頂的元素,那麽說明堆頂的元素肯定小於kth largest element。所以replace他。 1 class

#215. Kth Largest Element in an Array

-1 amp right logs highlight brush pre vector -- class Solution { public: int findKthLargest(vector<int>& nums, int k) {

215. Kth Largest Element in an Array 第K大的數

start ++ uic find sta 第k大 ret color post class Solution { public: int quicksort(vector<int>& nums, int start, int end, int

215 Kth Largest Element in an Array

---恢復內容開始--- 題目:在一個整數序列中尋找第k大的元素。 注意幾個問題: 1)k的取值:是不變的啊! 不會因為遞迴時在privot的左邊或者右邊尋找而改變,因為nums的長度n是不變的,元素的下標也是不變的; 2)partition函式遞迴呼叫自己時是要加return的!不然會報錯。

[leetcode]215. Kth Largest Element in an [email 

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