Skip to content

Commit

Permalink
solve: rest week7 problems
Browse files Browse the repository at this point in the history
  • Loading branch information
Invidam committed Jun 14, 2024
1 parent 22edc6a commit 4c9c346
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 68 deletions.
83 changes: 83 additions & 0 deletions binary-tree-level-order-traversal/invidam.go.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
트리λ₯Ό μˆœνšŒν•˜λŠ” 방법듀 쀑, 레벨 μˆœμ„œλŒ€λ‘œ μˆœνšŒν•˜λŠ” 방식을 μ•Œκ³ μžˆμ–΄ 이λ₯Ό μ΄μš©ν–ˆλ‹€.
# Approach
1. BFSλ₯Ό μ§„ν–‰ν•œλ‹€.
2. μ§„ν–‰ν•˜κΈ° μ „, ν•΄λ‹Ή λ ˆλ²¨μ— λͺ‡κ°œμ˜ μ›μ†Œκ°€ μžˆλŠ”μ§€(`currLen`)을 μ €μž₯ν•œλ‹€.
3. μ €μž₯된 μ›μ†Œλ§ŒνΌ μˆœνšŒν–ˆλ‹€λ©΄, λ ˆλ²¨μ„ λ‹€ λ‘˜λŸ¬λ³Έ κ²ƒμ΄λ―€λ‘œ λΆ€κ°€μž‘μ—…(`levels = append(levels, level)`)을 ν•œλ‹€.

# Complexity
- Time complexity: $O(n)$
- 트리의 μ›μ†Œλ₯Ό n개라고 ν–ˆμ„ λ•Œ, λͺ¨λ“  μ›μ†Œλ₯Ό μˆœνšŒν•˜λŠ” λΉ„μš© `O(n)`이 μ†Œλͺ¨λœλ‹€.
- Space complexity: $O(n)$
- 트리의 μ›μ†Œλ₯Ό n개라고 ν–ˆμ„ λ•Œ, λͺ¨λ“  μ›μ†Œλ“€μ„ μ €μž₯ν•˜λŠ” 배열이 `O(n)`을 μ†Œλͺ¨ν•œλ‹€.

# Code
```go
func levelOrder(root *TreeNode) [][]int {
levels := make([][]int, 0)

if root == nil {
return levels
}

q := []*TreeNode{root}

for len(q) > 0 {
level := make([]int, 0)
currLen := len(q)
for i := 0; i < currLen; i++ {
front := q[0]
level = append(level, front.Val)
q = q[1:]

if front.Left != nil {
q = append(q, front.Left)
}
if front.Right != nil {
q = append(q, front.Right)
}
}
levels = append(levels, level)
}

return levels
}

```

```go
func levelOrder(root *TreeNode) [][]int {
levels := make([][]int, 0)

if root == nil {
return levels
}

q := []*TreeNode{root}
var nextQ []*TreeNode

for len(q) > 0 {
level := make([]int, 0)
for _, front := range q {
level = append(level, front.Val)

if front.Left != nil {
nextQ = append(nextQ, front.Left)
}
if front.Right != nil {
nextQ = append(nextQ, front.Right)
}
}
q, nextQ = nextQ, q[:0]

levels = append(levels, level)
}

return levels
}

```
- 첫 번째 μ½”λ“œλŠ” `q[1:]`을 μ΄μš©ν•΄μ„œ 큐의 `pop()`을 κ΅¬ν˜„ν•œλ‹€. ν•˜μ§€λ§Œ GoLangμ—μ„œλŠ” `pop()`을 ν•œλ‹€κ³ ν•΄μ„œ, μ°Έμ‘°κ°€ ν•΄μ œλ˜μ§€ μ•Šμ•„ λ©”λͺ¨λ¦¬λ₯Ό 계속 μž‘μ•„λ¨ΉλŠ”λ‹€.
- `pop()`을 ν•˜λ”λΌλ„ `q`κ°€ μˆœνšŒν•˜λŠ” λͺ¨λ“  μ›μ†Œλ“€μ„ μ°Έμ‘°ν•˜κ³  μžˆλ‹€.
- 두 번째 μ½”λ“œλŠ” `q, nextQ = nextQ, q[:0]`을 μ΄μš©ν•΄μ„œ `q`와 `nextQ`의 λ©”λͺ¨λ¦¬ μ˜μ—­μ„ ꡐ체할 뿐, 뢀가적인 λ©”λͺ¨λ¦¬ 곡간을 ν•„μš”λ‘œ ν•˜μ§€ μ•Šμ•„ λ”μš± νš¨μœ¨μ μ΄λ‹€.
68 changes: 0 additions & 68 deletions remove-nth-node-from-end-of-list/invidam.go.md

This file was deleted.

84 changes: 84 additions & 0 deletions validate-binary-search-tree/invidam.go.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# Intuition (Array)
BSTλŠ” μ •λ ¬ 관계λ₯Ό λ§Œμ‘±ν•˜λ―€λ‘œ, μˆœμ„œμ— 맞게 배열에 μ €μž₯ν•œ ν›„ μ •λ ¬ 관계가 λ§žλŠ”μ§€ λΉ„κ΅ν–ˆλ‹€.
# Approach
<!-- Describe your approach to solving the problem. -->
1. μ€‘μœ„ 순회(쒌 --> 루트 --> 우)λ₯Ό ν•˜λ©° 배열을 μ±„μš΄λ‹€. (`fillNodes()`)
2. 배열을 μˆœνšŒν•˜λ©° μ •λ ¬ 관계가 λ²—μ–΄λ‚¬λŠ”μ§€ νŒλ³„ν•œλ‹€. (`if nodes[i] >= nodes[i+1]`)

# Complexity
- Time complexity: $O(n)$
- 트리의 μ›μ†Œλ₯Ό n개라고 ν–ˆμ„ λ•Œ, λͺ¨λ“  μ›μ†Œλ₯Ό μˆœνšŒν•˜λŠ” λΉ„μš© `O(n)`이 μ†Œλͺ¨λœλ‹€.
- Space complexity: $O(n)$
- 트리의 μ›μ†Œλ₯Ό n개라고 ν–ˆμ„ λ•Œ, λͺ¨λ“  μ›μ†Œλ“€μ„ μ €μž₯ν•˜λŠ” 배열이 `O(n)`을 μ†Œλͺ¨ν•œλ‹€.

# Code
```go
func fillNodes(root *TreeNode) []int {
nodes := make([]int, 0)
if root.Left != nil {
nodes = append(nodes, fillNodes(root.Left)...)
}
nodes = append(nodes, root.Val)
if root.Right != nil {
nodes = append(nodes, fillNodes(root.Right)...)
}
return nodes
}

func isValidBST(root *TreeNode) bool {
nodes := fillNodes(root)
for i := 0; i < len(nodes)-1; i++ {
if nodes[i] >= nodes[i+1] {
return false
}
}
return true
}

```
# Intuition (Recursion)
예제λ₯Ό μ°Έκ³ ν–ˆμ„ λ•Œ, BST의 λ²”μœ„(μ΅œμ†Œ, μ΅œλŒ€)λ₯Ό λ²—μ–΄λ‚˜μ§€ μ•ŠλŠ”μ§€λ₯Ό μœ μ§€ν•˜λ©΄ νŒλ³„ν•  수 μžˆλ‹€κ³  μƒκ°ν–ˆλ‹€.
# Approach
<!-- Describe your approach to solving the problem. -->
1. μ „μœ„ 순회(루트 --> 쒌 --> 우)λ₯Ό ν•œλ‹€.
2. ν•΄λ‹Ή λ…Έλ“œμ—μ„œ 주어진 λ²”μœ„λ₯Ό λ²—μ–΄λ‚˜λŠ”μ§€ νŒλ³„ν•œλ‹€. (`if !(min < root.Val && root.Val < max)`)
3. μžμ‹ λ…Έλ“œλ“€μ— λŒ€ν•΄μ„œλ„ BSTκ°€ λ§Œμ‘±ν•˜λŠ”μ§€ μž¬κ·€ν•¨μˆ˜ ν˜ΈμΆœμ„ 톡해 νŒλ³„ν•œλ‹€.
- κ°€λŠ₯ν•œ λ²”μœ„λŠ” ν•΄λ‹Ή 루트 λ…Έλ“œλ₯Ό κΈ°μ€€μœΌλ‘œ κ°±μ‹ ν•œλ‹€.
# Complexity
- Time complexity: $O(n)$
- 트리의 μ›μ†Œλ₯Ό n개라고 ν–ˆμ„ λ•Œ, λͺ¨λ“  μ›μ†Œλ₯Ό μˆœνšŒν•˜λŠ” λΉ„μš© `O(n)`이 μ†Œλͺ¨λœλ‹€.
- Space complexity: $O(n)$
- 트리의 μ›μ†Œλ₯Ό n개라고 ν–ˆμ„ λ•Œ, 트리의 λ†’μ΄λ§ŒνΌ 콜 μŠ€νƒμ΄ λ°œμƒν•˜μ—¬ λ³΅μž‘λ„κ°€ μ†Œλͺ¨λœλ‹€.
- μ΅œμ•…μ˜ 경우(`skewed tree`), λ†’μ΄λŠ” `n`κ°œμ΄λ―€λ‘œ `O(n)`을 μ†Œλͺ¨ν•œλ‹€.
- μ΅œμ„ μ˜ 경우(`perfect binary tree`), λ†’μ΄λŠ” `log(n)`개 μ΄λ―€λ‘œ `O(log(n))`을 μ†Œλͺ¨ν•œλ‹€.

# Code
```go
func isValidBSTFrom(root *TreeNode, min, max int) bool {
if root == nil {
return true
}
if !(min < root.Val && root.Val < max) {
return false
}
return isValidBSTFrom(root.Left, min, root.Val) && isValidBSTFrom(root.Right, root.Val, max)
}

func isValidBST(root *TreeNode) bool {
return isValidBSTFrom(root, math.MinInt, math.MaxInt)
}

```

# Learned
- `math` 라이브러리의 `MinInt`, `MaxInt` μ‚¬μš©λ²•
- `MinInt`λŠ” `-1 << 31`둜 κ³„μ‚°ν•œλ‹€.
- μ™œ `-`을 μ‰¬ν”„νŠΈν•˜λŠ”μ§€ κΆκΈˆν•΄μ„œ μ°Ύμ•„λ΄€λ‹€.
- λΉ„νŠΈ 맨뒀가 1인 μˆ˜λ“€μ€ `<< 31`을 ν•˜λ©΄ λͺ¨λ‘ `MinInt`λ₯Ό λ§Œλ“€ 수 μžˆμ§€λ§Œ, `1 << 31`둜 `MaxInt`와 λŒ€λΉ„λ˜λ„λ‘ `-1`을 μ„ νƒν•œ 것 κ°™λ‹€. λ‹€λ₯Έ μˆ˜λ“€μ— λΉ„ν•΄ μ’€ 더 직관적이기도 ν•˜λ‹€.

- `...` operator (Three dots, Ellipsis)
- unpacking을 ν•˜λŠ” μš©λ„μ΄λ‹€.
- 사둀
- ν•¨μˆ˜ 인자둜 λͺ‡ κ°œμΈμ§€ μ•Œ 수 μ—†λŠ” μΈμžλ“€μ΄ λ“€μ–΄μ˜¬ λ•Œ
- λ°°μ—΄ μ΄ˆκΈ°ν™” μ‹œ 길이λ₯Ό λͺ°λΌ μ»΄νŒŒμΌλŸ¬μ—κ²Œ 맑기고 싢을 λ•Œ
- 참고: [링크](https://blog.advenoh.pe.kr/go/Go%EC%97%90%EC%84%9C-%EC%82%BC-%EB%8F%84%ED%8A%B8-dot-%EC%82%AC%EC%9A%A9%EB%B0%A9%EB%B2%95-Three-Dots-Usage/)

0 comments on commit 4c9c346

Please sign in to comment.