Skip to content

Commit

Permalink
Merge pull request #593 from 0xff-dev/2471
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 2471
  • Loading branch information
6boris authored Aug 22, 2023
2 parents fd25da6 + 89aa903 commit 3fc788e
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 22 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,28 +1,53 @@
# [2471.Minimum Number of Operations to Sort a Binary Tree by Level][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
## Description
You are given the `root` of a binary tree with **unique values**.

In one operation, you can choose any two nodes **at the same level** and swap their values.

Return the minimum number of operations needed to make the values at each level sorted in a **strictly increasing order**.

The **level** of a node is the number of edges along the path between it and the root node.

**Example 1:**

**Example 1:**
![example1](./1.png)

```
Input: a = "11", b = "1"
Output: "100"
Input: root = [1,4,3,7,6,8,5,null,null,null,null,9,null,10]
Output: 3
Explanation:
- Swap 4 and 3. The 2nd level becomes [3,4].
- Swap 7 and 5. The 3rd level becomes [5,6,8,7].
- Swap 8 and 7. The 3rd level becomes [5,6,7,8].
We used 3 operations so return 3.
It can be proven that 3 is the minimum number of operations needed.
```

## 题意
> ...
**Example 2:**

## 题解
![example2](./2.png)

### 思路1
> ...
Minimum Number of Operations to Sort a Binary Tree by Level
```go
```
Input: root = [1,3,2,7,6,5,4]
Output: 3
Explanation:
- Swap 3 and 2. The 2nd level becomes [2,3].
- Swap 7 and 4. The 3rd level becomes [4,6,5,7].
- Swap 6 and 5. The 3rd level becomes [4,5,6,7].
We used 3 operations so return 3.
It can be proven that 3 is the minimum number of operations needed.
```

**Example 3:**

![example3](./3.png)

```
Input: root = [1,2,3,4,5,6]
Output: 0
Explanation: Each level is already sorted in increasing order so return 0.
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,65 @@
package Solution

func Solution(x bool) bool {
return x
import (
"sort"
)

type TreeNode struct {
Val int
Left, Right *TreeNode
}

func minimumCostSort(nums []int) int {
b := make([]int, len(nums))
rightPos := make(map[int]int)
copy(b, nums)
sort.Ints(b)
for i, n := range b {
rightPos[n] = i
}
swapCount := 0
idx := 0
for idx < len(nums) {
if nums[idx] == b[idx] {
idx++
continue
}
correctPos := rightPos[nums[idx]]
nums[idx], nums[correctPos] = nums[correctPos], nums[idx]
swapCount++

}
/*
for idx := 0; idx < len(nums); idx++ {
if nums[idx] == b[idx] {
continue
}
correctPos := rightPos[nums[idx]]
nums[idx], nums[correctPos] = nums[correctPos], nums[idx]
swapCount++
}
*/
return swapCount
}

func Solution(root *TreeNode) int {
ans := 0
queue := []*TreeNode{root}
for len(queue) > 0 {
next := make([]*TreeNode, 0)
values := make([]int, 0)
for _, item := range queue {
if item.Left != nil {
next = append(next, item.Left)
values = append(values, item.Left.Val)
}
if item.Right != nil {
next = append(next, item.Right)
values = append(values, item.Right.Val)
}
}
ans += minimumCostSort(values)
queue = next
}
return ans
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,52 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs *TreeNode
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", &TreeNode{
Val: 1,
Left: &TreeNode{
Val: 4,
Left: &TreeNode{Val: 7},
Right: &TreeNode{Val: 6},
},
Right: &TreeNode{
Val: 3,
Left: &TreeNode{
Val: 8,
Left: &TreeNode{Val: 9},
},
Right: &TreeNode{Val: 5, Left: &TreeNode{Val: 10}},
},
}, 3},
{"TestCase2", &TreeNode{
Val: 1,
Left: &TreeNode{
Val: 3,
Left: &TreeNode{Val: 7},
Right: &TreeNode{Val: 6},
},
Right: &TreeNode{
Val: 2,
Left: &TreeNode{
Val: 5,
},
Right: &TreeNode{Val: 4},
},
}, 3},
{"TestCase3", &TreeNode{
Val: 1,
Left: &TreeNode{
Val: 2,
Left: &TreeNode{Val: 4},
Right: &TreeNode{Val: 5},
},
Right: &TreeNode{
Val: 3,
Left: &TreeNode{Val: 6},
},
}, 0},
}

// 开始测试
Expand All @@ -30,10 +70,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}

0 comments on commit 3fc788e

Please sign in to comment.