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{}
for _, b := range s {
set[b]++
type Node struct {
Mask uint64
Children []*Node
}
singletons := 0
for _, v := range set {
switch v {
case 1:
singletons++
case 2:
default:
return false
func toTree(parents []int, s string) *Node {
nodes := []*Node{}
for i := range s {
nodes = append(nodes, &Node{1 << (s[i] - 'a'), []*Node{}})
}
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 := ""

35
pkg/tools/heap/main.go Normal file
View File

@@ -0,0 +1,35 @@
package heap
import (
"cmp"
"container/heap"
)
type Heap[T any] struct {
Data []T
lessFunc func(a, b T) bool
}
func More[T cmp.Ordered](x, y T) bool {
return !cmp.Less(x, y) && x != y
}
func NewBy[T any](lessFunc func(a, b T) bool, items ...T) *Heap[T] {
h := &Heap[T]{items, lessFunc}
heap.Init(h)
return h
}
func New[T cmp.Ordered](items ...T) *Heap[T] {
return NewBy[T](func(a, b T) bool { return a < b }, items...)
}
func (h Heap[T]) Len() int { return len(h.Data) }
func (h Heap[T]) Less(i, j int) bool { return h.lessFunc(h.Data[i], h.Data[j]) }
func (h Heap[T]) Swap(i, j int) { h.Data[i], h.Data[j] = h.Data[j], h.Data[i] }
func (h *Heap[T]) Push(x any) { h.Data = append(h.Data, x.(T)) }
func (h *Heap[T]) Pop() any {
defer func() { h.Data = h.Data[:len(h.Data)-1] }()
return h.Data[len(h.Data)-1]
}