Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[jinho] Week 2 #763

Merged
merged 1 commit into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions 3sum/neverlish.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// 시간복잡도: O(n^2)
// 공간복잡도: O(n)

package main

import (
"sort"
"testing"
)

func TestThreeSum(t *testing.T) {
test1 := []int{-1, 0, 1, 2, -1, -4}
result1 := threeSum(test1)

for _, comp := range result1 {
for _, num := range comp {
t.Logf("%d ", num)
}
}

println("YO")

if len(result1) != 2 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert를 사용하는 대신에 이런식으로 테스트를 작성해도 깔끔하군요..!

t.Errorf("Expected 2, got %d", len(result1))
}
}

func twoSum(nums []int, target int) [][]int {
result := [][]int{}
seen := map[int]int{}
for i, num := range nums {
complement := target - num
if _, ok := seen[complement]; ok {
result = append(result, []int{seen[complement], i})
}
seen[num] = i
}

return result
}

func threeSum(nums []int) [][]int {
result := map[int]map[int]map[int]bool{}

for i, num := range nums[:len(nums)-2] {
if (i > 0 && num == nums[i-1]) {
continue
}
for _, comp := range twoSum(nums[i+1:], 0-num) {
comps := []int{num, nums[comp[0]+i+1], nums[comp[1]+i+1]}
sort.Ints(comps)
comp1 := comps[0]
comp2 := comps[1]
comp3 := comps[2]
if _, ok := result[comp1]; !ok {
result[comp1] = map[int]map[int]bool{}
}
if _, ok := result[comp1][comp2]; !ok {
result[comp1][comp2] = map[int]bool{}
}
result[comp1][comp2][comp3] = true
}
}



answers := [][]int{}
for key1 := range result {

for key2 := range result[key1] {
for key3 := range result[key1][key2] {
comp := []int{key1, key2, key3}
answers = append(answers, comp)
}
}
}

return answers
}
43 changes: 43 additions & 0 deletions climbing-stairs/neverlish.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// 시간복잡도: O(n)
// 공간복잡도: O(n)

package main

import (
"testing"
)

func Test(t *testing.T) {
result1 := climbStairs(2)

if result1 != 2 {
t.Fatal("failed test1")
}

result2 := climbStairs(3)

if result2 != 3 {
t.Fatal("failed test2")
}
}

func climbStairs(n int) int {

if n == 1 {
return 1
}

if n == 2 {
return 2
}
dp := make([]int, n+1)

dp[1] = 1
dp[2] = 2

for i := 3; i <= n; i++ {
dp[i] = dp[i-1] + dp[i-2]
}

return dp[n]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import "testing"

func TestBuildTree(t *testing.T) {
preorder := []int{3,9,20,15,7}
inorder := []int{9,3,15,20,7}

result := buildTree(preorder, inorder)
println(result.Val)
println(result.Left.Val)
println(result.Right.Val)
println(result.Right.Left.Val)
println(result.Right.Right.Val)
}

type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}

func findIndex(target int, values []int) int {
for i, v := range values {
if v == target {
return i
}
}
return -1
}

func buildTree(preorder []int, inorder []int) *TreeNode {
if len(inorder) == 0 {
return nil
}
Val := preorder[0]

midIndex := findIndex(Val, inorder)

Left := buildTree(preorder[1:midIndex+1], inorder[:midIndex])
Right := buildTree(preorder[midIndex+1:], inorder[midIndex+1:])

return &TreeNode{
Val,
Left,
Right,
}
}
59 changes: 59 additions & 0 deletions decode-ways/neverlish.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// 시간복잡도: O(n)
// 공간복잡도: O(n)

package main

import (
"testing"
)

func TestNumDecodings(t *testing.T) {
test1 := "12"
result1 := numDecodings(test1)

if result1 != 2 {
t.Errorf("Expected 2, got %d", result1)
}

test2 := "226"
result2 := numDecodings(test2)

if result2 != 3 {
t.Errorf("Expected 3, got %d", result2)
}

test3 := "0"
result3 := numDecodings(test3)

if result3 != 0 {
t.Errorf("Expected 0, got %d", result3)
}

test4 := "06"
result4 := numDecodings(test4)

if result4 != 0 {
t.Errorf("Expected 0, got %d", result4)
}
}

func numDecodings(s string) int {
dp := make([]int, len(s)+1)

dp[0] = 1

for i := 1; i <= len(s); i++ {
curBefore1 := s[i-1]
if curBefore1 != '0' {
dp[i] += dp[i-1]
}
if i > 1 {
curBefore2 := s[i-2]
if curBefore2 != '0' && (curBefore2-'0')*10+(curBefore1-'0') <= 26 {
dp[i] += dp[i-2]
}
}
}

return dp[len(s)]
}
48 changes: 48 additions & 0 deletions valid-anagram/neverlish.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// 시간복잡도: O(n)
// 공간복잡도: O(n)

package main

import (
"testing"
)

func Test(t *testing.T) {
result1 := isAnagram("anagram", "nagaram")

if !result1 {
t.Fatal("failed test1")
}

result2 := isAnagram("rat", "car")

if result2 {
t.Fatal("failed test2")
}
}

func isAnagram(s string, t string) bool {
if len(s) != len(t) {
return false
}

sMap := make(map[rune]int)

for _, c := range s {
sMap[c]++
}

for _, c := range t {
if _, ok := sMap[c]; !ok {
return false
}

sMap[c]--

if sMap[c] == 0 {
delete(sMap, c)
}
}

return len(sMap) == 0
}
Loading