LeetCode Count of Smaller Number 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].

Solution 1(divide and conquer):

vector<int> countSmaller(vector<int>& nums) {
        int n = nums.size();
        vector<int> cnt(n, 0);
        vector<int> index(n);
        for(int i = 0; i<n; i++)
            index[i] = i;
        mergeSort(nums, cnt, index, 0, n-1);
        return cnt;
    }
    void mergeSort(vector<int>& nums, vector<int>& cnt, vector<int>& index, int left, int right) {
        if(left >= right) return;
        int mid = (left + right)/2;
        mergeSort(nums, cnt, index, left, mid);
        mergeSort(nums, cnt, index, mid+1, right);
        merge(nums, cnt, index, left, right);
    }
    void merge(vector<int>& nums, vector<int>& cnt, vector<int>& index, int left, int right) {
        int mid = (left+right)/2;
        int i = left, j = mid+1, k = 0, count = 0;
        vector<int> newindex(right-left+1);
        while(i<=mid || j<=right){
            if(i>mid)
                newindex[k++] = index[j++];
            else if(j>right){
                cnt[index[i]] += count;
                newindex[k++] = index[i++];
            }
            else if(nums[index[i]] <= nums[index[j]]){
                cnt[index[i]] += count;
                newindex[k++] = index[i++];
            }
            else{
                newindex[k++] = index[j++];
                count++;
            }
        }
        for(i = 0; i<=right-left; i++)
            index[left+i] = newindex[i];
    }

notes:
Should change the index instead of the array itself.
reference:
https://leetcode.com/problems/count-of-smaller-numbers-after-self/discuss/76583/11ms-JAVA-solution-using-merge-sort-with-explanation

Solution 2(augmented BST):

class TreeNode{
    public:
        TreeNode *left = NULL, *right = NULL;
        int val, sum, dup = 1;
        TreeNode(int val){
            this->val = val;
            this->sum = 0;
        }
    };
    vector<int> countSmaller(vector<int>& nums) {
        int n = nums.size();
        vector<int> ans(n, 0);
        TreeNode* root = NULL;
        for(int i = n-1; i>=0; i--)
            root = insert(nums[i], ans, root, i, 0);
        return ans;
    }
    TreeNode* insert(int num, vector<int>& ans, TreeNode* root, int i, int preSum) {
        if(root == NULL){
            root = new TreeNode(num);
            ans[i] = preSum;
        }
        else if(num == root->val){
            root->dup++;
            ans[i] = preSum + root->sum;
        }
        else if(num < root->val){
            root->sum++;
            root->left = insert(num, ans, root->left, i, preSum);
        }
        else{
            root->right = insert(num, ans, root->right, i, preSum + root->sum + root->dup);
        }
        return root;
    }

reference:
https://leetcode.com/problems/count-of-smaller-numbers-after-self/discuss/76580/9ms-short-Java-BST-solution-get-answer-when-building-BST

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,448评论 0 10
  • 01 我的一个朋友晓晓,大学毕业后因为参加同学的婚礼,认识一个北漂男孩天哥。天哥瘦瘦高高的,脸蛋长得还算精致,一笑...
    孜晓阅读 922评论 5 50
  • 现在是凌晨两点多,一连几个夜都无法入睡,想写点什么,在自己还清醒能记住过往的回忆里,想把发生在自己身上的事写下来,...
    高估别人阅读 41评论 0 0
  • 萧萧月,冷冽寒冬夜。强自彷徨,独等待。月光寒,风凛冽。孤灯床前倚,落雨不成席。待到明月夜,只思乡。乡间伊人啐,嬉戏...
    逍遥为乐阅读 429评论 0 0
  • 我家就住在鑫农湖附近,世代为耕,但也是书香门第。昨天一早,天渐渐的放晴了,我开心的吃过早餐,跨上了那张不启用的旧事...
    米澜盛若阅读 181评论 0 0