Skip to content

[jinho] week 07 #957

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

Merged
merged 1 commit into from
Jan 27, 2025
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
49 changes: 49 additions & 0 deletions longest-substring-without-repeating-characters/neverlish.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// 시간복잡도: O(n^2)
// 공간복잡도: O(n)

package main

import "testing"

func TestLengthOfLongestSubstring(t *testing.T) {
result1 := lengthOfLongestSubstring("abcabcbb")

if result1 != 3 {
t.Errorf("Expected 3, but got %v", result1)
}

result2 := lengthOfLongestSubstring("bbbbb")
if result2 != 1 {
t.Errorf("Expected 1, but got %v", result2)
}

result3 := lengthOfLongestSubstring("pwwkew")
if result3 != 3 {
t.Errorf("Expected 3, but got %v", result3)
}

result4 := lengthOfLongestSubstring("")
if result4 != 0 {
t.Errorf("Expected 0, but got %v", result4)
}
}

func lengthOfLongestSubstring(s string) int {
result := 0

for i := 0; i < len(s); i++ {
m := make(map[rune]bool)
for j := i; j < len(s); j++ {
if _, ok := m[rune(s[j])]; ok {
break
}
m[rune(s[j])] = true
}

if len(m) > result {
result = len(m)
}
}

return result
}
58 changes: 58 additions & 0 deletions number-of-islands/neverlish.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// 시간복잡도: O(n^2)
// 공간복잡도: O(n)

package main

import "testing"

func TestNumIslands(t *testing.T) {
result1 := numIslands([][]byte{
{'1', '1', '1', '1', '0'},
{'1', '1', '0', '1', '0'},
{'1', '1', '0', '0', '0'},
{'0', '0', '0', '0', '0'},
})

if result1 != 1 {
t.Errorf("Expected 1, but got %v", result1)
}

result2 := numIslands([][]byte{
{'1', '1', '0', '0', '0'},
{'1', '1', '0', '0', '0'},
{'0', '0', '1', '0', '0'},
{'0', '0', '0', '1', '1'},
})

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

func dfs(grid [][]byte, i, j int) {
if i < 0 || j < 0 || i >= len(grid) || j >= len(grid[i]) || grid[i][j] == '0' {
return
}

grid[i][j] = '0'

dfs(grid, i-1, j)
dfs(grid, i+1, j)
dfs(grid, i, j-1)
dfs(grid, i, j+1)
}

func numIslands(grid [][]byte) int {
result := 0

for i := 0; i < len(grid); i++ {
for j := 0; j < len(grid[i]); j++ {
if grid[i][j] == '1' {
result++
dfs(grid, i, j)
}
}
}

return result
}
63 changes: 63 additions & 0 deletions reverse-linked-list/neverlish.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// 시간복잡도: O(n)
// 공간복잡도: O(1)

package main

import "testing"

func TestReverseList(t *testing.T) {
result1 := reverseList(&ListNode{
Val: 1,
Next: &ListNode{
Val: 2,
Next: &ListNode{
Val: 3,
Next: &ListNode{
Val: 4,
Next: &ListNode{
Val: 5,
Next: nil,
},
},
},
},
})

if result1.Val != 5 {
t.Errorf("Expected 5, but got %v", result1.Val)
}

if result1.Next.Val != 4 {
t.Errorf("Expected 4, but got %v", result1.Next.Val)
}

if result1.Next.Next.Val != 3 {
t.Errorf("Expected 3, but got %v", result1.Next.Next.Val)
}

if result1.Next.Next.Next.Val != 2 {
t.Errorf("Expected 2, but got %v", result1.Next.Next.Next.Val)
}

if result1.Next.Next.Next.Next.Val != 1 {
t.Errorf("Expected 1, but got %v", result1.Next.Next.Next.Next.Val)
}
}

type ListNode struct {
Val int
Next *ListNode
}

func reverseList(head *ListNode) *ListNode {
var prev *ListNode

for head != nil {
next := head.Next
head.Next = prev
prev = head
head = next
}

return prev
}
Loading