Skip to content

Commit 149effb

Browse files
committed
feat(invidam): week 2 update.
1 parent 71d9c80 commit 149effb

File tree

5 files changed

+436
-0
lines changed

5 files changed

+436
-0
lines changed

โ€Žinvert-binary-tree/invidam.go.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# Intuition
2+
<!-- Describe your first thoughts on how to solve this problem. -->
3+
Use DFS, referencing the property of a tree (i.e. child node is the root node of a subtree.)
4+
# Approach
5+
<!-- Describe your approach to solving the problem. -->
6+
1. Visit the root node.
7+
2. If visited node is `nil`, return `nil`
8+
3. Swap left and right nodes.
9+
4. Visit Swapped left and right nodes.
10+
5. Repeat Step 2 ~ 4.
11+
# Complexity
12+
- Time complexity: $$O(n)$$
13+
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
14+
15+
- Space complexity: $$O(n)$$
16+
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
17+
18+
# Code
19+
```
20+
func invertTree(root *TreeNode) *TreeNode {
21+
if root == nil {
22+
return nil
23+
}
24+
root.Left, root.Right = invertTree(root.Right), invertTree(root.Left)
25+
return root
26+
}
27+
```
28+
- - -
29+
# Intuition
30+
<!-- Describe your first thoughts on how to solve this problem. -->
31+
Visit, But Use BFS. (`for loop`)
32+
# Approach
33+
<!-- Describe your approach to solving the problem. -->
34+
1. Create Queue and push root node to it.
35+
2. If the queue is empty, return the `root` node.
36+
3. Otherwise, pop the top node.
37+
4. Swap the left and right children of the removed node.
38+
5. Push swapped children.
39+
4. Repeat Step
40+
# Complexity
41+
- Time complexity: $$O(n)$$
42+
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
43+
44+
- Space complexity: $$O(n)$$
45+
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
46+
47+
# Code
48+
```
49+
type Queue[T any] struct {
50+
Index int
51+
Nodes []T
52+
}
53+
54+
func NewQueue[T any]() Queue[T] {
55+
return Queue[T]{Nodes: make([]T, 0)}
56+
}
57+
58+
func (q *Queue[T]) Push(node T) {
59+
q.Nodes = append(q.Nodes, node)
60+
}
61+
62+
func (q *Queue[T]) Pop() T {
63+
ret := q.Nodes[q.Index]
64+
q.Index++
65+
return ret
66+
}
67+
68+
func (q *Queue[T]) IsEmpty() bool {
69+
return q.Index == len(q.Nodes)
70+
}
71+
72+
func invertTree(root *TreeNode) *TreeNode {
73+
q := NewQueue[*TreeNode]()
74+
q.Push(root)
75+
for !q.IsEmpty() {
76+
t := q.Pop()
77+
if t == nil {
78+
continue
79+
}
80+
t.Left, t.Right = t.Right, t.Left
81+
82+
q.Push(t.Left)
83+
q.Push(t.Right)
84+
}
85+
return root
86+
}
87+
```
88+
89+
# Learned
90+
- ๊ณ ์–ธ์–ด์—์„œ `a, b = b, a` ์ฒ˜๋Ÿผ ๊ฐ„๊ฒฐํ•œ ์ฝ”๋”ฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
91+
- ํฌ์ธํ„ฐ ํƒ€์ž…๊ณผ ์ผ๋ฐ˜ ํƒ€์ž…์˜ ์ฐจ์ด (๊ณ ์–ธ์–ด์—์„œ๋Š” ์ผ๋ฐ˜ ํƒ€์ž…์„ ๋„˜๊ธธ ์‹œ ๋ฌด์กฐ๊ฑด ๋ณต์‚ฌํ•œ๋‹ค.)

โ€Žlinked-list-cycle/invidam.go.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Intuition
2+
<!-- Describe your first thoughts on how to solve this problem. -->
3+
Makr elements as visited by setting a dirty bit.
4+
Check Dirty bit to visited nodes.
5+
# Approach
6+
<!-- Describe your approach to solving the problem. -->
7+
1. Initiate a constant value named `visited` over the input range. (In my case `-10001`.)
8+
2. Start Visitng node. by head.
9+
3. Check if the current node is `nil` or its value matches the visited value (Initially, the head node should not be visited value)
10+
4. If node is valid(i.e. not `nil` and not `visited`), set the node's value as the visited value.
11+
5. Move to the next node and repeat step 3.
12+
13+
# Complexity
14+
- Time complexity: $$O(n)$$
15+
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
16+
17+
18+
- Space complexity: $$O(n)$$
19+
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
20+
(n = list's size)
21+
# Code
22+
```
23+
const visited = -10001
24+
func hasCycle(head *ListNode) bool {
25+
if head == nil {
26+
return false;
27+
}
28+
if head.Val == visited {
29+
return true;
30+
}
31+
head.Val = visited
32+
return hasCycle(head.Next)
33+
}
34+
```
35+
36+
- - -
37+
# Institution
38+
Use "The tortoise and hare" Algorithom.
39+
# Approach
40+
<!-- Describe your approach to solving the problem. -->
41+
1. Designate two node to move at differnt speeds. One node should move faster (referred to as `fast`), and the other should move slower (`slow`)
42+
2. Allow both nodes to iterate through the graph, with each node moving at its designated speed.
43+
3. If the `fast` node catches up to the `slow` node (i.e. both node points to the same node at same point), the the graph contains a cycle.is cycle.
44+
# Complexity
45+
- Time complexity: $$O(n)$$
46+
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
47+
- Space complexity: $$O(1)$$
48+
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
49+
# Code
50+
```
51+
func hasCycle(head *ListNode) bool {
52+
if head == nil || head.Next == nil {
53+
return false
54+
}
55+
for slow, fast := head, head.Next; fast != nil && fast.Next != nil; slow, fast = slow.Next, fast.Next.Next{
56+
if slow == fast {
57+
return true
58+
}
59+
}
60+
return false
61+
}
62+
63+
```
64+
# Learned
65+
- ์•ฝํ•œ ๋ถ€๋ถ„์ด์—ˆ๋˜ ํˆฌํฌ์ธํ„ฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๋Œ€ํ•ด ์ข€ ๋” ์ƒ๊ฐํ•ด๋ณด์•˜๋‹ค.
66+
- ๋ฐ˜๋ณต๋ฌธ์„ ๊น”๋”ํ•˜๊ฒŒ ์ž‘์„ฑํ•˜๋Š” ๋ฒ•์„ ๊ณ ๋ฏผํ•ด๋ณด์•˜๋‹ค.
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# Intuition
2+
<!-- Describe your first thoughts on how to solve this problem. -->
3+
๋งํฌ๋“œ๋ฆฌ์ŠคํŠธ --> ๋ฐ˜๋ณต๋ฌธ or ์žฌ๊ท€ํ˜ธ์ถœ์ด ๋– ์˜ฌ๋ž๋‹ค.
4+
# Approach
5+
<!-- Describe your approach to solving the problem. -->
6+
- ์žฌ๊ท€ ํ˜ธ์ถœ๊ณผ ๋ฐ˜๋ณต๋ฌธ ์‚ฌ์ด์—์„œ ๊ณ ๋ฏผํ•˜์˜€๋‹ค.
7+
- ์ƒˆ๋กœ์šด ๋…ธ๋“œ๋ฅผ ๋งŒ๋“ค๊ณ , ์ž‘์€ ๋…ธ๋“œ๋“ค์„ ์ด์–ด ๋ถ™์˜€๋‹ค. (V1_1, V2_1)
8+
- ์ƒˆ๋กœ์šด ๋…ธ๋“œ์˜ ์ƒ์„ฑ ์—†์ด, ๊ธฐ์กด ๋…ธ๋“œ๋“ค์„ ์—ฐ๊ฒฐํ•˜์—ฌ ํ•ด๊ฒฐํ•  ์ˆ˜๋„ ์žˆ์—ˆ๋‹ค. (V1_2, V2_2)
9+
# Complexity
10+
- Time complexity: $$O(N+M)$$
11+
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
12+
13+
- Space complexity: $$O(N+M)$$
14+
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
15+
16+
(N,M์€ ๊ฐ๊ฐ ๋‘ ๋งํฌ๋“œ๋ฆฌ์ŠคํŠธ์˜ ๊ธธ์ด)
17+
# Code
18+
```
19+
func mergeTwoListsV1(list1 *ListNode, list2 *ListNode) *ListNode {
20+
if list1 == nil && list2 == nil {
21+
return nil
22+
} else if list1 != nil {
23+
return list1
24+
} else if list2 != nil {
25+
return list2
26+
}
27+
// assert list1, list2 is not nil
28+
var val int
29+
if list1.Val < list2.Val {
30+
val = list1.Val
31+
list1 = list1.Next
32+
} else {
33+
val = list2.Val
34+
list2 = list2.Next
35+
}
36+
37+
return &ListNode{Val: val, Next: mergeTwoListsV1(list1, list2)}
38+
}
39+
40+
func mergeTwoListsV1_2(list1 *ListNode, list2 *ListNode) *ListNode {
41+
if list1 == nil {
42+
return list2
43+
} else if list2 == nil {
44+
return list1
45+
}
46+
47+
if list1.Val < list2.Val {
48+
list1.Next = mergeTwoListsV1_2(list1.Next, list2)
49+
return list1
50+
} else {
51+
list2.Next = mergeTwoListsV1_2(list1, list2.Next)
52+
return list2
53+
}
54+
}
55+
56+
func mergeTwoListsV2_1(list1 *ListNode, list2 *ListNode) *ListNode {
57+
var head = &ListNode{}
58+
curr := head
59+
60+
for list1 != nil && list2 != nil {
61+
if list1.Val < list2.Val {
62+
curr.Next = list1
63+
list1 = list1.Next
64+
} else {
65+
curr.Next = list2
66+
list2 = list2.Next
67+
}
68+
curr = curr.Next
69+
}
70+
71+
if list1 != nil {
72+
curr.Next = list1
73+
} else if list2 != nil {
74+
curr.Next = list2
75+
}
76+
77+
return head.Next
78+
}
79+
80+
func mergeTwoListsV2_2(list1 *ListNode, list2 *ListNode) *ListNode {
81+
if list1 == nil {
82+
return list2
83+
} else if list2 == nil {
84+
return list1
85+
}
86+
87+
head := list1
88+
if list1.Val > list2.Val {
89+
head = list2
90+
list2 = list2.Next
91+
} else {
92+
list1 = list1.Next
93+
}
94+
curr := head
95+
96+
for list1 != nil && list2 != nil {
97+
if list1.Val < list2.Val {
98+
curr.Next = list1
99+
list1 = list1.Next
100+
} else {
101+
curr.Next = list2
102+
list2 = list2.Next
103+
}
104+
curr = curr.Next
105+
}
106+
107+
if list1 != nil {
108+
curr.Next = list1
109+
} else if list2 != nil {
110+
curr.Next = list2
111+
}
112+
113+
return head
114+
}
115+
116+
```

โ€Žreverse-linked-list/invidam.go.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Intuition
2+
๋‘ ๋ณ€์ˆ˜๊ฐ„์˜ Swap์ด ๋– ์˜ฌ๋ž๋‹ค.
3+
4+
# Approach
5+
<!-- Describe your approach to solving the problem. -->
6+
- ๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ์˜ "์–ธ์ œ๋‚˜ ์ž์‹ ์˜ ๊ฐ’๊ณผ ๋‹ค์Œ ๋…ธ๋“œ์˜ ์ฃผ์†Œ๋ฅผ ์œ ์ง€ํ•œ๋‹ค"๋Š” ํŠน์„ฑ์ด ์žฌ๊ท€ ํ•จ์ˆ˜๋ฅผ ์œ ์šฉํ•˜๊ฒŒ ์“ธ ์ˆ˜ ์žˆ๋‹ค๊ณ  ํŒ๋‹จํ–ˆ๋‹ค.
7+
# Complexity
8+
- Time complexity: $$O(n)$$
9+
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
10+
11+
- Space complexity: $$O(n)$$
12+
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
13+
14+
(N์€ ๋งํฌ๋“œ๋ฆฌ์ŠคํŠธ์˜ ๊ธธ์ด)
15+
*/
16+
17+
# Code
18+
- V1์€ ์žฌ๊ท€ํ˜ธ์ถœ, V2๋Š” ๋ฐ˜๋ณต๋ฌธ ํ™œ์šฉ์„ ํ•œ ๊ฒฝ์šฐ์ด๋‹ค.
19+
- V3์€ V1์„ ๊ฐœ์„ ํ•˜์—ฌ, ํ•˜๋‚˜์˜ ํ•จ์ˆ˜๋งŒ์œผ๋กœ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ํ•ด๊ฒฐํ–ˆ๋‹ค.
20+
```
21+
22+
func reverseListV3(head *ListNode) *ListNode {
23+
if head == nil || head.Next == nil {
24+
return head
25+
}
26+
27+
newHead := reverseList(head.Next)
28+
29+
head.Next.Next = head
30+
head.Next = nil
31+
32+
return newHead
33+
}
34+
35+
func reverseListV2(head *ListNode) *ListNode {
36+
var prev *ListNode
37+
curr := head
38+
for curr != nil {
39+
next := curr.Next
40+
curr.Next = prev
41+
prev = curr
42+
curr = next
43+
}
44+
return prev
45+
}
46+
47+
func reverseNodeAllV1(head *ListNode, prev *ListNode) *ListNode {
48+
if head == nil {
49+
return prev
50+
}
51+
next := head.Next
52+
head.Next = prev
53+
return reverseNodeAllV1(next, head)
54+
}
55+
func reverseListV1(head *ListNode) *ListNode {
56+
return reverseNodeAllV1(head, nil)
57+
}
58+
```
59+
60+
# Remind
61+
์žฌ๊ท€ ํ•จ์ˆ˜์˜ ํŠน์„ฑ(์ž๊ธฐ ์ž์‹ ์„ ํ˜ธ์ถœํ•œ ์ดํ›„ ์ด์ „์˜ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ )์„ ํ™œ์šฉํ•ด ๊ฐ„๊ฒฐํ•œ ์ฝ”๋”ฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

0 commit comments

Comments
ย (0)