Skip to content

Commit 4c9c346

Browse files
committed
solve: rest week7 problems
1 parent 22edc6a commit 4c9c346

File tree

3 files changed

+167
-68
lines changed

3 files changed

+167
-68
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Intuition
2+
<!-- Describe your first thoughts on how to solve this problem. -->
3+
ํŠธ๋ฆฌ๋ฅผ ์ˆœํšŒํ•˜๋Š” ๋ฐฉ๋ฒ•๋“ค ์ค‘, ๋ ˆ๋ฒจ ์ˆœ์„œ๋Œ€๋กœ ์ˆœํšŒํ•˜๋Š” ๋ฐฉ์‹์„ ์•Œ๊ณ ์žˆ์–ด ์ด๋ฅผ ์ด์šฉํ–ˆ๋‹ค.
4+
# Approach
5+
1. BFS๋ฅผ ์ง„ํ–‰ํ•œ๋‹ค.
6+
2. ์ง„ํ–‰ํ•˜๊ธฐ ์ „, ํ•ด๋‹น ๋ ˆ๋ฒจ์— ๋ช‡๊ฐœ์˜ ์›์†Œ๊ฐ€ ์žˆ๋Š”์ง€(`currLen`)์„ ์ €์žฅํ•œ๋‹ค.
7+
3. ์ €์žฅ๋œ ์›์†Œ๋งŒํผ ์ˆœํšŒํ–ˆ๋‹ค๋ฉด, ๋ ˆ๋ฒจ์„ ๋‹ค ๋‘˜๋Ÿฌ๋ณธ ๊ฒƒ์ด๋ฏ€๋กœ ๋ถ€๊ฐ€์ž‘์—…(`levels = append(levels, level)`)์„ ํ•œ๋‹ค.
8+
9+
# Complexity
10+
- Time complexity: $O(n)$
11+
- ํŠธ๋ฆฌ์˜ ์›์†Œ๋ฅผ n๊ฐœ๋ผ๊ณ  ํ–ˆ์„ ๋•Œ, ๋ชจ๋“  ์›์†Œ๋ฅผ ์ˆœํšŒํ•˜๋Š” ๋น„์šฉ `O(n)`์ด ์†Œ๋ชจ๋œ๋‹ค.
12+
- Space complexity: $O(n)$
13+
- ํŠธ๋ฆฌ์˜ ์›์†Œ๋ฅผ n๊ฐœ๋ผ๊ณ  ํ–ˆ์„ ๋•Œ, ๋ชจ๋“  ์›์†Œ๋“ค์„ ์ €์žฅํ•˜๋Š” ๋ฐฐ์—ด์ด `O(n)`์„ ์†Œ๋ชจํ•œ๋‹ค.
14+
15+
# Code
16+
```go
17+
func levelOrder(root *TreeNode) [][]int {
18+
levels := make([][]int, 0)
19+
20+
if root == nil {
21+
return levels
22+
}
23+
24+
q := []*TreeNode{root}
25+
26+
for len(q) > 0 {
27+
level := make([]int, 0)
28+
currLen := len(q)
29+
for i := 0; i < currLen; i++ {
30+
front := q[0]
31+
level = append(level, front.Val)
32+
q = q[1:]
33+
34+
if front.Left != nil {
35+
q = append(q, front.Left)
36+
}
37+
if front.Right != nil {
38+
q = append(q, front.Right)
39+
}
40+
}
41+
levels = append(levels, level)
42+
}
43+
44+
return levels
45+
}
46+
47+
```
48+
49+
```go
50+
func levelOrder(root *TreeNode) [][]int {
51+
levels := make([][]int, 0)
52+
53+
if root == nil {
54+
return levels
55+
}
56+
57+
q := []*TreeNode{root}
58+
var nextQ []*TreeNode
59+
60+
for len(q) > 0 {
61+
level := make([]int, 0)
62+
for _, front := range q {
63+
level = append(level, front.Val)
64+
65+
if front.Left != nil {
66+
nextQ = append(nextQ, front.Left)
67+
}
68+
if front.Right != nil {
69+
nextQ = append(nextQ, front.Right)
70+
}
71+
}
72+
q, nextQ = nextQ, q[:0]
73+
74+
levels = append(levels, level)
75+
}
76+
77+
return levels
78+
}
79+
80+
```
81+
- ์ฒซ ๋ฒˆ์งธ ์ฝ”๋“œ๋Š” `q[1:]`์„ ์ด์šฉํ•ด์„œ ํ์˜ `pop()`์„ ๊ตฌํ˜„ํ•œ๋‹ค. ํ•˜์ง€๋งŒ GoLang์—์„œ๋Š” `pop()`์„ ํ•œ๋‹ค๊ณ ํ•ด์„œ, ์ฐธ์กฐ๊ฐ€ ํ•ด์ œ๋˜์ง€ ์•Š์•„ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๊ณ„์† ์žก์•„๋จน๋Š”๋‹ค.
82+
- `pop()`์„ ํ•˜๋”๋ผ๋„ `q`๊ฐ€ ์ˆœํšŒํ•˜๋Š” ๋ชจ๋“  ์›์†Œ๋“ค์„ ์ฐธ์กฐํ•˜๊ณ  ์žˆ๋‹ค.
83+
- ๋‘ ๋ฒˆ์งธ ์ฝ”๋“œ๋Š” `q, nextQ = nextQ, q[:0]`์„ ์ด์šฉํ•ด์„œ `q`์™€ `nextQ`์˜ ๋ฉ”๋ชจ๋ฆฌ ์˜์—ญ์„ ๊ต์ฒดํ•  ๋ฟ, ๋ถ€๊ฐ€์ ์ธ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์„ ํ•„์š”๋กœ ํ•˜์ง€ ์•Š์•„ ๋”์šฑ ํšจ์œจ์ ์ด๋‹ค.

โ€Žremove-nth-node-from-end-of-list/invidam.go.md

Lines changed: 0 additions & 68 deletions
This file was deleted.
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Intuition (Array)
2+
BST๋Š” ์ •๋ ฌ ๊ด€๊ณ„๋ฅผ ๋งŒ์กฑํ•˜๋ฏ€๋กœ, ์ˆœ์„œ์— ๋งž๊ฒŒ ๋ฐฐ์—ด์— ์ €์žฅํ•œ ํ›„ ์ •๋ ฌ ๊ด€๊ณ„๊ฐ€ ๋งž๋Š”์ง€ ๋น„๊ตํ–ˆ๋‹ค.
3+
# Approach
4+
<!-- Describe your approach to solving the problem. -->
5+
1. ์ค‘์œ„ ์ˆœํšŒ(์ขŒ --> ๋ฃจํŠธ --> ์šฐ)๋ฅผ ํ•˜๋ฉฐ ๋ฐฐ์—ด์„ ์ฑ„์šด๋‹ค. (`fillNodes()`)
6+
2. ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉฐ ์ •๋ ฌ ๊ด€๊ณ„๊ฐ€ ๋ฒ—์–ด๋‚ฌ๋Š”์ง€ ํŒ๋ณ„ํ•œ๋‹ค. (`if nodes[i] >= nodes[i+1]`)
7+
8+
# Complexity
9+
- Time complexity: $O(n)$
10+
- ํŠธ๋ฆฌ์˜ ์›์†Œ๋ฅผ n๊ฐœ๋ผ๊ณ  ํ–ˆ์„ ๋•Œ, ๋ชจ๋“  ์›์†Œ๋ฅผ ์ˆœํšŒํ•˜๋Š” ๋น„์šฉ `O(n)`์ด ์†Œ๋ชจ๋œ๋‹ค.
11+
- Space complexity: $O(n)$
12+
- ํŠธ๋ฆฌ์˜ ์›์†Œ๋ฅผ n๊ฐœ๋ผ๊ณ  ํ–ˆ์„ ๋•Œ, ๋ชจ๋“  ์›์†Œ๋“ค์„ ์ €์žฅํ•˜๋Š” ๋ฐฐ์—ด์ด `O(n)`์„ ์†Œ๋ชจํ•œ๋‹ค.
13+
14+
# Code
15+
```go
16+
func fillNodes(root *TreeNode) []int {
17+
nodes := make([]int, 0)
18+
if root.Left != nil {
19+
nodes = append(nodes, fillNodes(root.Left)...)
20+
}
21+
nodes = append(nodes, root.Val)
22+
if root.Right != nil {
23+
nodes = append(nodes, fillNodes(root.Right)...)
24+
}
25+
return nodes
26+
}
27+
28+
func isValidBST(root *TreeNode) bool {
29+
nodes := fillNodes(root)
30+
for i := 0; i < len(nodes)-1; i++ {
31+
if nodes[i] >= nodes[i+1] {
32+
return false
33+
}
34+
}
35+
return true
36+
}
37+
38+
```
39+
# Intuition (Recursion)
40+
์˜ˆ์ œ๋ฅผ ์ฐธ๊ณ ํ–ˆ์„ ๋•Œ, BST์˜ ๋ฒ”์œ„(์ตœ์†Œ, ์ตœ๋Œ€)๋ฅผ ๋ฒ—์–ด๋‚˜์ง€ ์•Š๋Š”์ง€๋ฅผ ์œ ์ง€ํ•˜๋ฉด ํŒ๋ณ„ํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค.
41+
# Approach
42+
<!-- Describe your approach to solving the problem. -->
43+
1. ์ „์œ„ ์ˆœํšŒ(๋ฃจํŠธ --> ์ขŒ --> ์šฐ)๋ฅผ ํ•œ๋‹ค.
44+
2. ํ•ด๋‹น ๋…ธ๋“œ์—์„œ ์ฃผ์–ด์ง„ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜๋Š”์ง€ ํŒ๋ณ„ํ•œ๋‹ค. (`if !(min < root.Val && root.Val < max)`)
45+
3. ์ž์‹ ๋…ธ๋“œ๋“ค์— ๋Œ€ํ•ด์„œ๋„ BST๊ฐ€ ๋งŒ์กฑํ•˜๋Š”์ง€ ์žฌ๊ท€ํ•จ์ˆ˜ ํ˜ธ์ถœ์„ ํ†ตํ•ด ํŒ๋ณ„ํ•œ๋‹ค.
46+
- ๊ฐ€๋Šฅํ•œ ๋ฒ”์œ„๋Š” ํ•ด๋‹น ๋ฃจํŠธ ๋…ธ๋“œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๊ฐฑ์‹ ํ•œ๋‹ค.
47+
# Complexity
48+
- Time complexity: $O(n)$
49+
- ํŠธ๋ฆฌ์˜ ์›์†Œ๋ฅผ n๊ฐœ๋ผ๊ณ  ํ–ˆ์„ ๋•Œ, ๋ชจ๋“  ์›์†Œ๋ฅผ ์ˆœํšŒํ•˜๋Š” ๋น„์šฉ `O(n)`์ด ์†Œ๋ชจ๋œ๋‹ค.
50+
- Space complexity: $O(n)$
51+
- ํŠธ๋ฆฌ์˜ ์›์†Œ๋ฅผ n๊ฐœ๋ผ๊ณ  ํ–ˆ์„ ๋•Œ, ํŠธ๋ฆฌ์˜ ๋†’์ด๋งŒํผ ์ฝœ ์Šคํƒ์ด ๋ฐœ์ƒํ•˜์—ฌ ๋ณต์žก๋„๊ฐ€ ์†Œ๋ชจ๋œ๋‹ค.
52+
- ์ตœ์•…์˜ ๊ฒฝ์šฐ(`skewed tree`), ๋†’์ด๋Š” `n`๊ฐœ์ด๋ฏ€๋กœ `O(n)`์„ ์†Œ๋ชจํ•œ๋‹ค.
53+
- ์ตœ์„ ์˜ ๊ฒฝ์šฐ(`perfect binary tree`), ๋†’์ด๋Š” `log(n)`๊ฐœ ์ด๋ฏ€๋กœ `O(log(n))`์„ ์†Œ๋ชจํ•œ๋‹ค.
54+
55+
# Code
56+
```go
57+
func isValidBSTFrom(root *TreeNode, min, max int) bool {
58+
if root == nil {
59+
return true
60+
}
61+
if !(min < root.Val && root.Val < max) {
62+
return false
63+
}
64+
return isValidBSTFrom(root.Left, min, root.Val) && isValidBSTFrom(root.Right, root.Val, max)
65+
}
66+
67+
func isValidBST(root *TreeNode) bool {
68+
return isValidBSTFrom(root, math.MinInt, math.MaxInt)
69+
}
70+
71+
```
72+
73+
# Learned
74+
- `math` ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ `MinInt`, `MaxInt` ์‚ฌ์šฉ๋ฒ•
75+
- `MinInt`๋Š” `-1 << 31`๋กœ ๊ณ„์‚ฐํ•œ๋‹ค.
76+
- ์™œ `-`์„ ์‰ฌํ”„ํŠธํ•˜๋Š”์ง€ ๊ถ๊ธˆํ•ด์„œ ์ฐพ์•„๋ดค๋‹ค.
77+
- ๋น„ํŠธ ๋งจ๋’ค๊ฐ€ 1์ธ ์ˆ˜๋“ค์€ `<< 31`์„ ํ•˜๋ฉด ๋ชจ๋‘ `MinInt`๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์ง€๋งŒ, `1 << 31`๋กœ `MaxInt`์™€ ๋Œ€๋น„๋˜๋„๋ก `-1`์„ ์„ ํƒํ•œ ๊ฒƒ ๊ฐ™๋‹ค. ๋‹ค๋ฅธ ์ˆ˜๋“ค์— ๋น„ํ•ด ์ข€ ๋” ์ง๊ด€์ ์ด๊ธฐ๋„ ํ•˜๋‹ค.
78+
79+
- `...` operator (Three dots, Ellipsis)
80+
- unpacking์„ ํ•˜๋Š” ์šฉ๋„์ด๋‹ค.
81+
- ์‚ฌ๋ก€
82+
- ํ•จ์ˆ˜ ์ธ์ž๋กœ ๋ช‡ ๊ฐœ์ธ์ง€ ์•Œ ์ˆ˜ ์—†๋Š” ์ธ์ž๋“ค์ด ๋“ค์–ด์˜ฌ ๋•Œ
83+
- ๋ฐฐ์—ด ์ดˆ๊ธฐํ™” ์‹œ ๊ธธ์ด๋ฅผ ๋ชฐ๋ผ ์ปดํŒŒ์ผ๋Ÿฌ์—๊ฒŒ ๋งก๊ธฐ๊ณ  ์‹ถ์„ ๋•Œ
84+
- ์ฐธ๊ณ : [๋งํฌ](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 commit comments

Comments
ย (0)