Count of Smaller Numbers After Self
You are given an integer arraynumsand you have to return a newcountsarray. Thecountsarray has the property wherecounts[i]is the number of smaller elements to the right ofnums[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.binary index tree
和后面reverse pair比较,正序update是因为提高的权重应该是比它大的数字(index比它高)。因为右边起,所以只有右边的数能让左边数权重变大
Nummap里存排序好的数,ordered num->ordered index
右边起,遇到数就更新树,让大于它的Parent的权重都+1.(index > currrent i)
segment tree
也是排序建立map:ordered num->ordered index
倒序遍历,先得到范围前的sum,然后然后该数范围权重+=1
Last updated
Was this helpful?