zoukankan      html  css  js  c++  java
  • 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;
        }
    };
  • 相关阅读:
    [USACO17JAN]Subsequence Reversal序列反转
    P1330 封锁阳光大学
    P1403 [AHOI2005]约数研究
    poj1456——Supermarket
    P1807 最长路_NOI导刊2010提高(07)
    P1137 旅行计划
    P1162 填涂颜色
    P1040 加分二叉树
    P1135 奇怪的电梯
    P1086 花生采摘
  • 原文地址:https://www.cnblogs.com/fu11211129/p/5140975.html
Copyright © 2011-2022 走看看