topk

https://www.nowcoder.com/profile/601412/codeBookDetail?submissionId=8514010

1、全排序  時間複雜度O(nlogn)  *通過牛客*

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public:
    vector<int> GetLeastNumbers_Solution(vector<int> input, int k) {
        vector<int> res;
        if(input.empty()||k>input.size()) return res;
         
        sort(input.begin(),input.end());
         
        for(int i=0;i<k;i++)
            res.push_back(input[i]);
         
        return res;
         
    }
};
2、Partiton思想 時間複雜度O(n)   *通過VS2013,牛客超時,很納悶,歡迎找錯*

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
class Solution {
public:
    int Partition(vector<int>& input, int begin, int end)
    {
        int low=begin;
        int high=end;
         
        int pivot=input[low];
        while(low<high)
        {
            while(low<high&&pivot<=input[high])
                high--;
            input[low]=input[high];
            while(low<high&&pivot>=input[low])
                low++;
            input[high]=input[low];
        }
        input[low]=pivot;
        return low;
    }
     
    vector<int> GetLeastNumbers_Solution(vector<int> input, int k) {
         
        int len=input.size();
        if(len==0||k>len) return vector<int>();
        if(len==k) return input;
         
        int start=0;
        int end=len-1;
        int index=Partition(input,start,end);
        while(index!=(k-1))
        {
            if(index>k-1)
            {
                end=index-1;
                index=Partition(input,start,end);
            }
            else
            {
                start=index+1;
                index=Partition(input,start,end);
            }
        }
         
        vector<int> res(input.begin(), input.begin() + k);
         
        return res;
    }
 
};
3、最大堆 時間複雜度O(nlogk)  *通過VS2013,牛客超時,很納悶,歡迎找錯*
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution {
public:
    vector<int> GetLeastNumbers_Solution(vector<int> input, int k) {
        int len=input.size();
        if(len<=0||k>len) return vector<int>();
         
        vector<int> res(input.begin(),input.begin()+k);
        //建堆
        make_heap(res.begin(),res.end());
         
        for(int i=k;i<len;i++)
        {
            if(input[i]<res[0])
            {
                //先pop,然後在容器中刪除
                pop_heap(res.begin(),res.end());
                res.pop_back();
                //先在容器中加入,再push
                res.push_back(input[i]);
                push_heap(res.begin(),res.end());
            }
        }
        //使其從小到大輸出
        sort_heap(res.begin(),res.end());
         
        return res;
         
    }
};
4、紅黑樹:multiset集合  利用仿函數改變排序順序 時間複雜度O(nlogk)  *通過牛客*
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution {
public:
    vector<int> GetLeastNumbers_Solution(vector<int> input, int k) {
        int len=input.size();
        if(len<=0||k>len) return vector<int>();
         
        //仿函數中的greater<T>模板,從大到小排序
        multiset<int, greater<int> > leastNums;
        vector<int>::iterator vec_it=input.begin();
        for(;vec_it!=input.end();vec_it++)
        {
            //將前k個元素插入集合
            if(leastNums.size()<k)
                leastNums.insert(*vec_it);
            else
            {
                //第一個元素是最大值
                multiset<int, greater<int> >::iterator greatest_it=leastNums.begin();
                //如果後續元素<第一個元素,刪除第一個,加入當前元素
                if(*vec_it<*(leastNums.begin()))
                {
                    leastNums.erase(greatest_it);
                    leastNums.insert(*vec_it);
                }
            }
        }
         
        return vector<int>(leastNums.begin(),leastNums.end());
    }
};


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章