File tree Expand file tree Collapse file tree 5 files changed +264
-0
lines changed
longest-common-subsequence
longest-repeating-character-replacement Expand file tree Collapse file tree 5 files changed +264
-0
lines changed Original file line number Diff line number Diff line change 1+ // A: node 의 갯수, B: neighbors의 길이
2+ // 시간복잡도: O(A + B)
3+ // 공간복잡도: O(A + B)
4+
5+ package main
6+
7+ import "testing"
8+
9+ func Test_cloneGraph (t * testing.T ) {
10+ result1 := cloneGraph (& Node {
11+ Val : 1 ,
12+ Neighbors : []* Node {
13+ {
14+ Val : 2 ,
15+ Neighbors : []* Node {
16+ {
17+ Val : 4 ,
18+ Neighbors : []* Node {
19+ {
20+ Val : 3 ,
21+ Neighbors : []* Node {
22+ {
23+ Val : 1 ,
24+ },
25+ {
26+ Val : 4 ,
27+ },
28+ },
29+ },
30+ {
31+ Val : 1 ,
32+ Neighbors : []* Node {
33+ {
34+ Val : 3 ,
35+ },
36+ {
37+ Val : 2 ,
38+ },
39+ },
40+ },
41+ },
42+ },
43+ {
44+ Val : 1 ,
45+ Neighbors : []* Node {
46+ {
47+ Val : 4 ,
48+ },
49+ {
50+ Val : 2 ,
51+ },
52+ },
53+ },
54+ },
55+ },
56+ {
57+ Val : 3 ,
58+ Neighbors : []* Node {
59+ {
60+ Val : 4 ,
61+ },
62+ {
63+ Val : 1 ,
64+ },
65+ },
66+ },
67+ },
68+ })
69+
70+ if result1 .Val != 1 {
71+ t .Fatal (result1 .Val )
72+ }
73+ }
74+
75+ type Node struct {
76+ Val int
77+ Neighbors []* Node
78+ }
79+
80+ func dfs (node * Node , visited map [* Node ]* Node ) * Node {
81+ if node == nil {
82+ return nil
83+ }
84+
85+ if _ , ok := visited [node ]; ok {
86+ return visited [node ]
87+ }
88+
89+ cloneNode := & Node {Val : node .Val }
90+
91+ visited [node ] = cloneNode
92+
93+ for _ , neighbor := range node .Neighbors {
94+ cloneNode .Neighbors = append (cloneNode .Neighbors , dfs (neighbor , visited ))
95+ }
96+
97+ return cloneNode
98+ }
99+
100+ func cloneGraph (node * Node ) * Node {
101+ if node == nil {
102+ return nil
103+ }
104+
105+ visited := make (map [* Node ]* Node )
106+
107+ return dfs (node , visited )
108+ }
Original file line number Diff line number Diff line change 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+ prev := make ([]int , len (text2 )+ 1 )
30+ curr := make ([]int , len (text2 )+ 1 )
31+
32+ for i := 1 ; i <= len (text1 ); i ++ {
33+ for j := 1 ; j <= len (text2 ); j ++ {
34+ if text1 [i - 1 ] == text2 [j - 1 ] {
35+ curr [j ] = prev [j - 1 ] + 1
36+ } else {
37+ curr [j ] = max (prev [j ], curr [j - 1 ])
38+ }
39+ }
40+ prev , curr = curr , prev
41+ }
42+
43+ return prev [len (text2 )]
44+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1+ // 시간복잡도: O(log n)
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+ }
You can’t perform that action at this time.
0 commit comments