website/content.en/ChapterFour/0300~0399/0315.Count-of-Smaller-Numbers-After-Self.md
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.
Given an integer array nums,return a new array counts as required。The array counts has this property: the value of counts[i] is the number of elements to the right of nums[i] that are smaller than 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 are 0 smaller elements.
[1,i-1] interval, which is the number of smaller elements on the right。Note that the final output is in forward order, while the computation is done in reverse order,so the answers in the final array still need to be reversed once。Similar routine problems include,Problem 327,Problem 493。
package leetcode
import (
"sort"
"github.com/halfrost/leetcode-go/template"
)
// Solution One Segment Tree
func countSmaller(nums []int) []int {
if len(nums) == 0 {
return []int{}
}
st, minNum, numsMap, numsArray, res := template.SegmentCountTree{}, 0, make(map[int]int, 0), []int{}, make([]int, len(nums))
for i := 0; i < len(nums); i++ {
numsMap[nums[i]] = nums[i]
}
for _, v := range numsMap {
numsArray = append(numsArray, v)
}
// Sorting is to make the intervals in the segment tree satisfy left <= right,if not sorted here,many intervals in the segment tree will be invalid。
sort.Ints(numsArray)
minNum = numsArray[0]
// Initialize the segment tree,the values inside the nodes are all assigned 0,that is, the count is 0
st.Init(numsArray, func(i, j int) int {
return 0
})
for i := len(nums) - 1; i >= 0; i-- {
if nums[i] == minNum {
res[i] = 0
st.UpdateCount(nums[i])
continue
}
st.UpdateCount(nums[i])
res[i] = st.Query(minNum, nums[i]-1)
}
return res
}
// Solution Two Binary Indexed Tree
func countSmaller1(nums []int) []int {
// copy one copy of the original array to the allNums array of all numbers
allNums, res := make([]int, len(nums)), []int{}
copy(allNums, nums)
// Discretize allNums
sort.Ints(allNums)
k := 1
kth := map[int]int{allNums[0]: k}
for i := 1; i < len(allNums); i++ {
if allNums[i] != allNums[i-1] {
k++
kth[allNums[i]] = k
}
}
// Binary Indexed Tree Query
bit := template.BinaryIndexedTree{}
bit.Init(k)
for i := len(nums) - 1; i >= 0; i-- {
res = append(res, bit.Query(kth[nums[i]]-1))
bit.Add(kth[nums[i]], 1)
}
for i := 0; i < len(res)/2; i++ {
res[i], res[len(res)-1-i] = res[len(res)-1-i], res[i]
}
return res
}