Skip to content

Commit

Permalink
week04
Browse files Browse the repository at this point in the history
  • Loading branch information
neverlish committed Jan 5, 2025
1 parent b1e9457 commit df1bbd2
Show file tree
Hide file tree
Showing 5 changed files with 233 additions and 0 deletions.
50 changes: 50 additions & 0 deletions coin-change/neverlish.go
Original file line number Diff line number Diff line change
@@ -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]
}
36 changes: 36 additions & 0 deletions merge-two-sorted-lists/neverlish.go
Original file line number Diff line number Diff line change
@@ -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
}
}
26 changes: 26 additions & 0 deletions missing-number/neverlish.go
Original file line number Diff line number Diff line change
@@ -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
}
40 changes: 40 additions & 0 deletions palindromic-substrings/neverlish.go
Original file line number Diff line number Diff line change
@@ -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
}
81 changes: 81 additions & 0 deletions word-search/neverlish.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit df1bbd2

Please sign in to comment.