[email protected] [315/215] Count of Smaller Numbers After Self / Kth Largest Element in an Array (BST)

https://leetcode.com/problems/count-of-smaller-numbers-after-self/

You are given an integer array nums and you have to return a new counts array. The counts array has the property where counts[i] is the number of smaller elements to the right of nums[i].

Example:

Given nums = [5, 2, 6, 1]

To the right of 5 there are 2 smaller elements (2 and 1).
To the right of 2 there is only 1 smaller element (1).
To the right of 6 there is 1 smaller element (1).
To the right of 1 there is 0 smaller element.

Return the array [2, 1, 1, 0].

class Solution {
    class TreeNode{
        public: int val, rank;
        TreeNode *l, *r;
        TreeNode(int v): val(v), rank(1), l(NULL), r(NULL) {}
    };
public:
    int getRank(TreeNode* root, int v) {
        int rank = 0;
        while(true) {
            if(v <= root->val) {
                ++root->rank;
                if(root->l == NULL) {
                    root->l = new TreeNode(v);
                    break;
                }
                else root = root->l;
            }
            else{
                rank += root->rank;
                if(root->r == NULL) {
                    root->r = new TreeNode(v);
                    break;
                }
                else root = root->r;
            }
        }
        return rank;
    }

    vector<int> countSmaller(vector<int>& nums) {
        vector<int> res;

        if(nums.size() == 0)  return res;
        TreeNode* root = new TreeNode(nums[nums.size()-1]);

        res.push_back(0);
        for(int i=nums.size()-2; i>=0; --i) {
            int rank = getRank(root, nums[i]);
            res.push_back(rank);
        }

        vector<int> rev_res;
        for(vector<int>::reverse_iterator p = res.rbegin(); p!=res.rend(); ++p)  rev_res.push_back(*p);
        return rev_res;
    }
};

https://leetcode.com/problems/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.

class Solution {
    class TreeNode{
        public: int val, rank;
        TreeNode *l, *r;
        TreeNode(int v): val(v), rank(1), l(NULL), r(NULL) {}
    };

public:
    void addNode(TreeNode* root, int v) {
        while(true) {
            if(v <= root->val) {
                ++root->rank;
                if(root->l == NULL) {
                    root->l = new TreeNode(v);
                    break;
                }
                else root = root->l;
            }
            else{
                if(root->r == NULL) {
                    root->r = new TreeNode(v);
                    break;
                }
                else root = root->r;
            }
        }
    }

    void dfs(TreeNode* root, int k, int& res) {
        if(root->rank == k) {
            res = root->val;
            return;
        }

        if(root->l)  dfs(root->l, k, res);
        if(root->r)  dfs(root->r, k - root->rank, res);
    }

    int findKthLargest(vector<int>& nums, int k) {
        if(nums.size() == 1)  return nums[0];

        TreeNode *root = new TreeNode(nums[0]);
        for(int i=1; i<nums.size(); ++i) {
            addNode(root, nums[i]);
        }

        int res = -1;
        dfs(root, nums.size() - k + 1, res);
        return res;
    }
};
时间: 2024-11-03 16:10:03

[email protected] [315/215] Count of Smaller Numbers After Self / Kth Largest Element in an Array (BST)的相关文章

LeetCode OJ 215. Kth Largest Element in an Array 堆排序求解

题目链接:https://leetcode.com/problems/kth-largest-element-in-an-array/ 215. Kth Largest Element in an Array My Submissions Question Total Accepted: 43442 Total Submissions: 136063 Difficulty: Medium Find the kth largest element in an unsorted array. Not

网易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

刷题215. Kth Largest Element in an Array

一.题目说明 题目215. Kth Largest Element in an Array,在一个无序数组中找第k大的元素.难度是Medium! 二.我的解答 这个题目最直观的解答是,先对数组排序,然后直接返回: class Solution{ public: int findKthLargest(vector<int>& nums,int k){ sort(nums.begin(),nums.end()); return nums[nums.size()-k]; } }; 性能如下:

703. Kth Largest Element in a Stream/215. Kth Largest Element in an Array/

703 非常经典的一个题,假设有一个不断增加的序列,要求输出第K 大的数 int k = 3; int[] arr = [4,5,8,2]; KthLargest kthLargest = new KthLargest(3, arr); kthLargest.add(3);   // returns 4 kthLargest.add(5);   // returns 5 kthLargest.add(10);  // returns 5 kthLargest.add(9);   // retur

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 le

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

Java for LeetCode 215 Kth Largest Element in an Array【Coming Soon】

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. 解题思路: 本题是<算法导论>原题,

【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 ≤ arr

[leedcode 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