Skip to content

Commit a851064

Browse files
committed
week 8
1 parent 2496829 commit a851064

File tree

5 files changed

+262
-0
lines changed

5 files changed

+262
-0
lines changed

clone-graph/neverlish.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package main
2+
3+
import "testing"
4+
5+
func Test_cloneGraph(t *testing.T) {
6+
result1 := cloneGraph(&Node{
7+
Val: 1,
8+
Neighbors: []*Node{
9+
{
10+
Val: 2,
11+
Neighbors: []*Node{
12+
{
13+
Val: 4,
14+
Neighbors: []*Node{
15+
{
16+
Val: 3,
17+
Neighbors: []*Node{
18+
{
19+
Val: 1,
20+
},
21+
{
22+
Val: 4,
23+
},
24+
},
25+
},
26+
{
27+
Val: 1,
28+
Neighbors: []*Node{
29+
{
30+
Val: 3,
31+
},
32+
{
33+
Val: 2,
34+
},
35+
},
36+
},
37+
},
38+
},
39+
{
40+
Val: 1,
41+
Neighbors: []*Node{
42+
{
43+
Val: 4,
44+
},
45+
{
46+
Val: 2,
47+
},
48+
},
49+
},
50+
},
51+
},
52+
{
53+
Val: 3,
54+
Neighbors: []*Node{
55+
{
56+
Val: 4,
57+
},
58+
{
59+
Val: 1,
60+
},
61+
},
62+
},
63+
},
64+
})
65+
66+
if result1.Val != 1 {
67+
t.Fatal(result1.Val)
68+
}
69+
}
70+
71+
type Node struct {
72+
Val int
73+
Neighbors []*Node
74+
}
75+
76+
func dfs(node *Node, visited map[*Node]*Node) *Node {
77+
if node == nil {
78+
return nil
79+
}
80+
81+
if _, ok := visited[node]; ok {
82+
return visited[node]
83+
}
84+
85+
cloneNode := &Node{Val: node.Val}
86+
87+
visited[node] = cloneNode
88+
89+
for _, neighbor := range node.Neighbors {
90+
cloneNode.Neighbors = append(cloneNode.Neighbors, dfs(neighbor, visited))
91+
}
92+
93+
return cloneNode
94+
}
95+
96+
func cloneGraph(node *Node) *Node {
97+
if node == nil {
98+
return nil
99+
}
100+
101+
visited := make(map[*Node]*Node)
102+
103+
return dfs(node, visited)
104+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// 시간복잡도: O(n^2)
2+
// 공간복잡도: O(n^2)
3+
4+
package main
5+
6+
import "testing"
7+
8+
func Test_longestCommonSubsequence(t *testing.T) {
9+
result1 := longestCommonSubsequence("abcde", "ace")
10+
11+
if result1 != 3 {
12+
t.Fatal(result1)
13+
}
14+
15+
result2 := longestCommonSubsequence("abc", "abc")
16+
17+
if result2 != 3 {
18+
t.Fatal(result2)
19+
}
20+
21+
result3 := longestCommonSubsequence("abc", "def")
22+
23+
if result3 != 0 {
24+
t.Fatal(result3)
25+
}
26+
}
27+
28+
func longestCommonSubsequence(text1 string, text2 string) int {
29+
commonSubsequence := make([][]int, len(text1)+1)
30+
31+
for i := 0; i < len(text1)+1; i++ {
32+
commonSubsequence[i] = make([]int, len(text2)+1)
33+
}
34+
35+
for i := 1; i < len(text1)+1; i++ {
36+
for j := 1; j < len(text2)+1; j++ {
37+
if text1[i-1] == text2[j-1] {
38+
commonSubsequence[i][j] = commonSubsequence[i-1][j-1] + 1
39+
} else {
40+
commonSubsequence[i][j] = max(commonSubsequence[i-1][j], commonSubsequence[i][j-1])
41+
}
42+
}
43+
}
44+
45+
return commonSubsequence[len(text1)][len(text2)]
46+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// 시간복잡도: O(n)
2+
// 공간복잡도: O(1)
3+
4+
package main
5+
6+
import "testing"
7+
8+
func Test_characterReplacement(t *testing.T) {
9+
result1 := characterReplacement("ABAB", 2)
10+
if result1 != 4 {
11+
t.Fatal(result1)
12+
}
13+
14+
result2 := characterReplacement("AABABBA", 1)
15+
if result2 != 4 {
16+
t.Fatal(result2)
17+
}
18+
}
19+
20+
func maxValue(m map[byte]int) int {
21+
max := 0
22+
for _, v := range m {
23+
if v > max {
24+
max = v
25+
}
26+
}
27+
return max
28+
}
29+
30+
func characterReplacement(s string, k int) int {
31+
max_length := 0
32+
counter := make(map[byte]int)
33+
34+
window_start := 0
35+
window_end := 0
36+
37+
for window_end < len(s) {
38+
counter[s[window_end]]++
39+
40+
for window_end-window_start+1-maxValue(counter) > k {
41+
counter[s[window_start]]--
42+
window_start++
43+
}
44+
45+
max_length = max(max_length, window_end-window_start+1)
46+
47+
window_end++
48+
}
49+
50+
return max_length
51+
}

number-of-1-bits/neverlish.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// 시간복잡도: O(1)
2+
// 공간복잡도: O(1)
3+
4+
package main
5+
6+
import "testing"
7+
8+
func Test_hammingWeight(t *testing.T) {
9+
result1 := hammingWeight(11)
10+
if result1 != 3 {
11+
t.Fatal(result1)
12+
}
13+
14+
result2 := hammingWeight(128)
15+
16+
if result2 != 1 {
17+
t.Fatal(result2)
18+
}
19+
20+
result3 := hammingWeight(2147483645)
21+
22+
if result3 != 30 {
23+
t.Fatal(result3)
24+
}
25+
}
26+
27+
func hammingWeight(n int) int {
28+
result := 0
29+
for n > 0 {
30+
result += n & 1
31+
n >>= 1
32+
}
33+
return result
34+
}

sum-of-two-integers/neverlish.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// 시간복잡도: O(1)
2+
// 공간복잡도: O(1)
3+
4+
package main
5+
6+
import "testing"
7+
8+
func Test_getSum(t *testing.T) {
9+
result1 := getSum(1, 2)
10+
if result1 != 3 {
11+
t.Fatal(result1)
12+
}
13+
14+
result2 := getSum(2, 3)
15+
if result2 != 5 {
16+
t.Fatal(result2)
17+
}
18+
}
19+
20+
func getSum(a int, b int) int {
21+
for b != 0 {
22+
carry := a & b
23+
a = a ^ b
24+
b = carry << 1
25+
}
26+
return a
27+
}

0 commit comments

Comments
 (0)