Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@
- 线段树的每个叶子节点代表一个长度为 1 的元区间 `[x, x]`;
- 对于每个内部节点 `[l, r]`,它的左儿子是 `[l, mid]`,右儿子是 `[mid + 1, r]`, 其中 `mid = ⌊(l + r) / 2⌋` (即向下取整)。

**方法三:归并排序**

<!-- tabs:start -->

### **Python3**
Expand Down Expand Up @@ -523,6 +525,67 @@ func countSmaller(nums []int) []int {
}
```

归并排序:

```go
type Pair struct {
val int
index int
}

var (
tmp []Pair
count []int
)

func countSmaller(nums []int) []int {
tmp, count = make([]Pair, len(nums)), make([]int, len(nums))
array := make([]Pair, len(nums))
for i, v := range nums {
array[i] = Pair{val: v, index: i}
}
sorted(array, 0, len(array)-1)
return count
}

func sorted(arr []Pair, low, high int) {
if low >= high {
return
}
mid := low + (high-low)/2
sorted(arr, low, mid)
sorted(arr, mid+1, high)
merge(arr, low, mid, high)
}

func merge(arr []Pair, low, mid, high int) {
left, right := low, mid+1
idx := low
for left <= mid && right <= high {
if arr[left].val <= arr[right].val {
count[arr[left].index] += right - mid - 1
tmp[idx], left = arr[left], left+1
} else {
tmp[idx], right = arr[right], right+1
}
idx++
}
for left <= mid {
count[arr[left].index] += right - mid - 1
tmp[idx] = arr[left]
idx, left = idx+1, left+1
}
for right <= high {
tmp[idx] = arr[right]
idx, right = idx+1, right+1
}
// 排序
for i := low; i <= high; i++ {
arr[i] = tmp[i]
}
}
```

### **...**

```
Expand Down