From 137a017cfdc47e2945a2f4e2f06810950ecfb815 Mon Sep 17 00:00:00 2001 From: jonghoonpark Date: Tue, 28 May 2024 08:46:53 +0900 Subject: [PATCH] week4 mission done --- counting-bits/dev-jonghoonpark.md | 20 ++++++++++++++++ group-anagrams/dev-jonghoonpark.md | 33 ++++++++++++++++++++++++++ missing-number/dev-jonghoonpark.md | 35 ++++++++++++++++++++++++++++ number-of-1-bits/dev-jonghoonpark.md | 15 ++++++++++++ reverse-bits/dev-jonghoonpark.md | 18 ++++++++++++++ 5 files changed, 121 insertions(+) create mode 100644 counting-bits/dev-jonghoonpark.md create mode 100644 group-anagrams/dev-jonghoonpark.md create mode 100644 missing-number/dev-jonghoonpark.md create mode 100644 number-of-1-bits/dev-jonghoonpark.md create mode 100644 reverse-bits/dev-jonghoonpark.md diff --git a/counting-bits/dev-jonghoonpark.md b/counting-bits/dev-jonghoonpark.md new file mode 100644 index 000000000..7dc3d3a9d --- /dev/null +++ b/counting-bits/dev-jonghoonpark.md @@ -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; +} +``` diff --git a/group-anagrams/dev-jonghoonpark.md b/group-anagrams/dev-jonghoonpark.md new file mode 100644 index 000000000..46c5ade61 --- /dev/null +++ b/group-anagrams/dev-jonghoonpark.md @@ -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> groupAnagrams(String[] strs) { + List> result = new ArrayList<>(); + HashMap 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 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의 평균이다. diff --git a/missing-number/dev-jonghoonpark.md b/missing-number/dev-jonghoonpark.md new file mode 100644 index 000000000..0bc7169c2 --- /dev/null +++ b/missing-number/dev-jonghoonpark.md @@ -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; +} +``` diff --git a/number-of-1-bits/dev-jonghoonpark.md b/number-of-1-bits/dev-jonghoonpark.md new file mode 100644 index 000000000..3f1357649 --- /dev/null +++ b/number-of-1-bits/dev-jonghoonpark.md @@ -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; +} +``` diff --git a/reverse-bits/dev-jonghoonpark.md b/reverse-bits/dev-jonghoonpark.md new file mode 100644 index 000000000..1b41f44dd --- /dev/null +++ b/reverse-bits/dev-jonghoonpark.md @@ -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; + } +} +```