From 8ae6dffc86073681218552d7525b3dfa0818b404 Mon Sep 17 00:00:00 2001 From: Dohyeon Ju Date: Tue, 24 Feb 2026 22:21:02 +0900 Subject: [PATCH 1/9] Solutions for Contains Duplicate #217 --- contains-duplicate/dohyeon2.java | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 contains-duplicate/dohyeon2.java diff --git a/contains-duplicate/dohyeon2.java b/contains-duplicate/dohyeon2.java new file mode 100644 index 0000000000..cc301ef3ac --- /dev/null +++ b/contains-duplicate/dohyeon2.java @@ -0,0 +1,24 @@ +import java.util.HashSet; + +class Solution { + // The problem is to check if there are any duplicate elements in the array. + // So, I decided to use HashSet because it has O(1) time complexity for add and contains operations which are good to use for checking duplicates. + public boolean containsDuplicate(int[] nums) { + // the element type of the array is int, so create Integer HashSet + // O(n) space complexity + HashSet exists = new HashSet(); + // the nums array has length up to 10^5, so use int type + // O(n) time complexity average + // worst case: O(n log n) if many elements hash to the same bucket + for(int i = 0; i < nums.length; i++){ + int num = nums[i]; + + if(exists.contains(num)){ + return true; + }else{ + exists.add(num); + } + } + return false; + } +} From 10416296f21271f61c8f98d963a92f778fd27ea5 Mon Sep 17 00:00:00 2001 From: Dohyeon Ju Date: Tue, 24 Feb 2026 22:52:03 +0900 Subject: [PATCH 2/9] Solution for Two Sum #219 --- two-sum/dohyeon2.java | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 two-sum/dohyeon2.java diff --git a/two-sum/dohyeon2.java b/two-sum/dohyeon2.java new file mode 100644 index 0000000000..05d0522f81 --- /dev/null +++ b/two-sum/dohyeon2.java @@ -0,0 +1,36 @@ +import java.util.HashMap; + +class Solution { + public int[] twoSum(int[] nums, int target) { + // Approach : using HashMap to get index with the element in O(n) time complexity + // SpaceComplexity is also O(n) + HashMap numIndexMap = new HashMap(); + + // Make key and value HashMap + for(int i = 0; i < nums.length; i++){ + int num = nums[i]; + numIndexMap.put(num,i); + } + + // Search for the other operand looping nums + for(int i = 0; i < nums.length; i++){ + int num = nums[i]; + int operand = target - num; + Integer index = numIndexMap.get(operand); + boolean indexExists = index != null; + boolean indexExistsAndIndexIsNotTheNum = indexExists && i != index; + if(indexExistsAndIndexIsNotTheNum){ + // Manual sort + if( i < index){ + return new int[]{ i, index }; + }else{ + return new int[]{ index, i }; + } + } + } + + // If the valid answer is always exists this is not needed + // But the compiler don't know about that + return new int[2]; + } +} \ No newline at end of file From 4388995572c10b9b1d24083e25f001b9e0e65168 Mon Sep 17 00:00:00 2001 From: Dohyeon Ju Date: Wed, 25 Feb 2026 01:46:51 +0900 Subject: [PATCH 3/9] Solution for Top K Frequent Elements #237 --- top-k-frequent-elements/dohyeon2.java | 42 +++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 top-k-frequent-elements/dohyeon2.java diff --git a/top-k-frequent-elements/dohyeon2.java b/top-k-frequent-elements/dohyeon2.java new file mode 100644 index 0000000000..19b0b9a4b7 --- /dev/null +++ b/top-k-frequent-elements/dohyeon2.java @@ -0,0 +1,42 @@ +import java.util.HashMap; +import java.util.Map; +import java.util.PriorityQueue; +import java.util.Comparator; + +public class dohyeon2 { + public int[] topKFrequent(int[] nums, int k) { + // approach : + // 1. create map to match num to frequency + // 2. create max frequency Priority Queue for sort + // 3. pop the max value from the queue until k + // time complexity O(n log n) : priority queue comparison + // space complexity O(n) + + // ChatGPT says that there is O(n) solution for this, how? + HashMap frequentMap = new HashMap<>(); + + for (int i = 0; i < nums.length; i++) { + int num = nums[i]; + frequentMap.merge(num, 1, Integer::sum); + } + + PriorityQueue> PQ = new PriorityQueue<>(k, + Comparator.comparing(Map.Entry::getValue).reversed()); + + for(Map.Entry e : frequentMap.entrySet()){ + PQ.add(e); + } + + int[] result = new int[k]; + + int idx = 0; + + while (idx < k && PQ.size() > 0) { + Map.Entry v = PQ.poll(); + result[idx] = (int) v.getKey(); + idx++; + } + + return result; + } +} From 318798726338976029060ccd7c68db50c2ceaf65 Mon Sep 17 00:00:00 2001 From: Dohyeon Ju Date: Sun, 1 Mar 2026 02:02:21 +0900 Subject: [PATCH 4/9] Solution for Longest Consecutive Sequence #240 --- longest-consecutive-sequence/dohyeon2.java | 79 ++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 longest-consecutive-sequence/dohyeon2.java diff --git a/longest-consecutive-sequence/dohyeon2.java b/longest-consecutive-sequence/dohyeon2.java new file mode 100644 index 0000000000..a145d9cbb1 --- /dev/null +++ b/longest-consecutive-sequence/dohyeon2.java @@ -0,0 +1,79 @@ +import java.util.HashSet; + +class Solution { + // This solution was inspired by: + // https://www.algodale.com/problems/longest-consecutive-sequence/ + // + // I initially believed this algorithm would run in O(n) time, + // but it resulted in a Time Limit Exceeded error. + // + // Although the expected time complexity is O(n), + // repeatedly calling set.iterator().next() might introduce overhead. + // Iterating through the set using a for-loop may be a better approach. + // + // In this case, it seems preferable to follow the approach described here: + // https://www.algodale.com/problems/longest-consecutive-sequence/#%ED%92%80%EC%9D%B4-3 + + public int oldApproach(int[] nums) { + int count = 0; + HashSet set = new HashSet<>(); + for (int num : nums) { + set.add(num); + } + + while (set.size() > 0) { + int buffer = 1; + // This may cause a Time Limit Exceeded error. + Integer curr = set.iterator().next(); + set.remove(curr); + Integer next = curr + 1; + Integer prev = curr - 1; + + while (set.contains(next)) { + set.remove(next); + next++; + buffer++; + } + + while (set.contains(prev)) { + set.remove(prev); + prev--; + buffer++; + } + + count = Math.max(count, buffer); + } + + return count; + } + + public int longestConsecutive(int[] nums) { + int count = 0; + + // The Set has O(n) space complexity, + // because it may store up to n elements in memory. + // Is this the correct way to evaluate space complexity? + HashSet set = new HashSet<>(); + for (int num : nums) { + set.add(num); + } + + for (int num : set) { + if (set.contains(num - 1)) { + continue; + } + + int currentNum = num; + int currentCount = 1; + + while (set.contains(currentNum + 1)) { + currentNum++; + currentCount++; + } + + count = Math.max(count, currentCount); + } + + return count; + } +} \ No newline at end of file From b613632315f1fe7d2346588fb992dd0bdf1839ae Mon Sep 17 00:00:00 2001 From: Dohyeon Ju Date: Sun, 1 Mar 2026 03:07:48 +0900 Subject: [PATCH 5/9] Solution for House Robber #264 --- house-robber/dohyeon2.java | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 house-robber/dohyeon2.java diff --git a/house-robber/dohyeon2.java b/house-robber/dohyeon2.java new file mode 100644 index 0000000000..d71c851aa2 --- /dev/null +++ b/house-robber/dohyeon2.java @@ -0,0 +1,30 @@ +class Solution { + public int rob(int[] nums) { + // Dynamic Planning + // Time Complexity: O(n) + // Space Complexity: O(n) for the dp array + // When applying DP, it is important to define the accumulated state clearly. + + if (nums.length == 1) { + return nums[0]; + } + + // The state of dp is maximum amount of robbery at i + int[] dp = new int[nums.length]; + + dp[0] = nums[0]; + dp[1] = Math.max(nums[0], nums[1]); + + for (int i = 2; i < nums.length; i++) { + dp[i] = Math.max( + // If we skip the current house, + // we can take the maximum amount of robbery at i - 1 + dp[i - 1], + // If we rob the current house, we cannot rob the previous house + // so we can take the maximum amount of robbery at i - 2 + dp[i - 2] + nums[i]); + } + + return dp[nums.length - 1]; + } +} \ No newline at end of file From 659e802c5a65b918b385e14469e49f3894494ccc Mon Sep 17 00:00:00 2001 From: Dohyeon Ju Date: Sun, 1 Mar 2026 03:46:09 +0900 Subject: [PATCH 6/9] Bucket Solution for Top K Frequent Elements #237 --- top-k-frequent-elements/dohyeon2.java | 47 +++++++++++++++------------ 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/top-k-frequent-elements/dohyeon2.java b/top-k-frequent-elements/dohyeon2.java index 19b0b9a4b7..6058d9e897 100644 --- a/top-k-frequent-elements/dohyeon2.java +++ b/top-k-frequent-elements/dohyeon2.java @@ -1,18 +1,11 @@ import java.util.HashMap; -import java.util.Map; -import java.util.PriorityQueue; -import java.util.Comparator; +import java.util.ArrayList; -public class dohyeon2 { +class Solution { public int[] topKFrequent(int[] nums, int k) { - // approach : - // 1. create map to match num to frequency - // 2. create max frequency Priority Queue for sort - // 3. pop the max value from the queue until k - // time complexity O(n log n) : priority queue comparison + // time complexity O(n) // space complexity O(n) - // ChatGPT says that there is O(n) solution for this, how? HashMap frequentMap = new HashMap<>(); for (int i = 0; i < nums.length; i++) { @@ -20,23 +13,35 @@ public int[] topKFrequent(int[] nums, int k) { frequentMap.merge(num, 1, Integer::sum); } - PriorityQueue> PQ = new PriorityQueue<>(k, - Comparator.comparing(Map.Entry::getValue).reversed()); + ArrayList[] buckets = new ArrayList[nums.length + 1]; - for(Map.Entry e : frequentMap.entrySet()){ - PQ.add(e); + for (int i = 0; i <= nums.length; i++) { + buckets[i] = new ArrayList<>(); } - int[] result = new int[k]; + frequentMap.forEach((Integer a, Integer b) -> { + // Assign the largest values from the front of the array + buckets[nums.length - b].add(a); + }); - int idx = 0; + int[] result = new int[k]; - while (idx < k && PQ.size() > 0) { - Map.Entry v = PQ.poll(); - result[idx] = (int) v.getKey(); - idx++; + int pointer = 0; + + for (int i = 0; i < buckets.length; i++) { + ArrayList list = buckets[i]; + if (list.size() == 0) { + continue; + } + for (Integer num : list) { + result[pointer] = (int) num; + // Return the result when the pointer reaches k + if (pointer == (k - 1)) + return result; + pointer++; + } } return result; } -} +} \ No newline at end of file From c86b2012edd7a52fc0888b04b8f7379b9d044d90 Mon Sep 17 00:00:00 2001 From: Dohyeon Ju Date: Sun, 1 Mar 2026 03:59:29 +0900 Subject: [PATCH 7/9] =?UTF-8?q?=ED=8C=8C=EC=9D=BC=EC=9D=98=20=EB=A7=88?= =?UTF-8?q?=EC=A7=80=EB=A7=89=EC=97=90=20=EA=B0=9C=ED=96=89=EB=AC=B8?= =?UTF-8?q?=EC=9E=90=EB=A5=BC=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- house-robber/dohyeon2.java | 2 +- longest-consecutive-sequence/dohyeon2.java | 2 +- top-k-frequent-elements/dohyeon2.java | 2 +- two-sum/dohyeon2.java | 23 +++++++++++----------- 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/house-robber/dohyeon2.java b/house-robber/dohyeon2.java index d71c851aa2..c27b879251 100644 --- a/house-robber/dohyeon2.java +++ b/house-robber/dohyeon2.java @@ -27,4 +27,4 @@ public int rob(int[] nums) { return dp[nums.length - 1]; } -} \ No newline at end of file +} diff --git a/longest-consecutive-sequence/dohyeon2.java b/longest-consecutive-sequence/dohyeon2.java index a145d9cbb1..96c872f270 100644 --- a/longest-consecutive-sequence/dohyeon2.java +++ b/longest-consecutive-sequence/dohyeon2.java @@ -76,4 +76,4 @@ public int longestConsecutive(int[] nums) { return count; } -} \ No newline at end of file +} diff --git a/top-k-frequent-elements/dohyeon2.java b/top-k-frequent-elements/dohyeon2.java index 6058d9e897..5969d67854 100644 --- a/top-k-frequent-elements/dohyeon2.java +++ b/top-k-frequent-elements/dohyeon2.java @@ -44,4 +44,4 @@ public int[] topKFrequent(int[] nums, int k) { return result; } -} \ No newline at end of file +} diff --git a/two-sum/dohyeon2.java b/two-sum/dohyeon2.java index 05d0522f81..d6152b905c 100644 --- a/two-sum/dohyeon2.java +++ b/two-sum/dohyeon2.java @@ -2,29 +2,30 @@ class Solution { public int[] twoSum(int[] nums, int target) { - // Approach : using HashMap to get index with the element in O(n) time complexity + // Approach : using HashMap to get index with the element in O(n) time + // complexity // SpaceComplexity is also O(n) - HashMap numIndexMap = new HashMap(); + HashMap numIndexMap = new HashMap(); // Make key and value HashMap - for(int i = 0; i < nums.length; i++){ + for (int i = 0; i < nums.length; i++) { int num = nums[i]; - numIndexMap.put(num,i); + numIndexMap.put(num, i); } // Search for the other operand looping nums - for(int i = 0; i < nums.length; i++){ + for (int i = 0; i < nums.length; i++) { int num = nums[i]; int operand = target - num; Integer index = numIndexMap.get(operand); boolean indexExists = index != null; boolean indexExistsAndIndexIsNotTheNum = indexExists && i != index; - if(indexExistsAndIndexIsNotTheNum){ + if (indexExistsAndIndexIsNotTheNum) { // Manual sort - if( i < index){ - return new int[]{ i, index }; - }else{ - return new int[]{ index, i }; + if (i < index) { + return new int[] { i, index }; + } else { + return new int[] { index, i }; } } } @@ -33,4 +34,4 @@ public int[] twoSum(int[] nums, int target) { // But the compiler don't know about that return new int[2]; } -} \ No newline at end of file +} From b3dc2a0e933f7f56d584299818a01f2d8974b74b Mon Sep 17 00:00:00 2001 From: Dohyeon Ju Date: Sun, 1 Mar 2026 04:11:26 +0900 Subject: [PATCH 8/9] No need to sort --- two-sum/dohyeon2.java | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/two-sum/dohyeon2.java b/two-sum/dohyeon2.java index d6152b905c..07f558e12d 100644 --- a/two-sum/dohyeon2.java +++ b/two-sum/dohyeon2.java @@ -21,12 +21,7 @@ public int[] twoSum(int[] nums, int target) { boolean indexExists = index != null; boolean indexExistsAndIndexIsNotTheNum = indexExists && i != index; if (indexExistsAndIndexIsNotTheNum) { - // Manual sort - if (i < index) { - return new int[] { i, index }; - } else { - return new int[] { index, i }; - } + return new int[] { i, index }; } } @@ -34,4 +29,4 @@ public int[] twoSum(int[] nums, int target) { // But the compiler don't know about that return new int[2]; } -} +} \ No newline at end of file From aa308f2c5f9f199d00453045636877746beb4676 Mon Sep 17 00:00:00 2001 From: Dohyeon Ju Date: Sun, 1 Mar 2026 04:24:06 +0900 Subject: [PATCH 9/9] =?UTF-8?q?=EB=88=84=EB=9D=BD=EB=90=9C=20=EA=B0=9C?= =?UTF-8?q?=ED=96=89=EB=AC=B8=EC=9E=90=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- two-sum/dohyeon2.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/two-sum/dohyeon2.java b/two-sum/dohyeon2.java index 07f558e12d..4eab1d569a 100644 --- a/two-sum/dohyeon2.java +++ b/two-sum/dohyeon2.java @@ -29,4 +29,4 @@ public int[] twoSum(int[] nums, int target) { // But the compiler don't know about that return new int[2]; } -} \ No newline at end of file +}