diff --git a/coin-change/neverlish.go b/coin-change/neverlish.go new file mode 100644 index 000000000..92ec40a4a --- /dev/null +++ b/coin-change/neverlish.go @@ -0,0 +1,50 @@ +// 시간복잡도: O(n*m) +// 공간복잡도: O(n*m) + +package main + +import "testing" + +func TestCoinChange(t *testing.T) { + result1 := coinChange([]int{1, 2, 5}, 11) + if result1 != 3 { + t.Errorf("Expected 3 but got %d", result1) + } + + result2 := coinChange([]int{2}, 3) + + if result2 != -1 { + t.Errorf("Expected -1 but got %d", result2) + } + + result3 := coinChange([]int{1}, 0) + + if result3 != 0 { + t.Errorf("Expected 0 but got %d", result3) + } +} + +func coinChange(coins []int, amount int) int { + if amount == 0 { + return 0 + } + + dp := make([]int, amount+1) + for i := 1; i <= amount; i++ { + dp[i] = amount + 1 + } + + for i := 1; i <= amount; i++ { + for _, coin := range coins { + if coin <= i { + dp[i] = min(dp[i], dp[i-coin]+1) + } + } + } + + if dp[amount] > amount { + return -1 + } + + return dp[amount] +} diff --git a/merge-two-sorted-lists/neverlish.go b/merge-two-sorted-lists/neverlish.go new file mode 100644 index 000000000..be28ad8d3 --- /dev/null +++ b/merge-two-sorted-lists/neverlish.go @@ -0,0 +1,36 @@ +// 시간복잡도: O(n) +// 공간복잡도: O(1) + +package main + +import "testing" + +type ListNode struct { + Val int + Next *ListNode +} + +func TestMergeTwoLists(t *testing.T) { + result1 := mergeTwoLists(&ListNode{Val: 1, Next: &ListNode{Val: 2, Next: &ListNode{Val: 4}}}, &ListNode{Val: 1, Next: &ListNode{Val: 3, Next: &ListNode{Val: 4}}}) + if result1.Val != 1 || result1.Next.Val != 1 || result1.Next.Next.Val != 2 || result1.Next.Next.Next.Val != 3 || result1.Next.Next.Next.Next.Val != 4 || result1.Next.Next.Next.Next.Next.Val != 4 { + t.Error("Test case 1 failed") + } +} + +func mergeTwoLists(list1 *ListNode, list2 *ListNode) *ListNode { + if list1 == nil { + return list2 + } + + if list2 == nil { + return list1 + } + + if list1.Val < list2.Val { + list1.Next = mergeTwoLists(list1.Next, list2) + return list1 + } else { + list2.Next = mergeTwoLists(list1, list2.Next) + return list2 + } +} diff --git a/missing-number/neverlish.go b/missing-number/neverlish.go new file mode 100644 index 000000000..3ceb7cea8 --- /dev/null +++ b/missing-number/neverlish.go @@ -0,0 +1,26 @@ +// 시간복잡도: O(n) +// 공간복잡도: O(1) + +package main + +import "testing" + +func TestMissingNumber(t *testing.T) { + if missingNumber([]int{3, 0, 1}) != 2 { + t.Error("Test case 0 failed") + } + + if missingNumber([]int{9, 6, 4, 2, 3, 5, 7, 0, 1}) != 8 { + t.Error("Test case 1 failed") + } +} + +func missingNumber(nums []int) int { + sum := 0 + for _, num := range nums { + sum += num + } + + target := len(nums) * (len(nums) + 1) / 2 + return target - sum +} diff --git a/palindromic-substrings/neverlish.go b/palindromic-substrings/neverlish.go new file mode 100644 index 000000000..aafb974f8 --- /dev/null +++ b/palindromic-substrings/neverlish.go @@ -0,0 +1,40 @@ +// 시간복잡도: O(n^3) +// 공간복잡도: O(n^2) + +package main + +import "testing" + +func expect(input string, expected int, t *testing.T) { + result := countSubstrings(input) + if result != expected { + t.Errorf("Expected %d but got %d", expected, result) + } +} + +func TestCountSubstrings(t *testing.T) { + expect("abc", 3, t) + expect("dnncbwoneinoplypwgbwktmvkoimcooyiwirgbxlcttgteqthcvyoueyftiwgwwxvxvg", 77, t) +} + +func isPalindrome(s string) bool { + for i := 0; i < len(s)/2; i++ { + if s[i] != s[len(s)-1-i] { + return false + } + } + return true +} + +func countSubstrings(s string) int { + result := 0 + + for i := 0; i < len(s); i++ { + for j := i; j < len(s); j++ { + if isPalindrome(s[i:j+1]) { + result++ + } + } + } + return result +} diff --git a/word-search/neverlish.go b/word-search/neverlish.go new file mode 100644 index 000000000..38e852406 --- /dev/null +++ b/word-search/neverlish.go @@ -0,0 +1,81 @@ +// 시간복잡도: O(n^2 * 4^m) +// 공간복잡도: O(n * m) +// (n: board의 행의 개수, m: word의 길이) + +package main + +import "testing" + +func TestExist(t *testing.T) { + board := [][]byte{ + {'A', 'B', 'C', 'E'}, + {'S', 'F', 'C', 'S'}, + {'A', 'D', 'E', 'E'}, + } + + if !exist(board, "ABCCED") { + t.Error("Test case 0 failed") + } + + if !exist(board, "SEE") { + t.Error("Test case 1 failed") + } + + if exist(board, "ABCB") { + t.Error("Test case 2 failed") + } +} + +func backtrack(board [][]byte, histories [][]bool, n_row int, n_col int, word string, idx int) bool { + if idx == len(word) { + return true + } + + if n_row < 0 || n_row >= len(board) { + return false + } + + if n_col < 0 || n_col >= len(board[0]) { + return false + } + + if board[n_row][n_col] != word[idx] || histories[n_row][n_col] { + return false + } + + steps := [][]int{{0, 1}, {0, -1}, {1, 0}, {-1, 0}} + + histories[n_row][n_col] = true + + for _, step := range steps { + if backtrack(board, histories, n_row+step[0], n_col+step[1], word, idx+1) { + return true + } + } + + histories[n_row][n_col] = false + return false +} + +func exist(board [][]byte, word string) bool { + n_rows := len(board) + n_cols := len(board[0]) + + histories := make([][]bool, n_rows) + + for n_row := 0; n_row < n_rows; n_row++ { + for n_col := 0; n_col < n_cols; n_col++ { + if board[n_row][n_col] == word[0] { + for k := 0; k < n_rows; k++ { + histories[k] = make([]bool, n_cols) + } + + if backtrack(board, histories, n_row, n_col, word, 0) { + return true + } + } + } + } + + return false +}