-
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.
Merge pull request #92 from Invidam/week04-invidam
[Vidam, Invidam] Week 4 Solution
- Loading branch information
Showing
4 changed files
with
197 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,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 | ||
} | ||
``` |
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,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์์ ๋ฌธ์์ด์ ์ด๋ป๊ฒ ๋น๊ตํ๋์ง. | ||
- ํด๋น ๋ฌธ์ ํด๊ฒฐ์ ์์ด์ ์ฝ์งํ ๊ฒ์ ๋ฌด์์ด์๋์ง. ์์ผ๋ก ์ด๋ป๊ฒ ํด์ผ ์ํ ์ ์๋์ง. |
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,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 |
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,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 | ||
- ๊ณ ์ธ์ด์์ ์ฒซ ๋นํธ ์ฐ์ฐ์ ํ์ฉ |