Skip to content

[채승윤] 4주차 제출합니다 #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions week-04/counting-bits/chaeseungyun.py.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
이진수로 변환 후 1의 개수를 센다
# Approach
<!-- Describe your approach to solving the problem. -->
1. 파이썬의 bin 함수를 통해 이진수 문자열로 변환
2. 반복문을 통해 각 리스트의 1의 개수를 센다

# Complexity
- Time complexity: $$O(n)$$
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: $$O(n)$$
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```python
class Solution:
def countBits(self, n: int) -> List[int]:
result = []

for i in range(n + 1):
result.append(bin(i).count('1'))

return result
```

# learn
bin()이라는 사기적인 메소드를 배웠다.

bin은 십진수를 이진수의 문자열로 변환해준다.
count(x)는 x가 해당 문자열에서 몇 번 나타났는데 세어준다.

편안하다~
36 changes: 36 additions & 0 deletions week-04/group-anagrams/chaeseungyun.py.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Intuition
정렬을 통해 묶여질 수 있는 문자열을 찾고 그룹별로 카운트한다. 해시 테이블로 관리하면 좋겠다.

# Approach
1. 리스트를 순회하며 각 문자열을 정렬 후 딕셔너리에 저장한다.
2. 이미 있는 key라면 값을 추가하고 아니면 새롭게 만든다.
3. 딕셔너리의 value를 꺼내면 그것이 바로 정답

# Complexity
- Time complexity: $$O(nlogn)$$
반복문 n * 파이썬의 정렬 log n
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: $$O(n)$$
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
딕셔너리 크기만큼 차지할 것이다.

# Code
```python
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
word = {}

for str in strs:
sortedStr = ''.join(sorted(str))
if (sortedStr in word):
word[sortedStr].append(str)
else:
word[sortedStr] = [str]

return word.values()
```

# learned
딕셔너리의 key로 list를 사용할 수 없다는 것을 배웠다. immutable만 값만 들어갈 수 있다.
join이라는 메소드를 배웠다. 리스트를 문자열로 변환할 수 있다.
32 changes: 32 additions & 0 deletions week-04/missing-number/chaeseungyun.py.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Intuition
리스트를 정렬하면 숫자가 순서대로 배치되는데 이때 인덱스를 이용하면 될 것 같다.

# Approach
1. 어느 숫자가 비었는지 접근하기 쉽도록 리스트를 정렬한다.
2. emnumerate를 이용해서 인덱스와 값을 가져온다.
3. 인덱스와 값이 일치하지 않으면 해당하는 인덱스의 값이 부재인 것이다.
4. 따라서 인덱스를 반환한다.
5. 반복문 내에서 찾지 못하면 마지막값이 부재인 것이다.
6. 따라서 리스트의 길이를 반환한다.

# Complexity
- Time complexity: $$O(n)$$
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: $$O(n)$$
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```python
class Solution:
def missingNumber(self, nums: List[int]) -> int:
nums.sort()

for i, num in enumerate(nums):
if (num != i): return i

return len(nums)
```

# learned
정렬된 리스트는 순서가 보장되어 있으므로 인덱스로 쉽게 찾을 수 있었다.
23 changes: 23 additions & 0 deletions week-04/number-of-1-bits/chaeseungyun.py.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Intuition
파이썬 메소드를 활용한다.

# Approach
1. 파이썬 메소드를 활용해 이진수 문자열로 만들고 1의 개수를 센다.

# Complexity
- Time complexity: $$O(n)$$
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
count() 함수가 문자열 내부를 돌 것이므로 n
- Space complexity: $$O(1)$$
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```python
class Solution:
def hammingWeight(self, n: int) -> int:
return bin(n).count('1')
```

# learned
bin() 메소드는 십진수를 이진수 문자열로 변환해준다.
count()는 문자열 내에 해당 문자가 몇 번 나타났는지 보여준다.
23 changes: 23 additions & 0 deletions week-04/reverse-bit/chaeseungyun.py.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Intuition
파이썬 메소드를 활용한다.

# Approach
1. input을 32비트 이진수 형태의 문자열로 저장한다.
2. 슬라이싱을 통해 문자열을 뒤집고 십진수로 변환한다.

# Complexity
- Time complexity: $$O(n)$$
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: $$O(1)$$
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```python
class Solution:
def reverseBits(self, n: int) -> int:
return int(format(n, '032b')[::-1], 2)
```

# learned
format() 이라는 메소드를 배웠다. 숫자를 문자열로 바꿀 수 있는 문자열인데, 두 번째 인자로 형식을 줄 수 있다. 코드에선 32비트의 이진수의 형태로 변환했다.