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주차 답안 제출 #94

Merged
merged 2 commits into from
May 28, 2024
Merged
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
20 changes: 20 additions & 0 deletions counting-bits/dev-jonghoonpark.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
- https://leetcode.com/problems/counting-bits/
- time complexity : O(n \* log n), logn 이 붙는 이유는 bit는 log를 따라 수가 결정되기 때문
- space complexity : O(n)
- https://algorithm.jonghoonpark.com/2024/04/23/leetcode-338

```java
public int[] countBits(int n) {
int result[] = new int[n + 1];
for (int i = 0; i <= n; i++) {
int num = i;
int count = 0;
while (num > 0) {
count += num & 1;
num = num >> 1;
}
result[i] = count;
}
return result;
}
```
33 changes: 33 additions & 0 deletions group-anagrams/dev-jonghoonpark.md
dev-jonghoonpark marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
- https://leetcode.com/problems/group-anagrams/
- time complexity : O(n \* m log m), 여기서 m은 str 배열(strs)의 각 str의 평균이다.
- space complexity : O(n \* m)
- https://algorithm.jonghoonpark.com/2024/05/25/leetcode-49

```java
public List<List<String>> groupAnagrams(String[] strs) {
List<List<String>> result = new ArrayList<>();
HashMap<String, Integer> map = new HashMap<>();

for(String str: strs) {
char[] temp = str.toCharArray();
Arrays.sort(temp);
String sorted = String.valueOf(temp);
if(map.containsKey(sorted)) {
result.get(map.get(sorted)).add(str);
} else {
int newIndex = result.size();
List<String> newArrayList = new ArrayList<>();
result.add(newArrayList);
newArrayList.add(str);
map.put(sorted, newIndex);
}
}

return result;
}
```

## TC, SC

시간 복잡도는 O(n \* m log m)이고, 공간 복잡도는 O(n \* m)이다.
여기서 m은 str 배열(strs)의 각 str의 평균이다.
35 changes: 35 additions & 0 deletions missing-number/dev-jonghoonpark.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
- https://leetcode.com/problems/missing-number/
- time complexity : O(n)
- space complexity : O (n)
- https://algorithm.jonghoonpark.com/2024/05/25/leetcode-268

```java
public int missingNumber(int[] nums) {
int[] counts = new int[nums.length + 1];

for(int num : nums) {
counts[num] = 1;
}

for(int i = 0; i < counts.length; i ++){
if(counts[i] == 0) {
return i;
}
}

return -1;
}
```

등차수열로 푸는 방법도 있는 재밌는 문제.

```java
public int missingNumber(int[] arr) {
int sum = 0;
int max = (arr.length * (arr.length + 1)) / 2;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
return max - sum;
}
```
15 changes: 15 additions & 0 deletions number-of-1-bits/dev-jonghoonpark.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
- https://leetcode.com/problems/number-of-1-bits/
- time complexity : O(logn)
- space complexity : O(1)
- https://algorithm.jonghoonpark.com/2024/02/20/leetcode-191

```java
public int hammingWeight(int n) {
int count = 0;
while (n != 0) {
count += n % 2;
n = n >> 1;
}
return count;
}
```
18 changes: 18 additions & 0 deletions reverse-bits/dev-jonghoonpark.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
- https://leetcode.com/problems/reverse-bits/
- time complexity : O(1)
- space complexity : O(1)
- https://algorithm.jonghoonpark.com/2024/04/23/leetcode-190

```java
public class Solution {
public int reverseBits(int n) {
int ans = 0;
for (int i = 0; i < 32; i++) {
ans <<= 1;
ans |= (n & 1);
n >>= 1;
}
return ans;
}
}
```