-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
436 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# Intuition | ||
<!-- Describe your first thoughts on how to solve this problem. --> | ||
Use DFS, referencing the property of a tree (i.e. child node is the root node of a subtree.) | ||
# Approach | ||
<!-- Describe your approach to solving the problem. --> | ||
1. Visit the root node. | ||
2. If visited node is `nil`, return `nil` | ||
3. Swap left and right nodes. | ||
4. Visit Swapped left and right nodes. | ||
5. Repeat Step 2 ~ 4. | ||
# Complexity | ||
- Time complexity: $$O(n)$$ | ||
<!-- Add your time complexity here, e.g. $$O(n)$$ --> | ||
|
||
- Space complexity: $$O(n)$$ | ||
<!-- Add your space complexity here, e.g. $$O(n)$$ --> | ||
|
||
# Code | ||
``` | ||
func invertTree(root *TreeNode) *TreeNode { | ||
if root == nil { | ||
return nil | ||
} | ||
root.Left, root.Right = invertTree(root.Right), invertTree(root.Left) | ||
return root | ||
} | ||
``` | ||
- - - | ||
# Intuition | ||
<!-- Describe your first thoughts on how to solve this problem. --> | ||
Visit, But Use BFS. (`for loop`) | ||
# Approach | ||
<!-- Describe your approach to solving the problem. --> | ||
1. Create Queue and push root node to it. | ||
2. If the queue is empty, return the `root` node. | ||
3. Otherwise, pop the top node. | ||
4. Swap the left and right children of the removed node. | ||
5. Push swapped children. | ||
4. Repeat Step | ||
# Complexity | ||
- Time complexity: $$O(n)$$ | ||
<!-- Add your time complexity here, e.g. $$O(n)$$ --> | ||
|
||
- Space complexity: $$O(n)$$ | ||
<!-- Add your space complexity here, e.g. $$O(n)$$ --> | ||
|
||
# Code | ||
``` | ||
type Queue[T any] struct { | ||
Index int | ||
Nodes []T | ||
} | ||
func NewQueue[T any]() Queue[T] { | ||
return Queue[T]{Nodes: make([]T, 0)} | ||
} | ||
func (q *Queue[T]) Push(node T) { | ||
q.Nodes = append(q.Nodes, node) | ||
} | ||
func (q *Queue[T]) Pop() T { | ||
ret := q.Nodes[q.Index] | ||
q.Index++ | ||
return ret | ||
} | ||
func (q *Queue[T]) IsEmpty() bool { | ||
return q.Index == len(q.Nodes) | ||
} | ||
func invertTree(root *TreeNode) *TreeNode { | ||
q := NewQueue[*TreeNode]() | ||
q.Push(root) | ||
for !q.IsEmpty() { | ||
t := q.Pop() | ||
if t == nil { | ||
continue | ||
} | ||
t.Left, t.Right = t.Right, t.Left | ||
q.Push(t.Left) | ||
q.Push(t.Right) | ||
} | ||
return root | ||
} | ||
``` | ||
|
||
# Learned | ||
- κ³ μΈμ΄μμ `a, b = b, a` μ²λΌ κ°κ²°ν μ½λ©μ΄ κ°λ₯νλ€. | ||
- ν¬μΈν° νμ κ³Ό μΌλ° νμ μ μ°¨μ΄ (κ³ μΈμ΄μμλ μΌλ° νμ μ λκΈΈ μ 무쑰건 볡μ¬νλ€.) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# Intuition | ||
<!-- Describe your first thoughts on how to solve this problem. --> | ||
Makr elements as visited by setting a dirty bit. | ||
Check Dirty bit to visited nodes. | ||
# Approach | ||
<!-- Describe your approach to solving the problem. --> | ||
1. Initiate a constant value named `visited` over the input range. (In my case `-10001`.) | ||
2. Start Visitng node. by head. | ||
3. Check if the current node is `nil` or its value matches the visited value (Initially, the head node should not be visited value) | ||
4. If node is valid(i.e. not `nil` and not `visited`), set the node's value as the visited value. | ||
5. Move to the next node and repeat step 3. | ||
|
||
# Complexity | ||
- Time complexity: $$O(n)$$ | ||
<!-- Add your time complexity here, e.g. $$O(n)$$ --> | ||
|
||
|
||
- Space complexity: $$O(n)$$ | ||
<!-- Add your space complexity here, e.g. $$O(n)$$ --> | ||
(n = list's size) | ||
# Code | ||
``` | ||
const visited = -10001 | ||
func hasCycle(head *ListNode) bool { | ||
if head == nil { | ||
return false; | ||
} | ||
if head.Val == visited { | ||
return true; | ||
} | ||
head.Val = visited | ||
return hasCycle(head.Next) | ||
} | ||
``` | ||
|
||
- - - | ||
# Institution | ||
Use "The tortoise and hare" Algorithom. | ||
# Approach | ||
<!-- Describe your approach to solving the problem. --> | ||
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`) | ||
2. Allow both nodes to iterate through the graph, with each node moving at its designated speed. | ||
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. | ||
# Complexity | ||
- Time complexity: $$O(n)$$ | ||
<!-- Add your time complexity here, e.g. $$O(n)$$ --> | ||
- Space complexity: $$O(1)$$ | ||
<!-- Add your space complexity here, e.g. $$O(n)$$ --> | ||
# Code | ||
``` | ||
func hasCycle(head *ListNode) bool { | ||
if head == nil || head.Next == nil { | ||
return false | ||
} | ||
for slow, fast := head, head.Next; fast != nil && fast.Next != nil; slow, fast = slow.Next, fast.Next.Next{ | ||
if slow == fast { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
``` | ||
# Learned | ||
- μ½ν λΆλΆμ΄μλ ν¬ν¬μΈν° μκ³ λ¦¬μ¦μ λν΄ μ’ λ μκ°ν΄λ³΄μλ€. | ||
- λ°λ³΅λ¬Έμ κΉλνκ² μμ±νλ λ²μ κ³ λ―Όν΄λ³΄μλ€. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
# Intuition | ||
<!-- Describe your first thoughts on how to solve this problem. --> | ||
λ§ν¬λ리μ€νΈ --> λ°λ³΅λ¬Έ or μ¬κ·νΈμΆμ΄ λ μ¬λλ€. | ||
# Approach | ||
<!-- Describe your approach to solving the problem. --> | ||
- μ¬κ· νΈμΆκ³Ό λ°λ³΅λ¬Έ μ¬μ΄μμ κ³ λ―Όνμλ€. | ||
- μλ‘μ΄ λ Έλλ₯Ό λ§λ€κ³ , μμ λ Έλλ€μ μ΄μ΄ λΆμλ€. (V1_1, V2_1) | ||
- μλ‘μ΄ λ Έλμ μμ± μμ΄, κΈ°μ‘΄ λ Έλλ€μ μ°κ²°νμ¬ ν΄κ²°ν μλ μμλ€. (V1_2, V2_2) | ||
# Complexity | ||
- Time complexity: $$O(N+M)$$ | ||
<!-- Add your time complexity here, e.g. $$O(n)$$ --> | ||
|
||
- Space complexity: $$O(N+M)$$ | ||
<!-- Add your space complexity here, e.g. $$O(n)$$ --> | ||
|
||
(N,Mμ κ°κ° λ λ§ν¬λ리μ€νΈμ κΈΈμ΄) | ||
# Code | ||
``` | ||
func mergeTwoListsV1(list1 *ListNode, list2 *ListNode) *ListNode { | ||
if list1 == nil && list2 == nil { | ||
return nil | ||
} else if list1 != nil { | ||
return list1 | ||
} else if list2 != nil { | ||
return list2 | ||
} | ||
// assert list1, list2 is not nil | ||
var val int | ||
if list1.Val < list2.Val { | ||
val = list1.Val | ||
list1 = list1.Next | ||
} else { | ||
val = list2.Val | ||
list2 = list2.Next | ||
} | ||
return &ListNode{Val: val, Next: mergeTwoListsV1(list1, list2)} | ||
} | ||
func mergeTwoListsV1_2(list1 *ListNode, list2 *ListNode) *ListNode { | ||
if list1 == nil { | ||
return list2 | ||
} else if list2 == nil { | ||
return list1 | ||
} | ||
if list1.Val < list2.Val { | ||
list1.Next = mergeTwoListsV1_2(list1.Next, list2) | ||
return list1 | ||
} else { | ||
list2.Next = mergeTwoListsV1_2(list1, list2.Next) | ||
return list2 | ||
} | ||
} | ||
func mergeTwoListsV2_1(list1 *ListNode, list2 *ListNode) *ListNode { | ||
var head = &ListNode{} | ||
curr := head | ||
for list1 != nil && list2 != nil { | ||
if list1.Val < list2.Val { | ||
curr.Next = list1 | ||
list1 = list1.Next | ||
} else { | ||
curr.Next = list2 | ||
list2 = list2.Next | ||
} | ||
curr = curr.Next | ||
} | ||
if list1 != nil { | ||
curr.Next = list1 | ||
} else if list2 != nil { | ||
curr.Next = list2 | ||
} | ||
return head.Next | ||
} | ||
func mergeTwoListsV2_2(list1 *ListNode, list2 *ListNode) *ListNode { | ||
if list1 == nil { | ||
return list2 | ||
} else if list2 == nil { | ||
return list1 | ||
} | ||
head := list1 | ||
if list1.Val > list2.Val { | ||
head = list2 | ||
list2 = list2.Next | ||
} else { | ||
list1 = list1.Next | ||
} | ||
curr := head | ||
for list1 != nil && list2 != nil { | ||
if list1.Val < list2.Val { | ||
curr.Next = list1 | ||
list1 = list1.Next | ||
} else { | ||
curr.Next = list2 | ||
list2 = list2.Next | ||
} | ||
curr = curr.Next | ||
} | ||
if list1 != nil { | ||
curr.Next = list1 | ||
} else if list2 != nil { | ||
curr.Next = list2 | ||
} | ||
return head | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# Intuition | ||
λ λ³μκ°μ Swapμ΄ λ μ¬λλ€. | ||
|
||
# Approach | ||
<!-- Describe your approach to solving the problem. --> | ||
- λ§ν¬λ 리μ€νΈμ "μΈμ λ μμ μ κ°κ³Ό λ€μ λ Έλμ μ£Όμλ₯Ό μ μ§νλ€"λ νΉμ±μ΄ μ¬κ· ν¨μλ₯Ό μ μ©νκ² μΈ μ μλ€κ³ νλ¨νλ€. | ||
# Complexity | ||
- Time complexity: $$O(n)$$ | ||
<!-- Add your time complexity here, e.g. $$O(n)$$ --> | ||
|
||
- Space complexity: $$O(n)$$ | ||
<!-- Add your space complexity here, e.g. $$O(n)$$ --> | ||
|
||
(Nμ λ§ν¬λ리μ€νΈμ κΈΈμ΄) | ||
*/ | ||
|
||
# Code | ||
- V1μ μ¬κ·νΈμΆ, V2λ λ°λ³΅λ¬Έ νμ©μ ν κ²½μ°μ΄λ€. | ||
- V3μ V1μ κ°μ νμ¬, νλμ ν¨μλ§μΌλ‘ κ°κ²°νκ² ν΄κ²°νλ€. | ||
``` | ||
func reverseListV3(head *ListNode) *ListNode { | ||
if head == nil || head.Next == nil { | ||
return head | ||
} | ||
newHead := reverseList(head.Next) | ||
head.Next.Next = head | ||
head.Next = nil | ||
return newHead | ||
} | ||
func reverseListV2(head *ListNode) *ListNode { | ||
var prev *ListNode | ||
curr := head | ||
for curr != nil { | ||
next := curr.Next | ||
curr.Next = prev | ||
prev = curr | ||
curr = next | ||
} | ||
return prev | ||
} | ||
func reverseNodeAllV1(head *ListNode, prev *ListNode) *ListNode { | ||
if head == nil { | ||
return prev | ||
} | ||
next := head.Next | ||
head.Next = prev | ||
return reverseNodeAllV1(next, head) | ||
} | ||
func reverseListV1(head *ListNode) *ListNode { | ||
return reverseNodeAllV1(head, nil) | ||
} | ||
``` | ||
|
||
# Remind | ||
μ¬κ· ν¨μμ νΉμ±(μκΈ° μμ μ νΈμΆν μ΄ν μ΄μ μ μ½λλ₯Ό μ€νν μ μλ€λ μ )μ νμ©ν΄ κ°κ²°ν μ½λ©μ΄ κ°λ₯νλ€. |
Oops, something went wrong.