From a851064daa55c9bfdad1ae5ff43bfb8334c724f6 Mon Sep 17 00:00:00 2001 From: Jinho Hyeon Date: Tue, 28 Jan 2025 10:48:50 +0900 Subject: [PATCH 1/2] week 8 --- clone-graph/neverlish.go | 104 ++++++++++++++++++ longest-common-subsequence/neverlish.go | 46 ++++++++ .../neverlish.go | 51 +++++++++ number-of-1-bits/neverlish.go | 34 ++++++ sum-of-two-integers/neverlish.go | 27 +++++ 5 files changed, 262 insertions(+) create mode 100644 clone-graph/neverlish.go create mode 100644 longest-common-subsequence/neverlish.go create mode 100644 longest-repeating-character-replacement/neverlish.go create mode 100644 number-of-1-bits/neverlish.go create mode 100644 sum-of-two-integers/neverlish.go diff --git a/clone-graph/neverlish.go b/clone-graph/neverlish.go new file mode 100644 index 000000000..610eb9104 --- /dev/null +++ b/clone-graph/neverlish.go @@ -0,0 +1,104 @@ +package main + +import "testing" + +func Test_cloneGraph(t *testing.T) { + result1 := cloneGraph(&Node{ + Val: 1, + Neighbors: []*Node{ + { + Val: 2, + Neighbors: []*Node{ + { + Val: 4, + Neighbors: []*Node{ + { + Val: 3, + Neighbors: []*Node{ + { + Val: 1, + }, + { + Val: 4, + }, + }, + }, + { + Val: 1, + Neighbors: []*Node{ + { + Val: 3, + }, + { + Val: 2, + }, + }, + }, + }, + }, + { + Val: 1, + Neighbors: []*Node{ + { + Val: 4, + }, + { + Val: 2, + }, + }, + }, + }, + }, + { + Val: 3, + Neighbors: []*Node{ + { + Val: 4, + }, + { + Val: 1, + }, + }, + }, + }, + }) + + if result1.Val != 1 { + t.Fatal(result1.Val) + } +} + +type Node struct { + Val int + Neighbors []*Node +} + +func dfs(node *Node, visited map[*Node]*Node) *Node { + if node == nil { + return nil + } + + if _, ok := visited[node]; ok { + return visited[node] + } + + cloneNode := &Node{Val: node.Val} + + visited[node] = cloneNode + + for _, neighbor := range node.Neighbors { + cloneNode.Neighbors = append(cloneNode.Neighbors, dfs(neighbor, visited)) + } + + return cloneNode +} + +func cloneGraph(node *Node) *Node { + if node == nil { + return nil + } + + visited := make(map[*Node]*Node) + + return dfs(node, visited) +} diff --git a/longest-common-subsequence/neverlish.go b/longest-common-subsequence/neverlish.go new file mode 100644 index 000000000..62c6e368a --- /dev/null +++ b/longest-common-subsequence/neverlish.go @@ -0,0 +1,46 @@ +// 시간복잡도: O(n^2) +// 공간복잡도: O(n^2) + +package main + +import "testing" + +func Test_longestCommonSubsequence(t *testing.T) { + result1 := longestCommonSubsequence("abcde", "ace") + + if result1 != 3 { + t.Fatal(result1) + } + + result2 := longestCommonSubsequence("abc", "abc") + + if result2 != 3 { + t.Fatal(result2) + } + + result3 := longestCommonSubsequence("abc", "def") + + if result3 != 0 { + t.Fatal(result3) + } +} + +func longestCommonSubsequence(text1 string, text2 string) int { + commonSubsequence := make([][]int, len(text1)+1) + + for i := 0; i < len(text1)+1; i++ { + commonSubsequence[i] = make([]int, len(text2)+1) + } + + for i := 1; i < len(text1)+1; i++ { + for j := 1; j < len(text2)+1; j++ { + if text1[i-1] == text2[j-1] { + commonSubsequence[i][j] = commonSubsequence[i-1][j-1] + 1 + } else { + commonSubsequence[i][j] = max(commonSubsequence[i-1][j], commonSubsequence[i][j-1]) + } + } + } + + return commonSubsequence[len(text1)][len(text2)] +} diff --git a/longest-repeating-character-replacement/neverlish.go b/longest-repeating-character-replacement/neverlish.go new file mode 100644 index 000000000..eb205cf68 --- /dev/null +++ b/longest-repeating-character-replacement/neverlish.go @@ -0,0 +1,51 @@ +// 시간복잡도: O(n) +// 공간복잡도: O(1) + +package main + +import "testing" + +func Test_characterReplacement(t *testing.T) { + result1 := characterReplacement("ABAB", 2) + if result1 != 4 { + t.Fatal(result1) + } + + result2 := characterReplacement("AABABBA", 1) + if result2 != 4 { + t.Fatal(result2) + } +} + +func maxValue(m map[byte]int) int { + max := 0 + for _, v := range m { + if v > max { + max = v + } + } + return max +} + +func characterReplacement(s string, k int) int { + max_length := 0 + counter := make(map[byte]int) + + window_start := 0 + window_end := 0 + + for window_end < len(s) { + counter[s[window_end]]++ + + for window_end-window_start+1-maxValue(counter) > k { + counter[s[window_start]]-- + window_start++ + } + + max_length = max(max_length, window_end-window_start+1) + + window_end++ + } + + return max_length +} diff --git a/number-of-1-bits/neverlish.go b/number-of-1-bits/neverlish.go new file mode 100644 index 000000000..cb2b2f264 --- /dev/null +++ b/number-of-1-bits/neverlish.go @@ -0,0 +1,34 @@ +// 시간복잡도: O(1) +// 공간복잡도: O(1) + +package main + +import "testing" + +func Test_hammingWeight(t *testing.T) { + result1 := hammingWeight(11) + if result1 != 3 { + t.Fatal(result1) + } + + result2 := hammingWeight(128) + + if result2 != 1 { + t.Fatal(result2) + } + + result3 := hammingWeight(2147483645) + + if result3 != 30 { + t.Fatal(result3) + } +} + +func hammingWeight(n int) int { + result := 0 + for n > 0 { + result += n & 1 + n >>= 1 + } + return result +} diff --git a/sum-of-two-integers/neverlish.go b/sum-of-two-integers/neverlish.go new file mode 100644 index 000000000..04719bdf6 --- /dev/null +++ b/sum-of-two-integers/neverlish.go @@ -0,0 +1,27 @@ +// 시간복잡도: O(1) +// 공간복잡도: O(1) + +package main + +import "testing" + +func Test_getSum(t *testing.T) { + result1 := getSum(1, 2) + if result1 != 3 { + t.Fatal(result1) + } + + result2 := getSum(2, 3) + if result2 != 5 { + t.Fatal(result2) + } +} + +func getSum(a int, b int) int { + for b != 0 { + carry := a & b + a = a ^ b + b = carry << 1 + } + return a +} From a93a73cf4328da90954ad94668ba594eab7de373 Mon Sep 17 00:00:00 2001 From: Jinho Hyeon Date: Fri, 31 Jan 2025 22:53:32 +0900 Subject: [PATCH 2/2] applied feedback --- clone-graph/neverlish.go | 4 ++++ longest-common-subsequence/neverlish.go | 18 ++++++++---------- sum-of-two-integers/neverlish.go | 2 +- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/clone-graph/neverlish.go b/clone-graph/neverlish.go index 610eb9104..bd8f5fe91 100644 --- a/clone-graph/neverlish.go +++ b/clone-graph/neverlish.go @@ -1,3 +1,7 @@ +// A: node 의 갯수, B: neighbors의 길이 +// 시간복잡도: O(A + B) +// 공간복잡도: O(A + B) + package main import "testing" diff --git a/longest-common-subsequence/neverlish.go b/longest-common-subsequence/neverlish.go index 62c6e368a..6be532f1d 100644 --- a/longest-common-subsequence/neverlish.go +++ b/longest-common-subsequence/neverlish.go @@ -26,21 +26,19 @@ func Test_longestCommonSubsequence(t *testing.T) { } func longestCommonSubsequence(text1 string, text2 string) int { - commonSubsequence := make([][]int, len(text1)+1) + prev := make([]int, len(text2)+1) + curr := make([]int, len(text2)+1) - for i := 0; i < len(text1)+1; i++ { - commonSubsequence[i] = make([]int, len(text2)+1) - } - - for i := 1; i < len(text1)+1; i++ { - for j := 1; j < len(text2)+1; j++ { + for i := 1; i <= len(text1); i++ { + for j := 1; j <= len(text2); j++ { if text1[i-1] == text2[j-1] { - commonSubsequence[i][j] = commonSubsequence[i-1][j-1] + 1 + curr[j] = prev[j-1] + 1 } else { - commonSubsequence[i][j] = max(commonSubsequence[i-1][j], commonSubsequence[i][j-1]) + curr[j] = max(prev[j], curr[j-1]) } } + prev, curr = curr, prev } - return commonSubsequence[len(text1)][len(text2)] + return prev[len(text2)] } diff --git a/sum-of-two-integers/neverlish.go b/sum-of-two-integers/neverlish.go index 04719bdf6..6c2ac7d45 100644 --- a/sum-of-two-integers/neverlish.go +++ b/sum-of-two-integers/neverlish.go @@ -1,4 +1,4 @@ -// 시간복잡도: O(1) +// 시간복잡도: O(log n) // 공간복잡도: O(1) package main