Skip to content
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

[김성재] 4주차 제출합니다 #30

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
41 changes: 41 additions & 0 deletions week-04/counting-bits/seongjae.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Intuition
이를 0부터 n까지 모든 숫자를 이진수로 변환 후 1의 개수를 센다.

# Approach
1. 0부터 n까지의 모든 숫자를 순회함.
2. 각 숫자에 대해 이진수로 변환한 후 1의 개수를 셈.
3. 1의 개수를 결과 배열에 저장함.
4. ㅍ최종적으로 결과 배열을 반환함.

# Complexity
- Time complexity: O(n log n)
각 숫자에 대해 이진수 변환 및 1의 개수를 세는 데 O(log n) 시간이 걸리므로,
총 n개의 숫자에 대해 수행하면 O(n log n) 시간이 걸림.

- Space complexity: O(n)
결과 배열의 크기가 n + 1이므로 O(n)의 공간이 필요함.

2ms 46.43MB
# Code
```java
public class CountingBits338 {
public int[] countBits(int n) {
int[] array = new int[n + 1];
for(int i = 0; i <= n; i++) {
int count = 0;
int num = i;

while (num != 0) {
count += num & 1;
num >>>= 1;
}

array[i] = count;
}
return array;
}
}
```

# learn
잊고 있던 비트 연산자를 다시 써볼 수 있었다.
46 changes: 46 additions & 0 deletions week-04/group-anagrams/seongjae.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Intuition
단어를 정렬해서 확인한다.

# Approach
1. 입력된 문자열 배열을 순회함.
2. 각 문자열을 문자 배열로 변환한 뒤 정렬함.
3. 정렬된 문자열을 키로 사용하여 해시맵에 추가함.
4. 해시맵의 키에 해당하는 리스트에 원래 문자열을 추가함.
5. 해시맵의 모든 값을 결과 리스트에 추가하여 반환함.

# Complexity
- Time complexity: O(NKlogK)
N은 문자열의 개수, K는 문자열의 평균 길이
각 문자열을 정렬하는 데 O(K log K) 시간이 걸리므로 전체 시간 복잡도는 O(NKlogK)

- Space complexity: O(N * K)
해시맵에 모든 문자열을 저장하므로 공간 복잡도는 O(N * K)

6ms 47.83MB
# Code
```java
public class GroupAnagrams49 {
public static List<List<String>> groupAnagrams(String[] strs) {
List<List<String>> result = new ArrayList<>();
Map<String, List<String>> map = new HashMap<>();

for (String str : strs) {
char[] charArray = str.toCharArray();
Arrays.sort(charArray);
String sortedStr = new String(charArray);

if (!map.containsKey(sortedStr)) {
map.put(sortedStr, new ArrayList<>());
}
map.get(sortedStr).add(str);
}

result.addAll(map.values());

return result;
}
}

```

# learn
34 changes: 34 additions & 0 deletions week-04/missing-number/seongjae.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Intuition
배열을 정렬한 후, 각 인덱스와 배열 값을 비교하여 빠진 숫자를 찾아낸다.

# Approach
1. 주어진 배열을 정렬함.
2. 배열의 길이와 배열의 마지막 요소를 비교하여 빠진 숫자가 n인지 확인함.
3. 배열을 순회하며 인덱스와 배열 값을 비교하여 불일치하는 값을 찾음.
4. 불일치하는 값을 반환하고, 모든 값이 일치하면 0을 반환함.

# Complexity
- Time complexity: O(n log n)
배열을 정렬하는 데 O(n log n) 시간
배열을 순회하는 데 O(n)이 걸린다.

- Space complexity: O(1)
추가적인 공간을 거의 사용하지 않으므로 O(1)

5ms 45.24MB
# Code
```java
public class MissingNumber268 {
public int missingNumber(int[] nums) {
int len = nums.length;
Arrays.sort(nums);
if(len != nums[len-1]) return len;
for(int i = 0; i < len; i++) {
if(nums[i] != i) return i;
}
return 0;
}
}
```

# learn
39 changes: 39 additions & 0 deletions week-04/number-of-1-bits/seongjae.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Intuition
비트를 하나씩 확인한다.

# Approach
1. 주어진 정수가 0인 경우 0을 반환함.
2. 반복문을 사용하여 정수가 0이 될 때까지 각 비트를 확인함.
3. 각 반복에서 가장 높은 2의 거듭제곱 값을 찾아서 해당 값을 정수에서 빼고 1의 개수를 증가시킴.
4. 반복문이 종료되면 1의 개수를 반환함.

# Complexity
- Time complexity: O(log n)
각 반복에서 n을 반으로 줄이는 연산을 수행하므로 O(log n)

- Space complexity: O(1)
추가적인 공간을 거의 사용하지 않으므로 O(1)

1ms 40.45MB
# Code
```java
public class NumberOf1Bits191 {
public int hammingWeight(int n) {
if (n == 0) return 0;

int count = 0;
while (n > 0) {
int highestPowerOfTwo = 1;
while (highestPowerOfTwo <= n / 2) {
highestPowerOfTwo *= 2;
}
n -= highestPowerOfTwo;
count++;
}

return count;
}
}
```

# learn
35 changes: 35 additions & 0 deletions week-04/reverse-bits/seongjae.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Intuition
비트의 순서를 거꾸로 만든다.
# Approach
1. 결과를 저장할 변수를 0으로 초기화함.
2. ㅊ주어진 정수의 모든 32비트에 대해 반복함.
3. 매 반복마다 결과 변수를 왼쪽으로 1비트 이동시키고, 주어진 정수의 마지막 비트를 결과 변수에 추가함.
4. 주어진 정수를 오른쪽으로 1비트 이동시켜 다음 비트를 처리함.
5. 최종적으로 반전된 결과를 반환함.

# Complexity
- Time complexity: O(1)
32비트 정수를 처리하므로 항상 32번의 반복만 수행

- Space complexity: O(1)
추가적인 공간을 거의 사용하지 않으므로 O(1)

1ms 40.45MB
# Code
```java
public class ReverseBits190 {
public int reverseBits(int n) {
int res = 0;

for (int i = 0; i < 32; i++) {
res <<= 1;
res += n & 1;
n >>= 1;
}

return res;
}
}
```

# learn