-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add solution and test-cases for problem 2471
- Loading branch information
Showing
6 changed files
with
147 additions
and
22 deletions.
There are no files selected for viewing
Binary file added
BIN
+33.9 KB
...401-2500/2471.Minimum-Number-of-Operations-to-Sort-a-Binary-Tree-by-Level/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+23.1 KB
...401-2500/2471.Minimum-Number-of-Operations-to-Sort-a-Binary-Tree-by-Level/2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+29.1 KB
...401-2500/2471.Minimum-Number-of-Operations-to-Sort-a-Binary-Tree-by-Level/3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
51 changes: 38 additions & 13 deletions
51
...2500/2471.Minimum-Number-of-Operations-to-Sort-a-Binary-Tree-by-Level/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 62 additions & 2 deletions
64
...de/2401-2500/2471.Minimum-Number-of-Operations-to-Sort-a-Binary-Tree-by-Level/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters