feat: stuff
This commit is contained in:
@@ -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{}
|
info_map := map[int]*NodeInfo{}
|
||||||
for o, v := range preorder {
|
for o, v := range preorder {
|
||||||
info_map[v] = &NodeInfo{
|
info_map[v] = &NodeInfo{
|
||||||
|
|||||||
@@ -1,26 +1,53 @@
|
|||||||
package count_paths_that_can_form_a_palindrome_in_a_tree
|
package count_paths_that_can_form_a_palindrome_in_a_tree
|
||||||
|
|
||||||
func isPalindromable(s string) bool {
|
type Node struct {
|
||||||
set := map[rune]int{}
|
Mask uint64
|
||||||
|
Children []*Node
|
||||||
for _, b := range s {
|
|
||||||
set[b]++
|
|
||||||
}
|
}
|
||||||
|
|
||||||
singletons := 0
|
func toTree(parents []int, s string) *Node {
|
||||||
for _, v := range set {
|
nodes := []*Node{}
|
||||||
switch v {
|
for i := range s {
|
||||||
case 1:
|
nodes = append(nodes, &Node{1 << (s[i] - 'a'), []*Node{}})
|
||||||
singletons++
|
}
|
||||||
case 2:
|
|
||||||
default:
|
for child, parent := range parents {
|
||||||
return false
|
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 {
|
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
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ func visit(prereqs map[int][]int, temporary map[int]bool, permanent map[int]bool
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func canFinish(numCourses int, prerequisites [][]int) bool {
|
func CanFinish(numCourses int, prerequisites [][]int) bool {
|
||||||
prereqs := map[int][]int{}
|
prereqs := map[int][]int{}
|
||||||
for _, p := range prerequisites {
|
for _, p := range prerequisites {
|
||||||
class, prerequisite := p[0], p[1]
|
class, prerequisite := p[0], p[1]
|
||||||
|
|||||||
@@ -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))
|
||||||
|
}
|
||||||
11
pkg/questions/maximum_sliding_window/main.go
Normal file
11
pkg/questions/maximum_sliding_window/main.go
Normal 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
|
||||||
|
}
|
||||||
@@ -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)
|
ss, tt := NewSubstring(s), NewSubstring(t)
|
||||||
best_answer := ""
|
best_answer := ""
|
||||||
|
|
||||||
|
|||||||
35
pkg/tools/heap/main.go
Normal file
35
pkg/tools/heap/main.go
Normal 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]
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user