【leetcode】Kth Largest Element in an Array (middle)☆

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.

思路:

堆。讲解:二叉堆

class Solution {
public:
    //新插入i结点 其父节点为(i - 1) / 2
    void MinHeapFixup(int a[], int i)
    {
        int j = (i - 1) / 2; //父节点
        int temp = a[i];
        while(j >= 0 && i != 0)
        {
            if(a[j] <= temp) break;
            a[i] = a[j];
            i = j;
            j = (i - 1) / 2;
        }
        a[i] = temp;
    }

    //在最小堆中插入新数据nNum
    void MinHeapAddNumber(int a[], int n, int nNum)
    {
        a[n] = nNum;
        MinHeapFixup(a, n);
    }

    //堆删除后的调整  从i节点开始调整,n为节点总数 从0开始计算 i节点的子节点为 2*i+1, 2*i+2
    void MinHeapFixdown(int a[], int i, int n)
    {
        int j = 2 * i + 1;
        int temp = a[i];
        while(j < n)
        {
            if(j + 1 < n && a[j + 1] < a[j]) //在左右孩子中找最小的
                j++;
            if(a[j] >= temp)
                break;
            a[i] = a[j];  //把较小的子结点往上移动,替换它的父结点
            i = j;
            j = 2 * i  + 1;
        }
        a[i] = temp;
    }

    //在最小堆中删除数
    void MinHeapDeleteNumber(int a[], int n)
    {
        swap(a[0], a[n - 1]);
        MinHeapFixdown(a, 0, n - 1);
    }

    int findKthLargest(vector<int>& nums, int k) {
        int * a = new int[k]; //大小为k的最小堆
        for(int i = 0; i < nums.size(); ++i)
        {
            if(i < k)
            {
                MinHeapAddNumber(a, i, nums[i]); //插入数据
            }
            else if(nums[i] > a[0]) //比已有的k个最大的数字大
            {
                MinHeapDeleteNumber(a, k);
                MinHeapAddNumber(a, k - 1, nums[i]);
            }
        }
        return a[0];
    }
};

用STL的堆:

 int findKthLargest(vector<int>& nums, int k) {
    priority_queue<int> p;
    const int s(nums.size());

    for (int i = 0; i < s; ++i) p.push(nums[i]);
    while (--k) p.pop();

    return p.top();
}

堆的相关讲解:

http://www.cnblogs.com/flyoung2008/articles/2136485.html

时间: 2024-10-31 23:06:53

【leetcode】Kth Largest Element in an Array (middle)☆的相关文章

【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

leetcode_215题——Kth Largest Element in an Array(堆排序)

Kth Largest Element in an Array Total Accepted: 13165 Total Submissions: 48240My Submissions Question Solution 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 elemen

【树】Kth Smallest Element in a BST(递归)

题目: Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note: You may assume k is always valid, 1 ≤ k ≤ BST's total elements. 思路: 1.计算左子树元素个数leftSize. 2. leftSize+1 = K,则根节点即为第K个元素 leftSize >=k, 则第K个元素在左子树

LeetCode Kth Largest Element in an Array (快速排序)

题意: 在一个无序的数组中第k大的数是多少? 思路: 按照快排的思路,如果每次分成两段后,设为L和R.如果R>=k ,则答案在右边集合,否则在左边集合. 这里用了3位取中法.注意快排别给写死循环了. 1 class Solution { 2 public: 3 int findKthLargest(vector<int>& nums, int k) { 4 if(k>nums.size()) return 0; 5 return DFS(nums,0,nums.size()

【leetcode】Remove Duplicates from Sorted List II (middle)

Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example,Given 1->2->3->3->4->4->5, return 1->2->5.Given 1->1->1->2->3, return 2->3.

【leetcode】Binary Tree Zigzag Level Order Traversal (middle)

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its zigz

【leetcode】Flatten Binary Tree to Linked List (middle)

Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6 思路: 用先序遍历,得到的是从小到大的顺序.把先序遍历变形一下: void flatten(TreeNode* root) { if(NULL == root) return; vector<TreeNode *

LeetCode | 0215. Kth Largest Element in an Array数组中的第K个最大元素【Python】

LeetCode 0215. Kth Largest Element in an Array数组中的第K个最大元素[Medium][Python][快排][堆] Problem LeetCode 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. Example 1:

网易2016 实习研发工程师 [编程题]寻找第K大 and leetcode 215. Kth Largest Element in an Array

传送门 有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数. 给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在. 测试样例: [1,3,5,2,2],5,3 返回:2 note: 注意手写快排的时候: while(i < j) { while(j > i && a[j] > a[left]) j--; while(i < j && a[i] <= a[left]) i++; if(i