Skip to content

Commit

Permalink
Merge pull request #92 from Invidam/week04-invidam
Browse files Browse the repository at this point in the history
[Vidam, Invidam] Week 4 Solution
  • Loading branch information
SamTheKorean authored May 26, 2024
2 parents dc33916 + 2f63915 commit 8d0f2c5
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 0 deletions.
45 changes: 45 additions & 0 deletions counting-bits/invidam.go.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
์ด์ „ ๊ฐ’๋“ค์„ ์žฌํ™œ์šฉํ•œ๋‹ค.
# Approach
<!-- Describe your approach to solving the problem. -->
1. ์—ฃ์ง€ ์ผ€์ด์Šค๋Š” 0์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
2. 0, 1์„ ๋ฏธ๋ฆฌ ๊ณ„์‚ฐํ•œ๋‹ค.
3. `>>`์„ ์ˆ˜ํ–‰ํ•œ ๊ฒฐ๊ณผ + ์ง/ํ™€ ์—ฌ๋ถ€๋กœ ์ธํ•œ 1์„ ๋”ํ•ด์„œ ํ•ด๊ฒฐํ•ด์ค€๋‹ค.
- ์ด์ง„์ˆ˜ `1001`์˜ ๊ฒฝ์šฐ `100` ๊ณ„์‚ฐํ•œ ๊ฒฐ๊ด๊ฐ’์—์„œ `1`์„ ๋”ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.
- ์ด์ง„์ˆ˜ `1010`์˜ ๊ฒฝ์šฐ `101` ๊ณ„์‚ฐํ•œ ๊ฒฐ๊ด๊ฐ’์—์„œ `0`์„ ๋”ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

- ์†”๋ฃจ์…˜ ์ฐธ๊ณ : `i & (i-1)` ์—ฐ์‚ฐ์„ ํ†ตํ•ด ๊ณ„์‚ฐํ•œ๋‹ค.
- 2์˜ ์ œ๊ณฑ์ˆ˜์ธ ๊ฒฝ์šฐ `0`์ด ๋‚˜์™€ 1์„ ๋”ํ•˜๋ฉด ๋œ๋‹ค.
- ์•„๋‹Œ ๊ฒฝ์šฐ๋Š” ์•„์ง์€ ์ž˜ ๋ชจ๋ฅด๊ฒ ๋‹ค.
# Complexity
- Time complexity: $$O(n)$$
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
:`n`ํฌ๊ธฐ์˜ ๋ฐฐ์—ด์„ ๋ชจ๋‘๋ฅผ ์ˆœํšŒํ•œ๋‹ค.
- Space complexity: $$O(n)$$
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
:ํฌ๊ธฐ `n`์˜ ๋ฐฐ์—ด์„ ์„ ์–ธํ•œ๋‹ค.
# Code
```go
func countBits(n int) []int {
if n == 0 {
return []int{0}
}
ans := make([]int, n+1, n+1)

ans[0], ans[1] = 0, 1

for i := 2; i <= n; i++ {
ans[i] = ans[i>>1] + i&1
}
return ans
}

func countBitsSolution(n int) []int {
res := make([]int, n+1)
for i := 1; i <= n; i++ {
res[i] = res[i&(i-1)] + 1
}
return res
}
```
87 changes: 87 additions & 0 deletions group-anagrams/invidam.go.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# Intuition
์ •๋ ฌ๋œ ๋ฌธ์ž์—ด์„ ํ†ตํ•ด ๊ทธ๋ฃน ์—ฌ๋ถ€๋ฅผ ์‰ฝ๊ฒŒ ํ™•์ธํ•œ๋‹ค.
# Approach
1. `{์ •๋ ฌ๋œ ๋ฌธ์ž์—ด, ๊ทธ ๋ฌธ์ž์—ด์˜ ์ธ๋ฑ์Šค}`๋ฅผ ์œ ์ง€ํ•˜๋Š” ๋ฐฐ์—ด์„ ์„ ์–ธํ•œ๋‹ค.
2. ์›๋ณธ ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉฐ ์ •๋ ฌํ•œ ๊ฒฐ๊ณผ, ์ธ๋ฑ์Šค๋ฅผ struct๋กœ ํ•˜์—ฌ ๋ฐฐ์—ด์— ์‚ฝ์ž…ํ•œ๋‹ค.
- ์ธ๋ฑ์Šค๋ฅผ ์œ ์ง€ํ•˜๋Š” ์ด์œ ๋Š” ์›๋ณธ ๋ฐฐ์—ด์˜ ๋ฌธ์ž์—ด ๊ฐ’์„ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•จ์ด๋‹ค. (์ •๋ ฌ์„ ํ–ˆ๊ธฐ์— ์ด๋ ‡์ง€ ์•Š์œผ๋ฉด ํ™•์ธ์ด ์–ด๋ ต๋‹ค.)
3. ๋ชจ๋“  struct๊ฐ€ ์‚ฝ์ž…๋œ ๋ฐฐ์—ด์„ ๋ฌธ์ž์—ด์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค.
4. ๋ฐ˜๋ณต๋ฌธ์„ ์ˆœํšŒํ•˜๋ฉฐ `์ด์ „ ๋ฌธ์ž์—ด๊ณผ ๊ฐ™์€์ง€` ์—ฌ๋ถ€๋ฅผ ๊ทธ๋ฃน์„ ํŒ๋‹จํ•˜์—ฌ ์ธ๋ฑ์Šค๋“ค์„ ๊ทธ๋ฃจํ•‘ํ•ด ๋ชจ์•„๋†“๋Š”๋‹ค.
5. ๊ทธ๋ฃจํ•‘ํ•œ ์ธ๋ฑ์Šค๋“ค์„ ๋ฌธ์ž์—ด(์›๋ณธ ๋ฌธ์ž์—ด์„ ์ฐธ์กฐํ•ด์„œ)๋กœ ์น˜ํ™˜ํ•˜์—ฌ ์ตœ์ข…์ ์œผ๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
# Complexity
- Time complexity: $$O(nklog(n))$$
<!-- Add your time complexity here, e.g. $$O(nklog(n))$$ -->
: ๋ฐฐ์—ด์˜ ๊ธธ์ด `n`, ๋ฌธ์ž์—ด์˜ ๊ธธ์ด `k`. ๋ฌธ์ž์—ด์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•  ๋•Œ ๋ฐœ์ƒํ•˜๋Š” ๋น„์šฉ์ด๋‹ค.
- Space complexity: $$O(nk)$$

: ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์˜ ๊ธธ์ด `n`, ๋ฐฐ์—ด์ด ๊ฐ€์ง€๋Š” ๋ฌธ์ž์—ด์˜ ๊ธธ์ด `k`, (์ฝ”๋“œ๋กœ ์ƒ์„ฑํ•˜๋Š” ๋ฐฐ์—ด์˜ ๊ธธ์ด, ๋ฌธ์ž์—ด์˜ ํฌ๊ธฐ ๋ชจ๋‘ `n`, `k`์ด๋‹ค.)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```go
type StrAndIdx struct {
Str string
Idx int
}

func sortSring(s string) string {
runes := []rune(s)

sort.Slice(runes, func(i, j int) bool {
return runes[i] < runes[j]
})

return string(runes)
}

func groupAnagrams(strs []string) [][]string {

strAndIdxs := make([]StrAndIdx, 0, len(strs)+5)

for i, s := range strs {
strAndIdxs = append(strAndIdxs, StrAndIdx{sortSring(s), i})
}

sort.Slice(strAndIdxs, func(i, j int) bool {
return strAndIdxs[i].Str < strAndIdxs[j].Str
})

groupedIdxs := make([][]int, 0, len(strAndIdxs)/4)

group := make([]int, 0)
defaultString := "NOT_EXIST_STRING"
prev := defaultString
for _, sAI := range strAndIdxs {
curr := sAI.Str
idx := sAI.Idx
if prev == curr {
group = append(group, idx)
} else {
if prev != defaultString {
groupedIdxs = append(groupedIdxs, group)
}
group = []int{idx}
}
prev = curr
}

if len(group) != 0 {
groupedIdxs = append(groupedIdxs, group)
}

groupedStrs := make([][]string, 0, len(groupedIdxs))
for _, idxs := range groupedIdxs {

groupStr := make([]string, 0, len(idxs))
for _, idx := range idxs {
groupStr = append(groupStr, strs[idx])
}
groupedStrs = append(groupedStrs, groupStr)
}

return groupedStrs
}
```

# **To** Learn
- GoLang์—์„œ ๋ฌธ์ž์—ด์„ ์–ด๋–ป๊ฒŒ ๋น„๊ตํ•˜๋Š”์ง€.
- ํ•ด๋‹น ๋ฌธ์ œ ํ•ด๊ฒฐ์— ์žˆ์–ด์„œ ์‚ฝ์งˆํ•œ ๊ฒƒ์€ ๋ฌด์—‡์ด์—ˆ๋Š”์ง€. ์•ž์œผ๋กœ ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ์•ˆํ•  ์ˆ˜ ์žˆ๋Š”์ง€.
36 changes: 36 additions & 0 deletions missing-number/invidam.go.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
๋ฐฐํƒ€์ ์ธ ๊ฒฝ์šฐ์—๋งŒ ์ฐธ์„ ์ถœ๋ ฅํ•˜๋Š” `XOR` ์—ฐ์‚ฐ์„ ํ™œ์šฉํ•œ๋‹ค.
# Approach
<!-- Describe your approach to solving the problem. -->
- ๋ฌธ์ œ๋ฅผ ์น˜ํ™˜ํ•ด๋ณด์ž. ๋ชจ๋“  ์ˆ˜๊ฐ€ 2๋ฒˆ์”ฉ ๋“ฑ์žฅํ•˜๊ณ , ํ•˜๋‚˜์˜ ์ˆ˜๋งŒ ํ•œ ๋ฒˆ ๋“ฑ์žฅํ•œ๋‹ค๊ณ  ํ•˜์ž.
- ์ด ๊ฒฝ์šฐ, ๋ชจ๋“  ์ˆ˜๋“ค์— `^`์—ฐ์‚ฐ์„ ํ•˜๋ฉด ํ•œ ๋ฒˆ ๋“ฑ์žฅํ•œ ์ˆ˜๋งŒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.
- `๋ฐฐ์—ด์˜ ๊ธธ์ด` + `์ธ๋ฑ์Šค`์™€ `ํ•ด๋‹น ์ธ๋ฑ์Šค์ผ ๋•Œ์˜ ์›์†Œ๊ฐ’`๋“ค์„ ๋ชจ๋‘ `^`์—ฐ์‚ฐํ•˜๋ฉด
- ๋ฐฐ์—ด + ์ธ๋ฑ์Šค ์ˆœํšŒ๋กœ ๋ชจ๋“  ์ˆ˜๋Š” 1๋ฒˆ์”ฉ ๋“ฑ์žฅํ•œ๋‹ค.
- ์›์†Œ๊ฐ’ ์ˆœํšŒ๋กœ ํ•˜๋‚˜์˜ ์ˆ˜๋ฅผ ์ œ์™ธํ•˜๊ณค 1๋ฒˆ์”ฉ ๋“ฑ์žฅํ•œ๋‹ค.
- ํ•œ ๋ฒˆ๋งŒ ๋“ฑ์žฅํ•œ ์ˆ˜๊ฐ€ missing number์ด๋‹ค.
# Complexity
- Time complexity: $$O(n)$$
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
: ๋ฐฐ์—ด์˜ ๊ธธ์ด `n`, ์ด๋ฅผ ์ˆœํšŒํ•œ๋‹ค.
- Space complexity: $$O(n)$$

: inline, ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์˜ ๊ธธ์ด `n`
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```go
func missingNumber(nums []int) int {
num := len(nums)

for i, n := range nums {
num ^= i
num ^= n
}
return num
}
```

# Today I Learned
- ๋‹ค๋ฅธ ํ’€์ด๋กœ ํ’€ ๋•Œ, `abs()`๋ฅผ ์ด์šฉํ•ด์„œ ํ•ด๊ฒฐํ•˜๋ ค๊ณ  ํ•˜์˜€๋Š”๋ฐ ํ•ด๋‹น ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์•˜๋‹ค. ์™œ ๊ทธ๋Ÿฐ์ง€ ์ฐพ์•„๋ดค๋Š”๋ฐ ๋„ˆ๋ฌด ๊ฐ„๋‹จํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ์ฒ ํ•™์— ์•ˆ๋งž๋Š”๋‹ค๊ณ  ์•ˆ๋„ฃ์–ด์ค€๋‹ค ํ•˜๋”๋ผ.
- ์ฐธ๊ณ : https://go.dev/doc/faq#x_in_std
29 changes: 29 additions & 0 deletions number-of-1-bits/invidam.go.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Intuition
๋น„ํŠธ ์—ฐ์‚ฐ์ž๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๊ณ„์‚ฐํ•œ๋‹ค.
# Approach
1. `n`๊ณผ ๋น„๊ตํ•˜๊ธฐ ์œ„ํ•œ `mask`๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
2. `mask`๋ฅผ 2์ง„์ˆ˜ `1`, `10`, `100` ์ˆœ์œผ๋กœ ์ฆ๊ฐ€์‹œํ‚จ ํ›„ `&` ์—ฐ์‚ฐ์ž๋ฅผ ํ™œ์šฉํ•ด, n์˜ ํŠน์ • ๋น„ํŠธ๊ฐ€ `1`์ธ์ง€ ํŒ๋‹จํ•œ๋‹ค.
3. ์ฐธ์ธ ๊ฒฝ์šฐ `cnt`๋ฅผ ์ฆ๊ฐ€์‹œ์ผœ ๊ฒฐ๊ณผ๋ฅผ ๊ตฌํ•œ๋‹ค.

# Complexity
- Time complexity: $$O(logn)$$
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
: `mask`๊ฐ€ ์žˆ๋Š” ๋ฐ˜๋ณต๋ฌธ์— ์˜ํ•ด ๊ฒฐ์ •๋œ๋‹ค. `mask`๋Š” ์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด์˜จ `n`๊นŒ์ง€ 2๋ฐฐ์”ฉ ์ฆ๊ฐ€ํ•˜๋ฏ€๋กœ $$O(logn)$$์ด๋‹ค.
- Space complexity: $$O(1)$$
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
: ๋ณ„๋„ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.
# Code
```go
func hammingWeight(n int) int {
cnt := 0
for mask := 1; mask <= n; mask = mask << 1 {
if (mask & n) != 0 {
cnt += 1
}
}
return cnt
}
```

# What I learned
- ๊ณ ์–ธ์–ด์—์„œ ์ฒซ ๋น„ํŠธ ์—ฐ์‚ฐ์ž ํ™œ์šฉ

0 comments on commit 8d0f2c5

Please sign in to comment.