Skip to content

Commit 88bc280

Browse files
committed
week5 solution update
1 parent cf3291d commit 88bc280

File tree

5 files changed

+294
-0
lines changed

5 files changed

+294
-0
lines changed

β€Ž3sum/invidam.go.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Intuition
2+
μ˜ˆμ „μ— ν’€μ–΄λ΄€λ˜ λ¬Έμ œμ˜€μ–΄μ„œ 접근법을 μ•Œκ³ μžˆμ—ˆλ‹€.
3+
4+
# Approach
5+
1. 정렬을 ν•˜μ—¬ λ°°μ—΄μ˜ λŒ€μ†Œκ΄€κ³„λ₯Ό μΌμ •ν•˜κ²Œ ν•œλ‹€.
6+
2. i,j,kλ₯Ό μ„€μ •ν•΄μ•Ό ν•˜λŠ”λ°, i < j < k이도둝 ν•œλ‹€.
7+
3. iλŠ” 맨 μ•žμ—μ„œλΆ€ν„° μˆœνšŒν•œλ‹€.
8+
4. jλŠ” i의 λ’€λΆ€ν„° μˆœνšŒν•œλ‹€.
9+
5. kλŠ” 맨 λ’€μ—μ„œλΆ€ν„° μˆœνšŒν•œλ‹€.
10+
6. μ„Έ μΈλ±μŠ€κ°€ κ°€λ¦¬ν‚€λŠ” κ°’λ“€μ˜ 합을 λΉ„κ΅ν•˜μ—¬ j와 kλ₯Ό μˆ˜μ •ν•œλ‹€.
11+
12+
# Complexity
13+
- Time complexity: $$O(n^2)$$
14+
- μž…λ ₯ λ°°μ—΄μ˜ 길이 n에 λŒ€ν•˜μ—¬,
15+
16+
- Space complexity: $$O(n)$$
17+
- μž…λ ₯으둜 λ“€μ–΄μ˜¨ λ°°μ—΄μ˜ 길이 n에 λŒ€ν•˜μ—¬, μƒμ„±ν•˜λŠ” κ²°κ³Ό λ°°μ—΄μ˜ 길이 μ—­μ‹œ 이와 λ™μΌν•˜λ‹€.
18+
# Code
19+
20+
```go
21+
func update(i int, j int, k int, sum int, nums []int) (int, int) {
22+
if sum <= 0 {
23+
j++
24+
for j < len(nums) && j >= 1 && nums[j] == nums[j-1] {
25+
j++
26+
}
27+
} else {
28+
k--
29+
for k >= 0 && k+1 < len(nums) && nums[k] == nums[k+1] {
30+
k--
31+
}
32+
}
33+
34+
return j, k
35+
}
36+
37+
func threeSum(nums []int) [][]int {
38+
var triplets [][]int
39+
40+
sort.Ints(nums)
41+
for i := 0; i < len(nums); i++ {
42+
j, k := i+1, len(nums)-1
43+
44+
if i != 0 && nums[i-1] == nums[i] {
45+
continue
46+
}
47+
48+
for j < k {
49+
sum := nums[i] + nums[j] + nums[k]
50+
if sum == 0 {
51+
triplets = append(triplets, []int{nums[i], nums[j], nums[k]})
52+
}
53+
j, k = update(i, j, k, sum, nums)
54+
}
55+
}
56+
return triplets
57+
}
58+
59+
```
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Intuition
2+
κ΅¬λΆ„μžλ₯Ό λ„£μ–΄ 였λ₯˜λ₯Ό λ°©μ§€ν•œλ‹€.
3+
# Approach
4+
<!-- Describe your approach to solving the problem. -->
5+
1. UTF-8에 λ²—μ–΄λ‚˜λŠ” κ΅¬λΆ„μž `γ„±`을 λ„£μ–΄ κ΅¬λΆ„ν–ˆλ‹€.
6+
# Complexity
7+
- Time complexity: $$O(n)$$
8+
- λ¬Έμžμ—΄μ˜ 길이 n에 λŒ€ν•˜μ—¬, 이λ₯Ό μˆœνšŒν•˜λŠ” λΉ„μš©μ΄ λ°œμƒν•œλ‹€.
9+
10+
- Space complexity: $$O(n)$$
11+
- λ¬Έμžμ—΄μ˜ 길이 n에 λŒ€ν•˜μ—¬, `encoded`λ₯Ό λ§Œλ“œλŠ” 곡간이 λ°œμƒν•œλ‹€.
12+
13+
# Code
14+
```go
15+
func encodeV1(strs []string) string {
16+
ret := strings.Join(strs, string(DIVIDE_CHAR_V1))
17+
return ret
18+
}
19+
20+
func decodeV1(encoded string) []string {
21+
sep := string(DIVIDE_CHAR_V1)
22+
return strings.Split(encoded, sep)
23+
}
24+
```
25+
- - -
26+
# Intuition
27+
μ†”λ£¨μ…˜μ—μ„œ λ„€νŠΈμ›Œν¬ 톡신을 μœ„ν•œλ‹€λŠ” λͺ©μ μ„ λ“£κ³  μˆ˜μ •ν•΄λ³΄μ•˜λ‹€. (μœ λ‹ˆμ½”λ“œλ₯Ό λ„£λŠ” 게 μ˜λ„λŠ” μ•„λ‹Œ λ“―ν–ˆλ‹€.)
28+
# Approach
29+
1. κ΅¬λΆ„μž (`-`)와 이전 문자의 길이λ₯Ό ν•¨κ»˜ μ €μž₯ν•œλ‹€.
30+
2. κ΅¬λΆ„μžκ°€ λ‚˜μ˜¨λ‹€λ©΄, λ¬Έμžμ—΄μ˜ 길이λ₯Ό μΆ”μΆœν•˜μ—¬ λ¬Έμžμ—΄μ„ λ””μ½”λ”©ν•œλ‹€.
31+
3. μœ„ 과정을 배열을 μˆœνšŒν•˜λ©° λ°˜λ³΅ν•œλ‹€.
32+
# Complexity
33+
- Time complexity: $$O(n)$$
34+
- λ¬Έμžμ—΄μ˜ 길이 n에 λŒ€ν•˜μ—¬, 이λ₯Ό μˆœνšŒν•˜λŠ” λΉ„μš©μ΄ λ°œμƒν•œλ‹€.
35+
36+
- Space complexity: $$O(n)$$
37+
- λ¬Έμžμ—΄μ˜ 길이 n에 λŒ€ν•˜μ—¬, `encoded`λ₯Ό λ§Œλ“œλŠ” 곡간이 λ°œμƒν•œλ‹€.
38+
39+
# Code
40+
```go
41+
func encode(strs []string) string {
42+
ret := ""
43+
for _, str := range strs {
44+
ret += str
45+
ret += fmt.Sprintf("%c%04d", DIVIDE_CHAR, len(str))
46+
// a-1bcd-3
47+
}
48+
return ret
49+
}
50+
51+
func decode(encoded string) []string {
52+
ret := make([]string, 0)
53+
for i := 0; i < len(encoded); {
54+
if encoded[i] == DIVIDE_CHAR {
55+
lenStr := encoded[i+1 : i+5]
56+
len, _ := strconv.Atoi(lenStr)
57+
58+
decodeStr := encoded[i-len : i]
59+
ret = append(ret, decodeStr)
60+
i += 5
61+
} else {
62+
i += 1
63+
}
64+
}
65+
66+
return ret
67+
}
68+
```
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Intuition
2+
자기 μžμ‹  이후에 λͺ‡ κ°œκ°€ μ—°μ†λ˜μ—ˆλŠ”μ§€λ₯Ό `dp`둜 μ €μž₯ν•œλ‹€.
3+
# Approach
4+
1. λ“±μž₯ μ—¬λΆ€λ₯Ό `appear`에 κΈ°λ‘ν•œλ‹€.
5+
2. 자기 μžμ‹  이후 μ—°μ†λœ μ›μ†Œμ˜ 개수λ₯Ό λ°˜ν™˜ν•˜λŠ” ν•¨μˆ˜λ₯Ό λ§Œλ“€κ³  λ§€ λ°°μ—΄λ§ˆλ‹€ 이λ₯Ό ν˜ΈμΆœν•œλ‹€.
6+
- `dp`둜써, 자기 μžμ‹ (`from`)에 λ”°λ₯Έ 결과듀을 μœ μ§€ν•œλ‹€.
7+
- 자기 μžμ‹ μ΄ λ“±μž₯ν–ˆλŠ”μ§€ 여뢀에 따라 `0` ν˜Ήμ€ `λ‹€μŒ 연속 개수 + 1`을 λ°˜ν™˜ν•œλ‹€.
8+
3. 호좜 κ²°κ³Ό 쀑 μ΅œλŒ€λ₯Ό μ°Ύμ•„ λ°˜ν™˜ν•œλ‹€.
9+
# Complexity
10+
- Time complexity: $$O(nlog(n))$$
11+
12+
- Space complexity: $$O(n+m)$$
13+
# Code
14+
## Original
15+
```go
16+
func longestConsecutiveFrom(num int, appear map[int]bool, cache map[int]int) (ret int) {
17+
if val, ok := cache[num]; ok {
18+
return val
19+
}
20+
21+
if _, ok := appear[num]; ok {
22+
ret = longestConsecutiveFrom(num+1, appear, cache) + 1
23+
} else {
24+
ret = 0
25+
}
26+
cache[num] = ret
27+
return ret
28+
}
29+
30+
func longestConsecutive(nums []int) int {
31+
appear := make(map[int]bool, len(nums)/2)
32+
cache := make(map[int]int, len(nums)/2)
33+
for _, num := range nums {
34+
appear[num] = true
35+
}
36+
37+
var max int
38+
39+
for _, num := range nums {
40+
ret := longestConsecutiveFrom(num, appear, cache)
41+
if ret > max {
42+
max = ret
43+
}
44+
}
45+
return max
46+
}
47+
```
48+
49+
## Bottom-up
50+
```go
51+
func longestConsecutive(nums []int) int {
52+
appear := make(map[int]bool, len(nums)/2)
53+
for _, num := range nums {
54+
appear[num] = true
55+
}
56+
57+
var max int
58+
59+
for _, num := range nums {
60+
if appear[num-1] {
61+
continue
62+
}
63+
len := 1
64+
for appear[num+len] {
65+
len++
66+
}
67+
68+
if len > max {
69+
max = len
70+
}
71+
}
72+
return max
73+
}
74+
75+
```
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Intuition
2+
0의 λ“±μž₯ νšŸμˆ˜μ— 따라 경우의 μˆ˜κ°€ 달라진닀. 이λ₯Ό μ΄μš©ν•œλ‹€.
3+
# Approach
4+
(μ‚¬μ΄νŠΈμ—μ„œ Wrong Caseλ₯Ό μ œκ³΅ν•˜μ˜€κΈ°μ— λ‹€μ–‘ν•œ 경우의 수λ₯Ό λŒ€μ‘ν•  수 μžˆμ—ˆλ‹€, 쒋은 ν’€μ΄λŠ” μ•„λ‹ˆλΌκ³  μƒκ°ν•œλ‹€.)
5+
- λͺ¨λ“  수의 곱을 κ³„μ‚°ν•œ ν›„, 배열을 μˆœνšŒν•˜λ©° 자기 μžμ‹ μ„ λ‚˜λˆˆ 값을 μ €μž₯ν•œλ‹€.
6+
- 0이 λ“±μž₯ν•œ 경우, 0이 μ•„λ‹Œ λ°°μ—΄μ˜ μ›μ†Œλ“€μ€ 무쑰건 0을 μ €μž₯ν•΄μ•Ό ν•œλ‹€.
7+
- `golang`의 경우 `int`의 기본값이 0이라 아무것도 μ•ˆν•΄μ£Όλ©΄ λœλ‹€.
8+
- 0이 2회 이상 λ“±μž₯ν•œ 경우, λͺ¨λ“  μ›μ†ŒλŠ” 0이 λœλ‹€.
9+
- `golang`의 경우 길이만 μ„ μ–Έλœ `int` 배열을 λ°˜ν™˜ν•˜λ©΄ λœλ‹€.
10+
# Complexity
11+
- Time complexity: $$O(n)$$
12+
- λ°°μ—΄μ˜ 길이 n에 λŒ€ν•˜μ—¬, μˆœνšŒν•˜λŠ” λΉ„μš© n이 λ°œμƒν•œλ‹€.
13+
14+
- Space complexity: $$O(n)$$
15+
- λ°°μ—΄μ˜ 길이 n에 λŒ€ν•˜μ—¬, κ²°κ³Όλ₯Ό μ €μž₯ν•  λ°°μ—΄μ˜ 크기 n이 μ†Œλͺ¨λœλ‹€.
16+
# Code
17+
```go
18+
func productExceptSelf(nums []int) []int {
19+
answer := make([]int, len(nums))
20+
product := 1
21+
zeroCnt := 0
22+
for _, num := range nums {
23+
if num == 0 {
24+
zeroCnt++
25+
continue
26+
}
27+
product *= num
28+
}
29+
30+
if zeroCnt >= 2 {
31+
product = 0
32+
}
33+
34+
for i, num := range nums {
35+
if num == 0 {
36+
answer[i] = product
37+
} else if zeroCnt == 0 {
38+
answer[i] = product / num
39+
}
40+
}
41+
return answer
42+
}
43+
```
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Intuition
2+
λΉˆλ„μˆ˜ 계산, 정렬을 ν™œμš©ν•˜λ©΄ κ°€λŠ₯ν•˜λ‹€κ³  μƒκ°ν–ˆλ‹€. (κΉ”λ”ν•˜μ§„ μ•Šμ•„ ν•΄κ²°λ μ§€λŠ” μ˜λ¬Έμ΄μ—ˆλ‹€.)
3+
# Approach
4+
1. λΉˆλ„ 수λ₯Ό μ €μž₯ν•œλ‹€.
5+
2. μ›μ†Œλ“€μ„ λΉˆλ„μˆ˜(λ‚΄λ¦Όμ°¨μˆœ)λŒ€λ‘œ μ •λ ¬ν•œλ‹€.
6+
3. μ •λ ¬λœ μ›μ†Œ 쀑 μ•žμ—μ„œλΆ€ν„° k개λ₯Ό λ°˜ν™˜ν•œλ‹€.
7+
# Complexity
8+
- Time complexity: $$O(nlog(n))$$
9+
- λ°°μ—΄μ˜ 길이 n에 λŒ€ν•˜μ—¬, μ •λ ¬λΉ„μš©μœΌλ‘œ μ†Œλͺ¨λœλ‹€.
10+
11+
- Space complexity: $$O(n+m)$$
12+
- λ°°μ—΄μ˜ 길이 nκ³Ό μ›μ†Œμ˜ λ²”μœ„ k에 λŒ€ν•˜μ—¬, λΉˆλ„μˆ˜ μ €μž₯을 μœ„ν•΄ μ„ μ–Έν•˜λŠ” λΉ„μš© `k`와 μ •λ ¬λœ μ›μ†Œλ₯Ό μ €μž₯ν•˜λŠ” λΉ„μš© `n`이 μ†Œλͺ¨λœλ‹€.
13+
14+
# Code
15+
```go
16+
type NumAndFreq struct {
17+
Num int
18+
Freq int
19+
}
20+
21+
func topKFrequent(nums []int, k int) []int {
22+
freq := make(map[int]int, 20000)
23+
24+
for _, num := range nums {
25+
freq[num]++
26+
}
27+
28+
numAndFreqs := make([]NumAndFreq, 0, len(freq))
29+
for n, f := range freq {
30+
numAndFreqs = append(numAndFreqs, NumAndFreq{Num: n, Freq: f})
31+
}
32+
33+
sort.Slice(numAndFreqs, func(i, j int) bool {
34+
return numAndFreqs[i].Freq > numAndFreqs[j].Freq
35+
})
36+
37+
numsSortedByFreqDesc := make([]int, 0, len(nums))
38+
for _, e := range numAndFreqs {
39+
numsSortedByFreqDesc = append(numsSortedByFreqDesc, e.Num)
40+
}
41+
42+
return numsSortedByFreqDesc[0:k]
43+
}
44+
45+
```
46+
47+
# μ—¬λ‹΄
48+
- 타 μ†”λ£¨μ…˜λ“€μ„ 보며 정렬을 ν•˜μ§€ μ•Šκ³  λΉˆλ„μˆ˜λ³„λ‘œ μ›μ†Œλ“€μ„ λͺ¨μ•„놨닀가 배열을 λ§Œλ“œλŠ” 방법도 ν™•μΈν–ˆλ‹€. 직관적이진 μ•Šλ‹€κ³  λŠκΌˆλ‹€.
49+
- 문제 ν•΄κ²° 도쀑 `map`, `filter`와 같은 ν•¨μˆ˜ν˜• ν”„λ‘œκ·Έλž˜λ°μ„ ν™œμš©ν•˜λ©΄ μ’‹κ² λ‹€λŠ” 생각이 λ“€μ–΄ golangμ—μ„œλ„ 이λ₯Ό μ œκ³΅ν•˜λ‚˜ μ°Ύμ•„λ΄€λ‹€. [링크1](https://stackoverflow.com/questions/71624828/is-there-a-way-to-map-an-array-of-objects-in-go)κ³Ό [링크2](https://github.com/golang/go/issues/45955)λ₯Ό 톡해 λ‹€μ–‘ν•œ λ…Όμ˜κ°€ μžˆμ—ˆμœΌλ‚˜, μ—¬λŸ¬ 이유둜 λ„μž…ν•˜μ§€ μ•ŠμŒμ„ μ•Œκ²Œλ˜μ—ˆλ‹€.

0 commit comments

Comments
Β (0)