Skip to content

Commit

Permalink
Merge pull request #565 from obzva/main
Browse files Browse the repository at this point in the history
[Flynn] Week 12
  • Loading branch information
obzva authored Nov 2, 2024
2 parents c70fa10 + cde1132 commit fa89896
Show file tree
Hide file tree
Showing 5 changed files with 258 additions and 0 deletions.
41 changes: 41 additions & 0 deletions merge-intervals/flynn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
Big O
- N: ์ฃผ์–ด์ง„ ๋ฐฐ์—ด intervals์˜ ๊ธธ์ด
- Time complexity: O(NlogN)
- intervals๋ฅผ start์˜ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ -> O(NlogN)
- ๋ฐ˜๋ณต๋ฌธ -> O(N)
- O(NlogN + N) = O(NlogN)
- Space complexity: O(N)
- ์ •๋‹ต ๋ฐฐ์—ด์˜ ํฌ๊ธฐ -> O(N)
*/

import "sort"

func merge(intervals [][]int) [][]int {
sort.Slice(intervals, func(i, j int) bool {
return intervals[i][0] < intervals[j][0]
})
res := make([][]int, 0)
start := intervals[0][0]
end := intervals[0][1]
for i := 1; i < len(intervals); i++ {
curr := intervals[i]
if end >= curr[0] {
end = max(end, curr[1])
} else {
res = append(res, []int{start, end})
start = curr[0]
end = curr[1]
}
}
res = append(res, []int{start, end})
return res
}

func max(a, b int) int {
if a > b {
return a
} else {
return b
}
}
47 changes: 47 additions & 0 deletions number-of-connected-components-in-an-undirected-graph/flynn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
ํ’€์ด
- DFS์™€ hashmap(set)์„ ์ด์šฉํ•˜์—ฌ ํ’€์ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค
- ์ด์ „์— ํ’€์ดํ–ˆ๋˜ course schedule ๋ฌธ์ œ์™€ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค
Big O
- N: ๋…ธ๋“œ ๊ฐœ์ˆ˜
- E: ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜
- Time complexity: O(N + E)
- adj๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐ˜๋ณต๋ฌธ์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋Š” E์— ๋น„๋ก€ํ•˜์—ฌ ์ฆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค
- ์ „์ฒด ๋…ธ๋“œ๋ฅผ ์ตœ๋Œ€ 1๋ฒˆ์”ฉ ์กฐํšŒํ•˜๋ฏ€๋กœ ๋‘๋ฒˆ์งธ ๋ฐ˜๋ณต๋ฌธ์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋Š” N์— ๋น„๋ก€ํ•˜์—ฌ ์ฆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค
- Space complexity: O(N + E)
- adjacency list์˜ ํฌ๊ธฐ๋Š” E์— ๋น„๋ก€ํ•˜์—ฌ ์ฆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค
- checked์˜ ํฌ๊ธฐ๋Š” N์— ๋น„๋ก€ํ•˜์—ฌ ์ฆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค
- check ํ•จ์ˆ˜์˜ ์žฌ๊ท€ ํ˜ธ์ถœ ์Šคํƒ ๊นŠ์ด ๋˜ํ•œ ์ตœ์•…์˜ ๊ฒฝ์šฐ, N์— ๋น„๋ก€ํ•˜์—ฌ ์ฆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค
*/

func countComponents(n int, edges [][]int) int {
adj := make(map[int][]int)
for _, edge := range edges {
adj[edge[0]] = append(adj[edge[0]], edge[1])
adj[edge[1]] = append(adj[edge[1]], edge[0])
}
// Go๋Š” {int: bool} hashmap์„ set์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•จ
checked := make(map[int]bool) // ๋ชจ๋“  ํƒ์ƒ‰์ด ๋๋‚œ ๋…ธ๋“œ๋ฅผ ๊ธฐ๋กํ•จ
// ๊ฐ node๋ฅผ ์กฐํšŒํ•˜๋Š” ํ•จ์ˆ˜
var check func(int)
check = func(i int) {
checked[i] = true
for _, nxt := range adj[i] {
if _, ok := checked[nxt]; ok {
continue
}
check(nxt)
}
}

res := 0
for i := 0; i < n; i++ {
if _, ok := checked[i]; ok {
continue
}
res++
check(i)
}

return res
}
34 changes: 34 additions & 0 deletions remove-nth-node-from-end-of-list/flynn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
ํ’€์ด
- n+1 ๊ฐ„๊ฒฉ์„ ์œ ์ง€ํ•˜๋ฉฐ ์ด๋™ํ•˜๋Š” ๋‘ ๊ฐœ์˜ ํฌ์ธํ„ฐ๋ฅผ ์ด์šฉํ•˜๋ฉด one-pass๋กœ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค
Big O
- M: ๋งํฌ๋“œ๋ฆฌ์ŠคํŠธ์˜ ๊ธธ์ด
- Time complexity: O(M)
- Space complexity: O(1)
*/

/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/

func removeNthFromEnd(head *ListNode, n int) *ListNode {
dummy := &ListNode{Next: head}

slow := dummy
fast := dummy
for i := 0; i < n+1; i++ {
fast = fast.Next
}

for fast != nil {
slow = slow.Next
fast = fast.Next
}
slow.Next = slow.Next.Next

return dummy.Next
}
38 changes: 38 additions & 0 deletions same-tree/flynn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
ํ’€์ด
- ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด์„œ ํ’€์ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค
Big O
- N: ํŠธ๋ฆฌ ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜
- H: ํŠธ๋ฆฌ์˜ ๋†’์ด (logN <= H <= N)
- Time complexity: O(N)
- ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ์ตœ๋Œ€ 1๋ฒˆ ํƒ์ƒ‰ํ•ฉ๋‹ˆ๋‹ค
- Space complexity: O(H)
- ์žฌ๊ท€ ํ˜ธ์ถœ ์Šคํƒ์˜ ๊นŠ์ด๋Š” H์— ๋น„๋ก€ํ•˜์—ฌ ์ฆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค
*/

/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func isSameTree(p *TreeNode, q *TreeNode) bool {
// base case
if p == nil && q == nil {
return true
} else if p == nil || q == nil {
return false
}

if p.Val != q.Val {
return false
}

if !isSameTree(p.Left, q.Left) || !isSameTree(p.Right, q.Right) {
return false
}

return true
}
98 changes: 98 additions & 0 deletions serialize-and-deserialize-binary-tree/flynn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
ํ’€์ด
- DFS๋ฅผ ์ด์šฉํ•˜์—ฌ ํ’€์ดํ•ฉ๋‹ˆ๋‹ค
Big O
- N: ๋…ธ๋“œ์˜ ์ˆ˜
- Serialize
- Time complexity: O(N)
- ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ์ตœ๋Œ€ 1๋ฒˆ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค
- Space complexity: O(N)
- buildString์˜ ์žฌ๊ท€ ํ˜ธ์ถœ ์Šคํƒ์˜ ๊นŠ์ด๋Š” ๋…ธ๋“œ์˜ ๋†’์ด์— ๋น„๋ก€ํ•˜์—ฌ ์ฆ๊ฐ€ํ•˜๋ฉฐ, ๋…ธ๋“œ์˜ ๋†’์ด๋Š” ์ตœ๋Œ€ N์ž…๋‹ˆ๋‹ค
- ๊ฒฐ๊ณผ string์˜ ํฌ๊ธฐ ๋˜ํ•œ N์— ๋น„๋ก€ํ•˜๋Š” ํ˜•ํƒœ๋กœ ์ฆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค
- Deserialize
- Time complexity: O(N)
- ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ์ตœ๋Œ€ 1๋ฒˆ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค
- Space complexity: O(N)
- data๋ฅผ splitํ•œ ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๊ฐ€ N์— ๋น„๋ก€ํ•˜์—ฌ ์ฆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค
- buildTree์˜ ์žฌ๊ท€ ํ˜ธ์ถœ ์Šคํƒ์˜ ๊นŠ์ด๋Š” ๋…ธ๋“œ์˜ ๋†’์ด์— ๋น„๋ก€ํ•˜์—ฌ ์ฆ๊ฐ€ํ•˜๋ฉฐ, ๋…ธ๋“œ์˜ ๋†’์ด๋Š” ์ตœ๋Œ€ N์ž…๋‹ˆ๋‹ค
*/

import (
"strconv"
"strings"
)

/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
const (
DELIMITER = "|"
)

type Codec struct {
}

func Constructor() Codec {
codec := Codec{}
return codec
}

// Serializes a tree to a single string.
func (this *Codec) serialize(root *TreeNode) string {
if root == nil {
return ""
}
var sb strings.Builder
buildString(&sb, root)
return sb.String()
}

// Deserializes your encoded data to tree.
func (this *Codec) deserialize(data string) *TreeNode {
if data == "" {
return nil
}
splitData := make([]string, 0, len(data)/2)
splitData = strings.Split(data, DELIMITER)
splitData = splitData[:len(splitData)-1]
return buildTree(&splitData)
}

// ----- Helpers -----
func buildString(sb *strings.Builder, node *TreeNode) {
if node == nil {
sb.WriteString(DELIMITER)
return
}
sb.WriteString(strconv.Itoa(node.Val))
sb.WriteString(DELIMITER)
buildString(sb, node.Left)
buildString(sb, node.Right)
}

func buildTree(splitData *[]string) *TreeNode {
val := (*splitData)[0]
*splitData = (*splitData)[1:]
if val == "" {
return nil
}
node := &TreeNode{}
intVal, _ := strconv.Atoi(val)
node.Val = intVal
node.Left = buildTree(splitData)
node.Right = buildTree(splitData)
return node
}

/**
* Your Codec object will be instantiated and called as such:
* ser := Constructor();
* deser := Constructor();
* data := ser.serialize(root);
* ans := deser.deserialize(data);
*/

0 comments on commit fa89896

Please sign in to comment.