leetcode@ [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;
    }
};

 

你可能感兴趣的:(leetcode@ [315/215] Count of Smaller Numbers After Self / Kth Largest Element in an Array (BST))