315. 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].

解题思路:求逆序数对,可以用线段树或者后缀数组求解。由于数值可能为负数,所以需要离散化或者加上最小的数+1。

离散化:

#define lowbit(i) (i&(-i))
const int maxn = 100010;
struct node{
    int val,pos;
}temp[maxn];
bool cmp(node a,node b){
    return a.val<b.val;
}
class Solution {
public:
    int c[maxn]={0};
    int A[maxn]={0};
    void update(int x,int v){
        for(int i=x;i<maxn;i+=lowbit(i)){
            c[i]+=v;
        }
    }
    int getSum(int x){
        int sum=0;
        for(int i=x;i>0;i-=lowbit(i)){
            sum+=c[i];
        }
        return sum;
    }

    vector<int> countSmaller(vector<int>& nums) {
        vector<int>ans;
        int n=nums.size();
        for(int i=0;i<n;i++){
            temp[i].pos=i;
            temp[i].val=nums[i];
        }
        sort(temp,temp+n,cmp);
        for(int i=0;i<n;i++){
            if(i==0||temp[i].val!=temp[i-1].val){
                A[temp[i].pos]=i+1;
            }
            else  A[temp[i].pos]= A[temp[i-1].pos];
        }
        for(int i=n-1;i>=0;i--){
            update(A[i],1);
            ans.push_back(getSum(A[i]-1));
        }
        reverse(ans.begin(),ans.end());
        return ans;
    }
};

最小数+1:

#define lowbit(i) (i&(-i))
const int maxn = 100010;
class Solution {
public:
    int c[maxn]={0};
    void update(int x,int v){
        for(int i=x;i<maxn;i+=lowbit(i)){
            c[i]+=v;
        }
    }
    int getSum(int x){
        int sum=0;
        for(int i=x;i>0;i-=lowbit(i)){
            sum+=c[i];
        }
        return sum;
    }
    vector<int> countSmaller(vector<int>& nums) {
        vector<int>ans;
        int n=nums.size(),mmin=INT_MAX;
        for(int i=0;i<n;i++){
            mmin=min(mmin,nums[i]);
        }
        for(int i=0;i<n;i++){
            nums[i]=nums[i]-mmin+1;
        }
        for(int i=n-1;i>=0;i--){
            update(nums[i],1);
            ans.push_back(getSum(nums[i]-1));
        }
        reverse(ans.begin(),ans.end());
        return ans;
    }
};
时间: 2024-08-06 07:44:26

315. Count of Smaller Numbers After Self的相关文章

剑指Offer 面试题36:数组中的逆序对及其变形(Leetcode 315. Count of Smaller Numbers After Self)题解

剑指Offer 面试题36:数组中的逆序对 题目:在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组中的逆序对的总数. 例如, 在数组{7,5,6,4}中,一共存在5个逆序对,分别是(7,6),(7,5),(7,4),(6,4)和(5,4),输出5. 提交网址: http://www.nowcoder.com/practice/96bd6684e04a44eb80e6a68efc0ec6c5?tpId=13&tqId=11188 或 htt

leetcode 315. 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

第十四周 Leetcode 315. Count of Smaller Numbers After Self(HARD) 主席树

Leetcode315 题意很简单,给定一个序列,求每一个数的右边有多少小于它的树. O(n^2)的算法是显而易见的. 用普通的线段树可以优化到O(nlogn) 我们可以直接套用主席树的模板. 主席树的功能是什么呢? 其实就是一句话. 原序列a的子序列a[l,r]在a排序后的序列b的子序列[L,R]中的个数. 显然本题只用到了主席树的一小部分功能. const int N = 100000 + 5; int a[N], b[N], rt[N * 20], ls[N * 20], rs[N * 2

[LeetCode][JavaScript]Count of Smaller Numbers After Self

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,

leetcode Count of Smaller Numbers After Self

题目连接 https://leetcode.com/problems/count-of-smaller-numbers-after-self/ Count of Smaller Numbers After Self Description 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]$

[email&#160;protected] [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

Count of Smaller Numbers After Self -- LeetCode

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

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

[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 of nums[i]. Example: Given nums = [5, 2, 6, 1] To the right of 5 there are