Skip to content

Commit df1bbd2

Browse files
committed
week04
1 parent b1e9457 commit df1bbd2

File tree

5 files changed

+233
-0
lines changed

5 files changed

+233
-0
lines changed

coin-change/neverlish.go

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// 시간복잡도: O(n*m)
2+
// 공간복잡도: O(n*m)
3+
4+
package main
5+
6+
import "testing"
7+
8+
func TestCoinChange(t *testing.T) {
9+
result1 := coinChange([]int{1, 2, 5}, 11)
10+
if result1 != 3 {
11+
t.Errorf("Expected 3 but got %d", result1)
12+
}
13+
14+
result2 := coinChange([]int{2}, 3)
15+
16+
if result2 != -1 {
17+
t.Errorf("Expected -1 but got %d", result2)
18+
}
19+
20+
result3 := coinChange([]int{1}, 0)
21+
22+
if result3 != 0 {
23+
t.Errorf("Expected 0 but got %d", result3)
24+
}
25+
}
26+
27+
func coinChange(coins []int, amount int) int {
28+
if amount == 0 {
29+
return 0
30+
}
31+
32+
dp := make([]int, amount+1)
33+
for i := 1; i <= amount; i++ {
34+
dp[i] = amount + 1
35+
}
36+
37+
for i := 1; i <= amount; i++ {
38+
for _, coin := range coins {
39+
if coin <= i {
40+
dp[i] = min(dp[i], dp[i-coin]+1)
41+
}
42+
}
43+
}
44+
45+
if dp[amount] > amount {
46+
return -1
47+
}
48+
49+
return dp[amount]
50+
}

merge-two-sorted-lists/neverlish.go

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// 시간복잡도: O(n)
2+
// 공간복잡도: O(1)
3+
4+
package main
5+
6+
import "testing"
7+
8+
type ListNode struct {
9+
Val int
10+
Next *ListNode
11+
}
12+
13+
func TestMergeTwoLists(t *testing.T) {
14+
result1 := mergeTwoLists(&ListNode{Val: 1, Next: &ListNode{Val: 2, Next: &ListNode{Val: 4}}}, &ListNode{Val: 1, Next: &ListNode{Val: 3, Next: &ListNode{Val: 4}}})
15+
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 {
16+
t.Error("Test case 1 failed")
17+
}
18+
}
19+
20+
func mergeTwoLists(list1 *ListNode, list2 *ListNode) *ListNode {
21+
if list1 == nil {
22+
return list2
23+
}
24+
25+
if list2 == nil {
26+
return list1
27+
}
28+
29+
if list1.Val < list2.Val {
30+
list1.Next = mergeTwoLists(list1.Next, list2)
31+
return list1
32+
} else {
33+
list2.Next = mergeTwoLists(list1, list2.Next)
34+
return list2
35+
}
36+
}

missing-number/neverlish.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// 시간복잡도: O(n)
2+
// 공간복잡도: O(1)
3+
4+
package main
5+
6+
import "testing"
7+
8+
func TestMissingNumber(t *testing.T) {
9+
if missingNumber([]int{3, 0, 1}) != 2 {
10+
t.Error("Test case 0 failed")
11+
}
12+
13+
if missingNumber([]int{9, 6, 4, 2, 3, 5, 7, 0, 1}) != 8 {
14+
t.Error("Test case 1 failed")
15+
}
16+
}
17+
18+
func missingNumber(nums []int) int {
19+
sum := 0
20+
for _, num := range nums {
21+
sum += num
22+
}
23+
24+
target := len(nums) * (len(nums) + 1) / 2
25+
return target - sum
26+
}

palindromic-substrings/neverlish.go

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// 시간복잡도: O(n^3)
2+
// 공간복잡도: O(n^2)
3+
4+
package main
5+
6+
import "testing"
7+
8+
func expect(input string, expected int, t *testing.T) {
9+
result := countSubstrings(input)
10+
if result != expected {
11+
t.Errorf("Expected %d but got %d", expected, result)
12+
}
13+
}
14+
15+
func TestCountSubstrings(t *testing.T) {
16+
expect("abc", 3, t)
17+
expect("dnncbwoneinoplypwgbwktmvkoimcooyiwirgbxlcttgteqthcvyoueyftiwgwwxvxvg", 77, t)
18+
}
19+
20+
func isPalindrome(s string) bool {
21+
for i := 0; i < len(s)/2; i++ {
22+
if s[i] != s[len(s)-1-i] {
23+
return false
24+
}
25+
}
26+
return true
27+
}
28+
29+
func countSubstrings(s string) int {
30+
result := 0
31+
32+
for i := 0; i < len(s); i++ {
33+
for j := i; j < len(s); j++ {
34+
if isPalindrome(s[i:j+1]) {
35+
result++
36+
}
37+
}
38+
}
39+
return result
40+
}

word-search/neverlish.go

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// 시간복잡도: O(n^2 * 4^m)
2+
// 공간복잡도: O(n * m)
3+
// (n: board의 행의 개수, m: word의 길이)
4+
5+
package main
6+
7+
import "testing"
8+
9+
func TestExist(t *testing.T) {
10+
board := [][]byte{
11+
{'A', 'B', 'C', 'E'},
12+
{'S', 'F', 'C', 'S'},
13+
{'A', 'D', 'E', 'E'},
14+
}
15+
16+
if !exist(board, "ABCCED") {
17+
t.Error("Test case 0 failed")
18+
}
19+
20+
if !exist(board, "SEE") {
21+
t.Error("Test case 1 failed")
22+
}
23+
24+
if exist(board, "ABCB") {
25+
t.Error("Test case 2 failed")
26+
}
27+
}
28+
29+
func backtrack(board [][]byte, histories [][]bool, n_row int, n_col int, word string, idx int) bool {
30+
if idx == len(word) {
31+
return true
32+
}
33+
34+
if n_row < 0 || n_row >= len(board) {
35+
return false
36+
}
37+
38+
if n_col < 0 || n_col >= len(board[0]) {
39+
return false
40+
}
41+
42+
if board[n_row][n_col] != word[idx] || histories[n_row][n_col] {
43+
return false
44+
}
45+
46+
steps := [][]int{{0, 1}, {0, -1}, {1, 0}, {-1, 0}}
47+
48+
histories[n_row][n_col] = true
49+
50+
for _, step := range steps {
51+
if backtrack(board, histories, n_row+step[0], n_col+step[1], word, idx+1) {
52+
return true
53+
}
54+
}
55+
56+
histories[n_row][n_col] = false
57+
return false
58+
}
59+
60+
func exist(board [][]byte, word string) bool {
61+
n_rows := len(board)
62+
n_cols := len(board[0])
63+
64+
histories := make([][]bool, n_rows)
65+
66+
for n_row := 0; n_row < n_rows; n_row++ {
67+
for n_col := 0; n_col < n_cols; n_col++ {
68+
if board[n_row][n_col] == word[0] {
69+
for k := 0; k < n_rows; k++ {
70+
histories[k] = make([]bool, n_cols)
71+
}
72+
73+
if backtrack(board, histories, n_row, n_col, word, 0) {
74+
return true
75+
}
76+
}
77+
}
78+
}
79+
80+
return false
81+
}

0 commit comments

Comments
 (0)