From aad6f3e91f4fb9424b951f1c21cf6745c6155108 Mon Sep 17 00:00:00 2001 From: "M.V. Hutz" Date: Fri, 6 Mar 2026 19:17:30 -0500 Subject: [PATCH] feat: osijvrsoi --- .../best_time_to_buy_and_sell_stock/main.go | 0 pkg/{ => questions}/car_fleet/main.go | 0 pkg/{ => questions}/car_fleet/main_test.go | 0 .../main.go | 0 .../container_with_most_water/main.go | 0 .../main.go | 26 +++++++++ pkg/questions/course_schedule/main.go | 53 +++++++++++++++++ .../find_median_from_data_stream/main.go | 0 .../main.go | 0 pkg/{ => questions}/group_anagrams/main.go | 0 .../group_anagrams/main_test.go | 0 .../largest_rectangle_in_histogram/main.go | 0 .../main_test.go | 0 pkg/{ => questions}/linked_list_cycle/main.go | 0 .../longest_consecutive_sequence/main.go | 0 .../longest_consecutive_sequence/main_test.go | 0 .../merge_k_sorted_lists/main.go | 0 .../merge_two_sorted_lists/main.go | 0 .../minimum_window_substring/main.go | 0 .../pacific_atlantic_water_flow/main.go | 0 .../pacific_atlantic_water_flow/main_test.go | 0 .../product_of_array_except_self/main.go | 0 .../product_of_array_except_self/main_test.go | 0 .../remove_nth_node_from_end_of_list/main.go | 0 pkg/{ => questions}/reorder_list/main.go | 0 .../reverse_linked_list/main.go | 0 .../search_in_rotated_sorted_array/main.go | 0 pkg/{ => questions}/three_sum/main.go | 0 pkg/{ => questions}/three_sum/main_test.go | 0 .../top_k_frequent_elements/main.go | 0 .../top_k_frequent_elements/main_test.go | 0 .../trapping_rain_water/main.go | 0 .../trapping_rain_water/main_test.go | 0 pkg/{ => questions}/two_sum/main.go | 0 pkg/{ => questions}/two_sum/main_test.go | 0 pkg/{ => questions}/valid_palindrome/main.go | 0 .../valid_palindrome/main_test.go | 0 pkg/{ => questions}/valid_sudoku/main.go | 0 pkg/{ => questions}/valid_sudoku/main_test.go | 0 pkg/{ => questions}/wildcard_matching/main.go | 0 .../wildcard_matching/main_test.go | 0 pkg/{ => questions}/word_search_ii/main.go | 0 pkg/tools/list/main.go | 57 +++++++++++++++++++ 43 files changed, 136 insertions(+) rename pkg/{ => questions}/best_time_to_buy_and_sell_stock/main.go (100%) rename pkg/{ => questions}/car_fleet/main.go (100%) rename pkg/{ => questions}/car_fleet/main_test.go (100%) rename pkg/{ => questions}/construct_binary_tree_from_preorder_and_inorder_traversal/main.go (100%) rename pkg/{ => questions}/container_with_most_water/main.go (100%) create mode 100644 pkg/questions/count_paths_that_can_form_a_palindrome_in_a_tree/main.go create mode 100644 pkg/questions/course_schedule/main.go rename pkg/{ => questions}/find_median_from_data_stream/main.go (100%) rename pkg/{ => questions}/find_minimum_in_rotated_sorted_array/main.go (100%) rename pkg/{ => questions}/group_anagrams/main.go (100%) rename pkg/{ => questions}/group_anagrams/main_test.go (100%) rename pkg/{ => questions}/largest_rectangle_in_histogram/main.go (100%) rename pkg/{ => questions}/largest_rectangle_in_histogram/main_test.go (100%) rename pkg/{ => questions}/linked_list_cycle/main.go (100%) rename pkg/{ => questions}/longest_consecutive_sequence/main.go (100%) rename pkg/{ => questions}/longest_consecutive_sequence/main_test.go (100%) rename pkg/{ => questions}/merge_k_sorted_lists/main.go (100%) rename pkg/{ => questions}/merge_two_sorted_lists/main.go (100%) rename pkg/{ => questions}/minimum_window_substring/main.go (100%) rename pkg/{ => questions}/pacific_atlantic_water_flow/main.go (100%) rename pkg/{ => questions}/pacific_atlantic_water_flow/main_test.go (100%) rename pkg/{ => questions}/product_of_array_except_self/main.go (100%) rename pkg/{ => questions}/product_of_array_except_self/main_test.go (100%) rename pkg/{ => questions}/remove_nth_node_from_end_of_list/main.go (100%) rename pkg/{ => questions}/reorder_list/main.go (100%) rename pkg/{ => questions}/reverse_linked_list/main.go (100%) rename pkg/{ => questions}/search_in_rotated_sorted_array/main.go (100%) rename pkg/{ => questions}/three_sum/main.go (100%) rename pkg/{ => questions}/three_sum/main_test.go (100%) rename pkg/{ => questions}/top_k_frequent_elements/main.go (100%) rename pkg/{ => questions}/top_k_frequent_elements/main_test.go (100%) rename pkg/{ => questions}/trapping_rain_water/main.go (100%) rename pkg/{ => questions}/trapping_rain_water/main_test.go (100%) rename pkg/{ => questions}/two_sum/main.go (100%) rename pkg/{ => questions}/two_sum/main_test.go (100%) rename pkg/{ => questions}/valid_palindrome/main.go (100%) rename pkg/{ => questions}/valid_palindrome/main_test.go (100%) rename pkg/{ => questions}/valid_sudoku/main.go (100%) rename pkg/{ => questions}/valid_sudoku/main_test.go (100%) rename pkg/{ => questions}/wildcard_matching/main.go (100%) rename pkg/{ => questions}/wildcard_matching/main_test.go (100%) rename pkg/{ => questions}/word_search_ii/main.go (100%) create mode 100644 pkg/tools/list/main.go diff --git a/pkg/best_time_to_buy_and_sell_stock/main.go b/pkg/questions/best_time_to_buy_and_sell_stock/main.go similarity index 100% rename from pkg/best_time_to_buy_and_sell_stock/main.go rename to pkg/questions/best_time_to_buy_and_sell_stock/main.go diff --git a/pkg/car_fleet/main.go b/pkg/questions/car_fleet/main.go similarity index 100% rename from pkg/car_fleet/main.go rename to pkg/questions/car_fleet/main.go diff --git a/pkg/car_fleet/main_test.go b/pkg/questions/car_fleet/main_test.go similarity index 100% rename from pkg/car_fleet/main_test.go rename to pkg/questions/car_fleet/main_test.go diff --git a/pkg/construct_binary_tree_from_preorder_and_inorder_traversal/main.go b/pkg/questions/construct_binary_tree_from_preorder_and_inorder_traversal/main.go similarity index 100% rename from pkg/construct_binary_tree_from_preorder_and_inorder_traversal/main.go rename to pkg/questions/construct_binary_tree_from_preorder_and_inorder_traversal/main.go diff --git a/pkg/container_with_most_water/main.go b/pkg/questions/container_with_most_water/main.go similarity index 100% rename from pkg/container_with_most_water/main.go rename to pkg/questions/container_with_most_water/main.go diff --git a/pkg/questions/count_paths_that_can_form_a_palindrome_in_a_tree/main.go b/pkg/questions/count_paths_that_can_form_a_palindrome_in_a_tree/main.go new file mode 100644 index 0000000..906ee96 --- /dev/null +++ b/pkg/questions/count_paths_that_can_form_a_palindrome_in_a_tree/main.go @@ -0,0 +1,26 @@ +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]++ + } + + singletons := 0 + for _, v := range set { + switch v { + case 1: + singletons++ + case 2: + default: + return false + } + } + + return singletons < 2 +} + +func CountPalindromePaths(parent []int, s string) int64 { + return 0 +} diff --git a/pkg/questions/course_schedule/main.go b/pkg/questions/course_schedule/main.go new file mode 100644 index 0000000..2989ffc --- /dev/null +++ b/pkg/questions/course_schedule/main.go @@ -0,0 +1,53 @@ +package courseschedule + +func visit(prereqs map[int][]int, temporary map[int]bool, permanent map[int]bool, class int) bool { + if temporary[class] { + return false + } + if permanent[class] { + return true + } + + if _, ok := prereqs[class]; !ok { + return true + } + + temporary[class] = true + + for _, p := range prereqs[class] { + if !visit(prereqs, temporary, permanent, p) { + return false + } + } + + permanent[class] = true + temporary[class] = false + + return true +} + +func canFinish(numCourses int, prerequisites [][]int) bool { + prereqs := map[int][]int{} + for _, p := range prerequisites { + class, prerequisite := p[0], p[1] + + if _, ok := prereqs[class]; !ok { + prereqs[class] = []int{} + } + + prereqs[class] = append(prereqs[class], prerequisite) + } + + temporary, permanent := map[int]bool{}, map[int]bool{} + for c := range prereqs { + if permanent[c] { + continue + } + + if !visit(prereqs, temporary, permanent, c) { + return false + } + } + + return true +} diff --git a/pkg/find_median_from_data_stream/main.go b/pkg/questions/find_median_from_data_stream/main.go similarity index 100% rename from pkg/find_median_from_data_stream/main.go rename to pkg/questions/find_median_from_data_stream/main.go diff --git a/pkg/find_minimum_in_rotated_sorted_array/main.go b/pkg/questions/find_minimum_in_rotated_sorted_array/main.go similarity index 100% rename from pkg/find_minimum_in_rotated_sorted_array/main.go rename to pkg/questions/find_minimum_in_rotated_sorted_array/main.go diff --git a/pkg/group_anagrams/main.go b/pkg/questions/group_anagrams/main.go similarity index 100% rename from pkg/group_anagrams/main.go rename to pkg/questions/group_anagrams/main.go diff --git a/pkg/group_anagrams/main_test.go b/pkg/questions/group_anagrams/main_test.go similarity index 100% rename from pkg/group_anagrams/main_test.go rename to pkg/questions/group_anagrams/main_test.go diff --git a/pkg/largest_rectangle_in_histogram/main.go b/pkg/questions/largest_rectangle_in_histogram/main.go similarity index 100% rename from pkg/largest_rectangle_in_histogram/main.go rename to pkg/questions/largest_rectangle_in_histogram/main.go diff --git a/pkg/largest_rectangle_in_histogram/main_test.go b/pkg/questions/largest_rectangle_in_histogram/main_test.go similarity index 100% rename from pkg/largest_rectangle_in_histogram/main_test.go rename to pkg/questions/largest_rectangle_in_histogram/main_test.go diff --git a/pkg/linked_list_cycle/main.go b/pkg/questions/linked_list_cycle/main.go similarity index 100% rename from pkg/linked_list_cycle/main.go rename to pkg/questions/linked_list_cycle/main.go diff --git a/pkg/longest_consecutive_sequence/main.go b/pkg/questions/longest_consecutive_sequence/main.go similarity index 100% rename from pkg/longest_consecutive_sequence/main.go rename to pkg/questions/longest_consecutive_sequence/main.go diff --git a/pkg/longest_consecutive_sequence/main_test.go b/pkg/questions/longest_consecutive_sequence/main_test.go similarity index 100% rename from pkg/longest_consecutive_sequence/main_test.go rename to pkg/questions/longest_consecutive_sequence/main_test.go diff --git a/pkg/merge_k_sorted_lists/main.go b/pkg/questions/merge_k_sorted_lists/main.go similarity index 100% rename from pkg/merge_k_sorted_lists/main.go rename to pkg/questions/merge_k_sorted_lists/main.go diff --git a/pkg/merge_two_sorted_lists/main.go b/pkg/questions/merge_two_sorted_lists/main.go similarity index 100% rename from pkg/merge_two_sorted_lists/main.go rename to pkg/questions/merge_two_sorted_lists/main.go diff --git a/pkg/minimum_window_substring/main.go b/pkg/questions/minimum_window_substring/main.go similarity index 100% rename from pkg/minimum_window_substring/main.go rename to pkg/questions/minimum_window_substring/main.go diff --git a/pkg/pacific_atlantic_water_flow/main.go b/pkg/questions/pacific_atlantic_water_flow/main.go similarity index 100% rename from pkg/pacific_atlantic_water_flow/main.go rename to pkg/questions/pacific_atlantic_water_flow/main.go diff --git a/pkg/pacific_atlantic_water_flow/main_test.go b/pkg/questions/pacific_atlantic_water_flow/main_test.go similarity index 100% rename from pkg/pacific_atlantic_water_flow/main_test.go rename to pkg/questions/pacific_atlantic_water_flow/main_test.go diff --git a/pkg/product_of_array_except_self/main.go b/pkg/questions/product_of_array_except_self/main.go similarity index 100% rename from pkg/product_of_array_except_self/main.go rename to pkg/questions/product_of_array_except_self/main.go diff --git a/pkg/product_of_array_except_self/main_test.go b/pkg/questions/product_of_array_except_self/main_test.go similarity index 100% rename from pkg/product_of_array_except_self/main_test.go rename to pkg/questions/product_of_array_except_self/main_test.go diff --git a/pkg/remove_nth_node_from_end_of_list/main.go b/pkg/questions/remove_nth_node_from_end_of_list/main.go similarity index 100% rename from pkg/remove_nth_node_from_end_of_list/main.go rename to pkg/questions/remove_nth_node_from_end_of_list/main.go diff --git a/pkg/reorder_list/main.go b/pkg/questions/reorder_list/main.go similarity index 100% rename from pkg/reorder_list/main.go rename to pkg/questions/reorder_list/main.go diff --git a/pkg/reverse_linked_list/main.go b/pkg/questions/reverse_linked_list/main.go similarity index 100% rename from pkg/reverse_linked_list/main.go rename to pkg/questions/reverse_linked_list/main.go diff --git a/pkg/search_in_rotated_sorted_array/main.go b/pkg/questions/search_in_rotated_sorted_array/main.go similarity index 100% rename from pkg/search_in_rotated_sorted_array/main.go rename to pkg/questions/search_in_rotated_sorted_array/main.go diff --git a/pkg/three_sum/main.go b/pkg/questions/three_sum/main.go similarity index 100% rename from pkg/three_sum/main.go rename to pkg/questions/three_sum/main.go diff --git a/pkg/three_sum/main_test.go b/pkg/questions/three_sum/main_test.go similarity index 100% rename from pkg/three_sum/main_test.go rename to pkg/questions/three_sum/main_test.go diff --git a/pkg/top_k_frequent_elements/main.go b/pkg/questions/top_k_frequent_elements/main.go similarity index 100% rename from pkg/top_k_frequent_elements/main.go rename to pkg/questions/top_k_frequent_elements/main.go diff --git a/pkg/top_k_frequent_elements/main_test.go b/pkg/questions/top_k_frequent_elements/main_test.go similarity index 100% rename from pkg/top_k_frequent_elements/main_test.go rename to pkg/questions/top_k_frequent_elements/main_test.go diff --git a/pkg/trapping_rain_water/main.go b/pkg/questions/trapping_rain_water/main.go similarity index 100% rename from pkg/trapping_rain_water/main.go rename to pkg/questions/trapping_rain_water/main.go diff --git a/pkg/trapping_rain_water/main_test.go b/pkg/questions/trapping_rain_water/main_test.go similarity index 100% rename from pkg/trapping_rain_water/main_test.go rename to pkg/questions/trapping_rain_water/main_test.go diff --git a/pkg/two_sum/main.go b/pkg/questions/two_sum/main.go similarity index 100% rename from pkg/two_sum/main.go rename to pkg/questions/two_sum/main.go diff --git a/pkg/two_sum/main_test.go b/pkg/questions/two_sum/main_test.go similarity index 100% rename from pkg/two_sum/main_test.go rename to pkg/questions/two_sum/main_test.go diff --git a/pkg/valid_palindrome/main.go b/pkg/questions/valid_palindrome/main.go similarity index 100% rename from pkg/valid_palindrome/main.go rename to pkg/questions/valid_palindrome/main.go diff --git a/pkg/valid_palindrome/main_test.go b/pkg/questions/valid_palindrome/main_test.go similarity index 100% rename from pkg/valid_palindrome/main_test.go rename to pkg/questions/valid_palindrome/main_test.go diff --git a/pkg/valid_sudoku/main.go b/pkg/questions/valid_sudoku/main.go similarity index 100% rename from pkg/valid_sudoku/main.go rename to pkg/questions/valid_sudoku/main.go diff --git a/pkg/valid_sudoku/main_test.go b/pkg/questions/valid_sudoku/main_test.go similarity index 100% rename from pkg/valid_sudoku/main_test.go rename to pkg/questions/valid_sudoku/main_test.go diff --git a/pkg/wildcard_matching/main.go b/pkg/questions/wildcard_matching/main.go similarity index 100% rename from pkg/wildcard_matching/main.go rename to pkg/questions/wildcard_matching/main.go diff --git a/pkg/wildcard_matching/main_test.go b/pkg/questions/wildcard_matching/main_test.go similarity index 100% rename from pkg/wildcard_matching/main_test.go rename to pkg/questions/wildcard_matching/main_test.go diff --git a/pkg/word_search_ii/main.go b/pkg/questions/word_search_ii/main.go similarity index 100% rename from pkg/word_search_ii/main.go rename to pkg/questions/word_search_ii/main.go diff --git a/pkg/tools/list/main.go b/pkg/tools/list/main.go new file mode 100644 index 0000000..9873169 --- /dev/null +++ b/pkg/tools/list/main.go @@ -0,0 +1,57 @@ +package tools + +type Node[T any] struct { + value T + before *Node[T] + after *Node[T] +} + +type List[T any] struct { + front *Node[T] + back *Node[T] + size uint +} + +func New[T any]() List[T] { + return List[T]{nil, nil, 0} +} + +func (l *List[T]) PushFront(t T) { + item := &Node[T]{t, nil, nil} + + if l.back == nil { + l.back = item + } + + if front := l.front; front != nil { + front.before, item.after = item, front + } + + l.size++ + l.front = item +} + +func (l *List[T]) PopBack() (result T, good bool) { + if l.back == nil { + return + } + + result = l.back.value + good = true + + if l.front == l.back { + l.front = nil + } + + if before := l.back.before; before != nil { + before.after = nil + } + + l.size-- + l.back = l.back.before + return +} + +func (l *List[T]) Size() uint { + return l.size +}