【LeetCode 315】Count of Smaller Numbers After Self

18 篇文章 0 订阅
1 篇文章 0 订阅

题目描述:

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:

Input: [5,2,6,1]
Output: [2,1,1,0] 
Explanation:
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.

思路:

思路1:树状数组。哎…树状数组都不会了,回去复习了一下。

思路2:倒叙遍历原数组,维护一个有序数组,依次插入进去,插入之前二分查找数组中比它小的数字有多少个,即应该插入的位置。

树状数组胜。

代码:

树状数组:

class Solution {
public:
    int lowbit(int x) {
        return x & (-x);
    }

    int getsum(int x, vector<int> &c) {
        int sum = 0;
        for (int i=x; i>0; i-=lowbit(i)) {
            sum += c[i];
        }
        return sum;
    }

    void update(vector<int> &c, int x, int y) {
        for (int i=x; i<c.size(); i+=lowbit(i)) {
            c[i] += y;
        }
    }

    vector<int> countSmaller(vector<int>& nums) {
        if (nums.empty()) return {};
        vector<int> ans;
        int len = nums.size();
        vector<int> sum(len+1, 0);

        vector<int> temp = nums;
        sort(temp.begin(), temp.end());
        map<int, int> mp;

        for (int i=0; i<len; ++i) {
            mp[temp[i]] = i+1;
        }

        for (int i=len-1; i>=0; --i) {
            int temp = 0;
            int val = mp[nums[i]];
            if (val > 1) {
                temp = getsum(val-1, sum);
            }
            ans.push_back(temp);
            update(sum, val, 1);
        }
        reverse(ans.begin(), ans.end());
        return ans;
    }
};

二分查找:

class Solution {
public:
    vector<int> countSmaller(vector<int>& nums) {
        int len = nums.size();
        vector<int> tmp;
        vector<int> ans;
        for (int i=len-1; i>=0; --i) {
            int temp = nums[i];
            auto it = lower_bound(tmp.begin(), tmp.end(), nums[i]);
            ans.push_back(it-tmp.begin());
            tmp.insert(it, nums[i]);
        }
        reverse(ans.begin(), ans.end());
        return ans;
    }
};

二分代码

class Solution {
private:
    int find_(vector<int>& nums, int target) {
        if (nums.empty()) return 0;
        int left = 0, right = nums.size()-1;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] < target) left = mid + 1;
            else right = mid - 1;
        }
        return left;
    }
    
public:
    vector<int> countSmaller(vector<int>& nums) {
        vector<int> res(nums.size());
        vector<int> sorted;
        for (int i=nums.size()-1; i>=0; --i) {
            int index = find_(sorted, nums[i]);
            res[i] = index;
            sorted.insert(sorted.begin()+index, nums[i]);
        }
        return res;
    }
};

日期

2019 年 03 月 19 日 —— 树状数组 和 二分两种方法
2020 年 05 月 20 日 —— 二分
2020 年 07 月 12 日 —— 二分

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值