From bacf40eae647b8eb748a139e7bbbc299d9d2edd3 Mon Sep 17 00:00:00 2001 From: devtkwon Date: Mon, 12 Aug 2024 23:36:59 +0900 Subject: [PATCH 1/7] contains-duplicate --- contains-duplicate/taekwon-dev.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 contains-duplicate/taekwon-dev.java diff --git a/contains-duplicate/taekwon-dev.java b/contains-duplicate/taekwon-dev.java new file mode 100644 index 000000000..af2e5a81a --- /dev/null +++ b/contains-duplicate/taekwon-dev.java @@ -0,0 +1,19 @@ +class Solution { + /** + * 시간 복잡도: O(N) + * - 최악의 경우 주어진 배열의 마지막 원소까지 체크해야 함. + * - 중복 검사를 위해 Set 선택한 이유 + * - O(1) 으로 탐색 가능 + * - 데이터 순서 보장 필요 없음 + */ + public boolean containsDuplicate(int[] nums) { + HashSet set = new HashSet<>(); + for (int num: nums) { + if (set.contains(num)) { + return true; + } + set.add(num); + } + return false; + } +} From c75f0f136bb4272989adf59bbe699a38be157a58 Mon Sep 17 00:00:00 2001 From: devtkwon Date: Thu, 15 Aug 2024 23:16:17 +0900 Subject: [PATCH 2/7] refactor: use Set interface instead of HashSet for variable declaration --- contains-duplicate/taekwon-dev.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contains-duplicate/taekwon-dev.java b/contains-duplicate/taekwon-dev.java index af2e5a81a..e2ea2f729 100644 --- a/contains-duplicate/taekwon-dev.java +++ b/contains-duplicate/taekwon-dev.java @@ -7,7 +7,7 @@ class Solution { * - 데이터 순서 보장 필요 없음 */ public boolean containsDuplicate(int[] nums) { - HashSet set = new HashSet<>(); + Set set = new HashSet<>(); for (int num: nums) { if (set.contains(num)) { return true; From 003b6751498f118ff9f49cdb0a295101d0aa8bb2 Mon Sep 17 00:00:00 2001 From: devtkwon Date: Thu, 15 Aug 2024 23:17:59 +0900 Subject: [PATCH 3/7] docs: add space complexity explanation --- contains-duplicate/taekwon-dev.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/contains-duplicate/taekwon-dev.java b/contains-duplicate/taekwon-dev.java index e2ea2f729..a4394410a 100644 --- a/contains-duplicate/taekwon-dev.java +++ b/contains-duplicate/taekwon-dev.java @@ -5,6 +5,9 @@ class Solution { * - 중복 검사를 위해 Set 선택한 이유 * - O(1) 으로 탐색 가능 * - 데이터 순서 보장 필요 없음 + * + * 공간 복잡도: O(N) + * - 최악의 경우 주어진 배열의 모든 원소가 자료구조에 저장되므로 O(N) */ public boolean containsDuplicate(int[] nums) { Set set = new HashSet<>(); From fbc65acac8cf80e3a3c842bcb25fe97b5baec2c5 Mon Sep 17 00:00:00 2001 From: devtkwon Date: Thu, 15 Aug 2024 23:42:55 +0900 Subject: [PATCH 4/7] top-k-frequent-elements --- top-k-frequent-elements/taekwon-dev.java | 37 ++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 top-k-frequent-elements/taekwon-dev.java diff --git a/top-k-frequent-elements/taekwon-dev.java b/top-k-frequent-elements/taekwon-dev.java new file mode 100644 index 000000000..ee8adb3e7 --- /dev/null +++ b/top-k-frequent-elements/taekwon-dev.java @@ -0,0 +1,37 @@ +class Solution { + /** + * 사고의 흐름: + * - Map 이용해서 각 키 값 별로 몇 개가 있는지 저장하자. + * - 일단 주어진 모든 배열을 확인해야 한다. + * - 정렬이 필요하겠다. + * - Map의 값을 기준으로 정렬을 하는데, 키 값도 같이 따라와야겠네? + * - Comparable? Comparator? (하 ... 이거 어떻게 했더라..) + * + * 시간 복잡도: O(NlogN) + * - ArrayList sort() 는 Merge Sort + * - Merge Sort 는 O(NlogN) + * + * 공간 복잡도: O(N) + * - 최악의 경우 주어진 배열의 모든 원소가 자료구조에 저장되므로 O(N) + */ + public int[] topKFrequent(int[] nums, int k) { + Map map = new HashMap<>(); + for (int num: nums) { + map.put(num, map.getOrDefault(num, 0) + 1); + } + List> list = new ArrayList<>(map.entrySet()); + list.sort(new Comparator>() { + @Override + public int compare(Map.Entry e1, Map.Entry e2) { + return e2.getValue().compareTo(e1.getValue()); + } + }); + + int[] result = new int[k]; + for (int i = 0; i < k; i++) { + result[i] = list.get(i).getKey(); + } + + return result; + } +} \ No newline at end of file From a1ff55e3d5f980f096003eb0972acaafb1b2cc83 Mon Sep 17 00:00:00 2001 From: devtkwon Date: Sat, 17 Aug 2024 09:34:50 +0900 Subject: [PATCH 5/7] number-of-1-bits --- number-of-1-bits/taekwon-dev.java | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 number-of-1-bits/taekwon-dev.java diff --git a/number-of-1-bits/taekwon-dev.java b/number-of-1-bits/taekwon-dev.java new file mode 100644 index 000000000..35f545eb9 --- /dev/null +++ b/number-of-1-bits/taekwon-dev.java @@ -0,0 +1,22 @@ +class Solution { + /** + * 시간 복잡도: O(log N) + * - 주어진 수를 2로 나누는 행위를 반복 (주어진 수가 0이 될 때까지) + * - 이는 반대로 보면 2를 몇 번 곱해야 N이 나오는지를 확인하는 과정과 같음 (로그의 의미..ㅎ) + * - 또 생각해보니까 Binary Search Tree 에서 특정 값을 조회할 때와 같은 흐름이었던 것 같음! + * + * 공간 복잡도: O(1) + * - 자료구조를 활용하지 않음 + */ + public int hammingWeight(int n) { + int result = 0; + while (n >= 1) { + if ((n % 2) == 1) { + result++; + } + n /= 2; + } + + return result; + } +} \ No newline at end of file From 1be1c5bdd93a518a32ed7d7445f76310a7513636 Mon Sep 17 00:00:00 2001 From: devtkwon Date: Sat, 17 Aug 2024 10:14:57 +0900 Subject: [PATCH 6/7] kth-smallest-element-in-a-bst --- .../taekwon-dev.java | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 kth-smallest-element-in-a-bst/taekwon-dev.java diff --git a/kth-smallest-element-in-a-bst/taekwon-dev.java b/kth-smallest-element-in-a-bst/taekwon-dev.java new file mode 100644 index 000000000..c9714fc6e --- /dev/null +++ b/kth-smallest-element-in-a-bst/taekwon-dev.java @@ -0,0 +1,58 @@ +/** + * 사고의 흐름: + * - Binary Search Tree & 순서? + * - In-order 방식으로 순회를 해야겠다. + * - 1-indexed 이므로 리스트에 옮기고 k번째 접근한다면 -1 해서 접근해야겠다. + * - 근데, In-order 순회 어떻게 하더라? ㅋㅋ (재귀로 했던 것 같은데..) >> 여기서 검색 (...) + * + * 시간 복잡도: O(N) + * - BST 모든 노드를 다 순회해야 함 + * + * 공간 복잡도: O(N) + * - 내가 활용한 스택을 기준으로 보면, + * - Stack 에 최대로 많이 들어갈 수 있는 수는 BST 높이 + * - BST 는 최악의 경우 한 쪽으로 완전히 편향되어 높이가 N이 될 수도 있고, + * - 균형이 잘 맞다면 (마치 AVL, Red-Black) log N이 될 수 있음 + * - 따라서 O(N) 이지만, 트리의 균형이 잘 맞다면 O(log N) + */ + +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode() {} + * TreeNode(int val) { this.val = val; } + * TreeNode(int val, TreeNode left, TreeNode right) { + * this.val = val; + * this.left = left; + * this.right = right; + * } + * } + */ +class Solution { + public int kthSmallest(TreeNode root, int k) { + Stack stack = new Stack<>(); + TreeNode current = root; + int count = 0; + + while (current != null || !stack.isEmpty()) { + while (current != null) { + stack.push(current); + current = current.left; + } + + current = stack.pop(); + count++; + + if (count == k) { + return current.val; + } + + current = current.right; + } + + return -1; + } +} \ No newline at end of file From ca2017a91b48c99e8b38a30619f139c44fa2f401 Mon Sep 17 00:00:00 2001 From: devtkwon Date: Sun, 18 Aug 2024 10:02:30 +0900 Subject: [PATCH 7/7] chore: add final newline --- kth-smallest-element-in-a-bst/taekwon-dev.java | 2 +- number-of-1-bits/taekwon-dev.java | 2 +- top-k-frequent-elements/taekwon-dev.java | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/kth-smallest-element-in-a-bst/taekwon-dev.java b/kth-smallest-element-in-a-bst/taekwon-dev.java index c9714fc6e..938fca87a 100644 --- a/kth-smallest-element-in-a-bst/taekwon-dev.java +++ b/kth-smallest-element-in-a-bst/taekwon-dev.java @@ -55,4 +55,4 @@ public int kthSmallest(TreeNode root, int k) { return -1; } -} \ No newline at end of file +} diff --git a/number-of-1-bits/taekwon-dev.java b/number-of-1-bits/taekwon-dev.java index 35f545eb9..93c23ba61 100644 --- a/number-of-1-bits/taekwon-dev.java +++ b/number-of-1-bits/taekwon-dev.java @@ -19,4 +19,4 @@ public int hammingWeight(int n) { return result; } -} \ No newline at end of file +} diff --git a/top-k-frequent-elements/taekwon-dev.java b/top-k-frequent-elements/taekwon-dev.java index ee8adb3e7..13503afc6 100644 --- a/top-k-frequent-elements/taekwon-dev.java +++ b/top-k-frequent-elements/taekwon-dev.java @@ -34,4 +34,4 @@ public int compare(Map.Entry e1, Map.Entry e return result; } -} \ No newline at end of file +}