-
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.
- Loading branch information
1 parent
e86ad1e
commit 4b8e235
Showing
5 changed files
with
128 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,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; | ||
} | ||
``` |
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,40 @@ | ||
- 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의 평균이다. | ||
|
||
먼저 입력으로 돌아온 str 배열의 크기만큼 iterate를 한다. 여기서 n이 발생되고 | ||
각 str의 문자를 정렬하는데 m log m 만큼 시간이 소요된다. | ||
|
||
공간 복잡도는 str를 저장하기 위한 공간이 사용되므로 여기서 O(n) 이 사용되었다고 생각하였고 | ||
중간에 str를 char[]로 변환하는 과정에서 추가적인 공간이 사용되므로 여기서 O(m)이 사용되었다고 생각하였다. | ||
따라서 최종적으로 O(n + m) 으로 정리하였다. |
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,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; | ||
} | ||
``` |
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,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; | ||
} | ||
``` |
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,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; | ||
} | ||
} | ||
``` |