Skip to content

Commit 7a06cf6

Browse files
committed
neverlish: week02
1 parent 1341491 commit 7a06cf6

File tree

5 files changed

+277
-0
lines changed

5 files changed

+277
-0
lines changed

3sum/neverlish.go

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// 시간복잡도: O(n^2)
2+
// 공간복잡도: O(n)
3+
4+
package main
5+
6+
import (
7+
"sort"
8+
"testing"
9+
)
10+
11+
func TestThreeSum(t *testing.T) {
12+
test1 := []int{-1, 0, 1, 2, -1, -4}
13+
result1 := threeSum(test1)
14+
15+
for _, comp := range result1 {
16+
for _, num := range comp {
17+
t.Logf("%d ", num)
18+
}
19+
}
20+
21+
println("YO")
22+
23+
if len(result1) != 2 {
24+
t.Errorf("Expected 2, got %d", len(result1))
25+
}
26+
}
27+
28+
func twoSum(nums []int, target int) [][]int {
29+
result := [][]int{}
30+
seen := map[int]int{}
31+
for i, num := range nums {
32+
complement := target - num
33+
if _, ok := seen[complement]; ok {
34+
result = append(result, []int{seen[complement], i})
35+
}
36+
seen[num] = i
37+
}
38+
39+
return result
40+
}
41+
42+
func threeSum(nums []int) [][]int {
43+
result := map[int]map[int]map[int]bool{}
44+
45+
for i, num := range nums[:len(nums)-2] {
46+
if (i > 0 && num == nums[i-1]) {
47+
continue
48+
}
49+
for _, comp := range twoSum(nums[i+1:], 0-num) {
50+
comps := []int{num, nums[comp[0]+i+1], nums[comp[1]+i+1]}
51+
sort.Ints(comps)
52+
comp1 := comps[0]
53+
comp2 := comps[1]
54+
comp3 := comps[2]
55+
if _, ok := result[comp1]; !ok {
56+
result[comp1] = map[int]map[int]bool{}
57+
}
58+
if _, ok := result[comp1][comp2]; !ok {
59+
result[comp1][comp2] = map[int]bool{}
60+
}
61+
result[comp1][comp2][comp3] = true
62+
}
63+
}
64+
65+
66+
67+
answers := [][]int{}
68+
for key1 := range result {
69+
70+
for key2 := range result[key1] {
71+
for key3 := range result[key1][key2] {
72+
comp := []int{key1, key2, key3}
73+
answers = append(answers, comp)
74+
}
75+
}
76+
}
77+
78+
return answers
79+
}

climbing-stairs/neverlish.go

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// 시간복잡도: O(n)
2+
// 공간복잡도: O(n)
3+
4+
package main
5+
6+
import (
7+
"testing"
8+
)
9+
10+
func Test(t *testing.T) {
11+
result1 := climbStairs(2)
12+
13+
if result1 != 2 {
14+
t.Fatal("failed test1")
15+
}
16+
17+
result2 := climbStairs(3)
18+
19+
if result2 != 3 {
20+
t.Fatal("failed test2")
21+
}
22+
}
23+
24+
func climbStairs(n int) int {
25+
26+
if n == 1 {
27+
return 1
28+
}
29+
30+
if n == 2 {
31+
return 2
32+
}
33+
dp := make([]int, n+1)
34+
35+
dp[1] = 1
36+
dp[2] = 2
37+
38+
for i := 3; i <= n; i++ {
39+
dp[i] = dp[i-1] + dp[i-2]
40+
}
41+
42+
return dp[n]
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package main
2+
3+
import "testing"
4+
5+
func TestBuildTree(t *testing.T) {
6+
preorder := []int{3,9,20,15,7}
7+
inorder := []int{9,3,15,20,7}
8+
9+
result := buildTree(preorder, inorder)
10+
println(result.Val)
11+
println(result.Left.Val)
12+
println(result.Right.Val)
13+
println(result.Right.Left.Val)
14+
println(result.Right.Right.Val)
15+
}
16+
17+
type TreeNode struct {
18+
Val int
19+
Left *TreeNode
20+
Right *TreeNode
21+
}
22+
23+
func findIndex(target int, values []int) int {
24+
for i, v := range values {
25+
if v == target {
26+
return i
27+
}
28+
}
29+
return -1
30+
}
31+
32+
func buildTree(preorder []int, inorder []int) *TreeNode {
33+
if len(inorder) == 0 {
34+
return nil
35+
}
36+
Val := preorder[0]
37+
38+
midIndex := findIndex(Val, inorder)
39+
40+
Left := buildTree(preorder[1:midIndex+1], inorder[:midIndex])
41+
Right := buildTree(preorder[midIndex+1:], inorder[midIndex+1:])
42+
43+
return &TreeNode{
44+
Val,
45+
Left,
46+
Right,
47+
}
48+
}

decode-ways/neverlish.go

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// 시간복잡도: O(n)
2+
// 공간복잡도: O(n)
3+
4+
package main
5+
6+
import (
7+
"testing"
8+
)
9+
10+
func TestNumDecodings(t *testing.T) {
11+
test1 := "12"
12+
result1 := numDecodings(test1)
13+
14+
if result1 != 2 {
15+
t.Errorf("Expected 2, got %d", result1)
16+
}
17+
18+
test2 := "226"
19+
result2 := numDecodings(test2)
20+
21+
if result2 != 3 {
22+
t.Errorf("Expected 3, got %d", result2)
23+
}
24+
25+
test3 := "0"
26+
result3 := numDecodings(test3)
27+
28+
if result3 != 0 {
29+
t.Errorf("Expected 0, got %d", result3)
30+
}
31+
32+
test4 := "06"
33+
result4 := numDecodings(test4)
34+
35+
if result4 != 0 {
36+
t.Errorf("Expected 0, got %d", result4)
37+
}
38+
}
39+
40+
func numDecodings(s string) int {
41+
dp := make([]int, len(s)+1)
42+
43+
dp[0] = 1
44+
45+
for i := 1; i <= len(s); i++ {
46+
curBefore1 := s[i-1]
47+
if curBefore1 != '0' {
48+
dp[i] += dp[i-1]
49+
}
50+
if i > 1 {
51+
curBefore2 := s[i-2]
52+
if curBefore2 != '0' && (curBefore2-'0')*10+(curBefore1-'0') <= 26 {
53+
dp[i] += dp[i-2]
54+
}
55+
}
56+
}
57+
58+
return dp[len(s)]
59+
}

valid-anagram/neverlish.go

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// 시간복잡도: O(n)
2+
// 공간복잡도: O(n)
3+
4+
package main
5+
6+
import (
7+
"testing"
8+
)
9+
10+
func Test(t *testing.T) {
11+
result1 := isAnagram("anagram", "nagaram")
12+
13+
if !result1 {
14+
t.Fatal("failed test1")
15+
}
16+
17+
result2 := isAnagram("rat", "car")
18+
19+
if result2 {
20+
t.Fatal("failed test2")
21+
}
22+
}
23+
24+
func isAnagram(s string, t string) bool {
25+
if len(s) != len(t) {
26+
return false
27+
}
28+
29+
sMap := make(map[rune]int)
30+
31+
for _, c := range s {
32+
sMap[c]++
33+
}
34+
35+
for _, c := range t {
36+
if _, ok := sMap[c]; !ok {
37+
return false
38+
}
39+
40+
sMap[c]--
41+
42+
if sMap[c] == 0 {
43+
delete(sMap, c)
44+
}
45+
}
46+
47+
return len(sMap) == 0
48+
}

0 commit comments

Comments
 (0)