feat: stuff

This commit is contained in:
2026-04-02 15:43:24 +02:00
parent 3586c94e25
commit 2ca8bb4427
7 changed files with 113 additions and 17 deletions

View File

@@ -43,7 +43,7 @@ func buildTreeInfo(info []NodeInfo) *TreeNode {
}
}
func buildTree(preorder []int, inorder []int) *TreeNode {
func BuildTree(preorder []int, inorder []int) *TreeNode {
info_map := map[int]*NodeInfo{}
for o, v := range preorder {
info_map[v] = &NodeInfo{

View File

@@ -1,26 +1,53 @@
package count_paths_that_can_form_a_palindrome_in_a_tree
func isPalindromable(s string) bool {
set := map[rune]int{}
type Node struct {
Mask uint64
Children []*Node
}
for _, b := range s {
set[b]++
func toTree(parents []int, s string) *Node {
nodes := []*Node{}
for i := range s {
nodes = append(nodes, &Node{1 << (s[i] - 'a'), []*Node{}})
}
singletons := 0
for _, v := range set {
switch v {
case 1:
singletons++
case 2:
default:
return false
for child, parent := range parents {
if parent >= 0 {
nodes[parent].Children = append(nodes[parent].Children, nodes[child])
}
}
return singletons < 2
return nodes[0]
}
func doDFS(node *Node, path uint64, masks map[uint64]int64) {
if node == nil {
return
}
path ^= node.Mask
masks[path]++
for _, child := range node.Children {
doDFS(child, path, masks)
}
}
func CountPalindromePaths(parent []int, s string) int64 {
return 0
tree := toTree(parent, s)
masks := map[uint64]int64{}
doDFS(tree, 0, masks)
valid := int64(0)
for mask, count := range masks {
valid += count * (count + 1) / 2
for bit := range 26 {
valid += count * masks[mask^(1<<bit)]
}
}
return valid
}

View File

@@ -26,7 +26,7 @@ func visit(prereqs map[int][]int, temporary map[int]bool, permanent map[int]bool
return true
}
func canFinish(numCourses int, prerequisites [][]int) bool {
func CanFinish(numCourses int, prerequisites [][]int) bool {
prereqs := map[int][]int{}
for _, p := range prerequisites {
class, prerequisite := p[0], p[1]

View File

@@ -0,0 +1,23 @@
package maximumnumberofnonoverlappingpalindromesubstrings
import (
"maps"
"slices"
)
func FindPalindromes(letters string) [][2]int {
L := map[[2]int]bool{}
for length := range letters {
for start := range len(letters) - length {
end := start + length
if length < 2 {
L[[2]int{start, end}] = true
} else {
L[[2]int{start, end}] = L[[2]int{start + 1, end - 1}] && letters[start] == letters[end]
}
}
}
return slices.Collect(maps.Keys(L))
}

View File

@@ -0,0 +1,11 @@
package maximum_sliding_window
import (
"git.maximhutz.com/practice/pkg/tools/heap"
)
func MaxSlidingWindow(nums []int, k int) []int {
h := heap.NewBy(heap.More, nums[:k]...)
result := []int{h.Data[0]}
return result
}

View File

@@ -74,7 +74,7 @@ func (s *Substring) CullRight(t Substring) {
}
}
func minWindow(s string, t string) string {
func MinWindow(s string, t string) string {
ss, tt := NewSubstring(s), NewSubstring(t)
best_answer := ""