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; + } +} diff --git a/house-robber/dohyeon2.java b/house-robber/dohyeon2.java new file mode 100644 index 0000000000..c27b879251 --- /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]; + } +} diff --git a/longest-consecutive-sequence/dohyeon2.java b/longest-consecutive-sequence/dohyeon2.java new file mode 100644 index 0000000000..96c872f270 --- /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; + } +} diff --git a/top-k-frequent-elements/dohyeon2.java b/top-k-frequent-elements/dohyeon2.java new file mode 100644 index 0000000000..5969d67854 --- /dev/null +++ b/top-k-frequent-elements/dohyeon2.java @@ -0,0 +1,47 @@ +import java.util.HashMap; +import java.util.ArrayList; + +class Solution { + public int[] topKFrequent(int[] nums, int k) { + // time complexity O(n) + // space complexity O(n) + + HashMap frequentMap = new HashMap<>(); + + for (int i = 0; i < nums.length; i++) { + int num = nums[i]; + frequentMap.merge(num, 1, Integer::sum); + } + + ArrayList[] buckets = new ArrayList[nums.length + 1]; + + for (int i = 0; i <= nums.length; i++) { + buckets[i] = new ArrayList<>(); + } + + frequentMap.forEach((Integer a, Integer b) -> { + // Assign the largest values from the front of the array + buckets[nums.length - b].add(a); + }); + + int[] result = new int[k]; + + 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; + } +} diff --git a/two-sum/dohyeon2.java b/two-sum/dohyeon2.java new file mode 100644 index 0000000000..4eab1d569a --- /dev/null +++ b/two-sum/dohyeon2.java @@ -0,0 +1,32 @@ +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) { + return new int[] { i, index }; + } + } + + // If the valid answer is always exists this is not needed + // But the compiler don't know about that + return new int[2]; + } +}