From 0e85a808ea9b53c1fde9c4e6a39ad3cbc9c46b1c Mon Sep 17 00:00:00 2001 From: chaeseungyun Date: Tue, 6 Aug 2024 10:22:49 +0900 Subject: [PATCH] =?UTF-8?q?4=EC=A3=BC=EC=B0=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- week-04/counting-bits/chaeseungyun.py.md | 34 +++++++++++++++++++ week-04/group-anagrams/chaeseungyun.py.md | 36 +++++++++++++++++++++ week-04/missing-number/chaeseungyun.py.md | 32 ++++++++++++++++++ week-04/number-of-1-bits/chaeseungyun.py.md | 23 +++++++++++++ week-04/reverse-bit/chaeseungyun.py.md | 23 +++++++++++++ 5 files changed, 148 insertions(+) create mode 100644 week-04/counting-bits/chaeseungyun.py.md create mode 100644 week-04/group-anagrams/chaeseungyun.py.md create mode 100644 week-04/missing-number/chaeseungyun.py.md create mode 100644 week-04/number-of-1-bits/chaeseungyun.py.md create mode 100644 week-04/reverse-bit/chaeseungyun.py.md diff --git a/week-04/counting-bits/chaeseungyun.py.md b/week-04/counting-bits/chaeseungyun.py.md new file mode 100644 index 0000000..0196b3e --- /dev/null +++ b/week-04/counting-bits/chaeseungyun.py.md @@ -0,0 +1,34 @@ +# Intuition + +이진수로 변환 후 1의 개수를 센다 +# Approach + +1. 파이썬의 bin 함수를 통해 이진수 문자열로 변환 +2. 반복문을 통해 각 리스트의 1의 개수를 센다 + +# Complexity +- Time complexity: $$O(n)$$ + + +- Space complexity: $$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가 해당 문자열에서 몇 번 나타났는데 세어준다. + +편안하다~ \ No newline at end of file diff --git a/week-04/group-anagrams/chaeseungyun.py.md b/week-04/group-anagrams/chaeseungyun.py.md new file mode 100644 index 0000000..0f6c1a2 --- /dev/null +++ b/week-04/group-anagrams/chaeseungyun.py.md @@ -0,0 +1,36 @@ +# Intuition +정렬을 통해 묶여질 수 있는 문자열을 찾고 그룹별로 카운트한다. 해시 테이블로 관리하면 좋겠다. + +# Approach +1. 리스트를 순회하며 각 문자열을 정렬 후 딕셔너리에 저장한다. +2. 이미 있는 key라면 값을 추가하고 아니면 새롭게 만든다. +3. 딕셔너리의 value를 꺼내면 그것이 바로 정답 + +# Complexity +- Time complexity: $$O(nlogn)$$ +반복문 n * 파이썬의 정렬 log n + + +- Space complexity: $$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이라는 메소드를 배웠다. 리스트를 문자열로 변환할 수 있다. \ No newline at end of file diff --git a/week-04/missing-number/chaeseungyun.py.md b/week-04/missing-number/chaeseungyun.py.md new file mode 100644 index 0000000..41e49f2 --- /dev/null +++ b/week-04/missing-number/chaeseungyun.py.md @@ -0,0 +1,32 @@ +# Intuition +리스트를 정렬하면 숫자가 순서대로 배치되는데 이때 인덱스를 이용하면 될 것 같다. + +# Approach +1. 어느 숫자가 비었는지 접근하기 쉽도록 리스트를 정렬한다. +2. emnumerate를 이용해서 인덱스와 값을 가져온다. +3. 인덱스와 값이 일치하지 않으면 해당하는 인덱스의 값이 부재인 것이다. +4. 따라서 인덱스를 반환한다. +5. 반복문 내에서 찾지 못하면 마지막값이 부재인 것이다. +6. 따라서 리스트의 길이를 반환한다. + +# Complexity +- Time complexity: $$O(n)$$ + + +- Space complexity: $$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 +정렬된 리스트는 순서가 보장되어 있으므로 인덱스로 쉽게 찾을 수 있었다. \ No newline at end of file diff --git a/week-04/number-of-1-bits/chaeseungyun.py.md b/week-04/number-of-1-bits/chaeseungyun.py.md new file mode 100644 index 0000000..0bd314a --- /dev/null +++ b/week-04/number-of-1-bits/chaeseungyun.py.md @@ -0,0 +1,23 @@ +# Intuition +파이썬 메소드를 활용한다. + +# Approach +1. 파이썬 메소드를 활용해 이진수 문자열로 만들고 1의 개수를 센다. + +# Complexity +- Time complexity: $$O(n)$$ + +count() 함수가 문자열 내부를 돌 것이므로 n +- Space complexity: $$O(1)$$ + + +# Code +```python +class Solution: + def hammingWeight(self, n: int) -> int: + return bin(n).count('1') +``` + +# learned +bin() 메소드는 십진수를 이진수 문자열로 변환해준다. +count()는 문자열 내에 해당 문자가 몇 번 나타났는지 보여준다. \ No newline at end of file diff --git a/week-04/reverse-bit/chaeseungyun.py.md b/week-04/reverse-bit/chaeseungyun.py.md new file mode 100644 index 0000000..c0fbfb6 --- /dev/null +++ b/week-04/reverse-bit/chaeseungyun.py.md @@ -0,0 +1,23 @@ +# Intuition +파이썬 메소드를 활용한다. + +# Approach +1. input을 32비트 이진수 형태의 문자열로 저장한다. +2. 슬라이싱을 통해 문자열을 뒤집고 십진수로 변환한다. + +# Complexity +- Time complexity: $$O(n)$$ + + +- Space complexity: $$O(1)$$ + + +# Code +```python +class Solution: + def reverseBits(self, n: int) -> int: + return int(format(n, '032b')[::-1], 2) +``` + +# learned +format() 이라는 메소드를 배웠다. 숫자를 문자열로 바꿀 수 있는 문자열인데, 두 번째 인자로 형식을 줄 수 있다. 코드에선 32비트의 이진수의 형태로 변환했다. \ No newline at end of file