[Leetcode]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 ofnums[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 {
public:
    /*algorithm: hash
        time O(n*n) space O(n)
    */
    vector<int> countSmaller(vector<int>& nums) {
            vector<int>count(nums.size(),0);
            for(int i = 0;i < nums.size();i++){
                for(int j = 0;j < i;j++){
                    if(nums[j] > nums[i]){
                        count[j]++;
                    }
                }
            }
           return count;
    }
};
class Solution {
public:
    /*algorithm: binary search tree
    time O(nlgn) space O(n)
    */
    struct TreeNode{
        int val;
        int cnt;//left child nodes count
        int same; //same number nodes count
        TreeNode* left,*right;
        TreeNode(int v):val(v),cnt(0),same(1),left(NULL),right(NULL){}
    };
    int insert(TreeNode* &root,int val){
        if(!root){
            root = new TreeNode(val);
            return 0;
        }
        TreeNode* t = root;
        int count = 0;
        while(t){
            if(t->val > val){
                t->cnt++;
                if(!t->left){
                    t->left = new TreeNode(val);
                    break;
                }
                t = t->left;
            }else if(t->val < val){
                count += t->cnt + t->same;
                if(!t->right){
                    t->right = new TreeNode(val);
                    break;
                }
                t = t->right;
            }else{
                count += t->cnt;
                t->same++; //add self number occurence
                break;
            }
        }
        return count;
    }
    vector<int> countSmaller(vector<int>& nums) {
        vector<int>count(nums.size(),0);
        TreeNode* root = NULL;
        for(int i = (int)nums.size()-1;i >= 0;i--){
            count[i] = insert(root,nums[i]);
        }
        return count;
    }
};

 /*algorithm: merge sort
    time O(nlgn)
    */
    struct NumberIndex {
        int number;
        int index;
        NumberIndex(int val, int index):number(val),index(index){}
    };
    vector<NumberIndex> mergeSort(vector<NumberIndex>&nums,vector<int>&smaller) {
        int half = nums.size() / 2;
        if (half > 0) {
            vector<NumberIndex> leftPart,rightPart;
            int k;
            for (k = 0; k < half; k++) {
                leftPart.push_back(nums[k]);
            }
            for (;k < nums.size();k++) {
                rightPart.push_back(nums[k]);
            }
            vector<NumberIndex> left = mergeSort(leftPart, smaller);
            vector<NumberIndex> right = mergeSort(rightPart, smaller);
            int i = 0, j = 0,d = 0;
            while (i <left.size() || j < right.size()) {
                if (j == right.size() || i < left.size() && left[i].number <= right[j].number) {
                    nums[d++] = left[i];
                    smaller[left[i].index] += j;
                    i++;
                } else {
                    nums[d++] = right[j];
                    j++;
                }
            }
        }
        
        return nums;
    }

    vector<int> countSmaller(vector<int>& nums) {
        vector<NumberIndex>cnums;
        for (int i = 0; i < nums.size(); i++) {
            cnums.push_back(NumberIndex(nums[i],i));
        }
        vector<int>smaller(nums.size(),0);
        mergeSort(cnums, smaller);
        return smaller;
    }


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值