From 3fc06d1aae7cdb702ba586ad222a50ea47b31a27 Mon Sep 17 00:00:00 2001 From: hoyeongkwak Date: Sat, 26 Jul 2025 10:51:39 +0900 Subject: [PATCH] Week1 Solutions --- contains-duplicate/hoyeongkwak.java | 16 +++++++++++ house-robber/hoyeongkwak.java | 15 +++++++++++ longest-consecutive-sequence/hoyeongkwak.java | 27 +++++++++++++++++++ top-k-frequent-elements/hoyeongkwak.java | 24 +++++++++++++++++ two-sum/hoyeongkwak.java | 17 ++++++++++++ 5 files changed, 99 insertions(+) create mode 100644 contains-duplicate/hoyeongkwak.java create mode 100644 house-robber/hoyeongkwak.java create mode 100644 longest-consecutive-sequence/hoyeongkwak.java create mode 100644 top-k-frequent-elements/hoyeongkwak.java create mode 100644 two-sum/hoyeongkwak.java diff --git a/contains-duplicate/hoyeongkwak.java b/contains-duplicate/hoyeongkwak.java new file mode 100644 index 000000000..8f17576d4 --- /dev/null +++ b/contains-duplicate/hoyeongkwak.java @@ -0,0 +1,16 @@ +/* +time complexity : O(n) +space complexity : O(n) +*/ +class Solution { + public boolean containsDuplicate(int[] nums) { + HashSet uniqueNums = new HashSet(); + + for (int num:nums) { + if (!uniqueNums.add(num)) { + return true; + } + } + return false; + } +} diff --git a/house-robber/hoyeongkwak.java b/house-robber/hoyeongkwak.java new file mode 100644 index 000000000..3aaf08761 --- /dev/null +++ b/house-robber/hoyeongkwak.java @@ -0,0 +1,15 @@ +/* +time complexity : O(n) +space complexity : O(n) +*/ +class Solution { + public int rob(int[] nums) { + int[] dp = new int[nums.length + 1]; + dp[0] = 0; + dp[1] = nums[0]; + for (int i = 2; i < dp.length; i++) { + dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i - 1]); + } + return dp[dp.length - 1]; + } +} diff --git a/longest-consecutive-sequence/hoyeongkwak.java b/longest-consecutive-sequence/hoyeongkwak.java new file mode 100644 index 000000000..f4deae7e6 --- /dev/null +++ b/longest-consecutive-sequence/hoyeongkwak.java @@ -0,0 +1,27 @@ +/* +time complexity : O(n) +space complexity : O(n) +*/ +class Solution { + public int longestConsecutive(int[] nums) { + int maxLen = 0; + Set numSet = new HashSet(); + for (int num: nums) { + numSet.add(num); + } + + for (int num : numSet) { + if (!numSet.contains(num - 1)){ + int continueCnt = 1; + int current = num; + + while (numSet.contains(current + 1)) { + current++; + continueCnt++; + } + maxLen = Math.max(continueCnt, maxLen); + } + } + return maxLen; + } +} diff --git a/top-k-frequent-elements/hoyeongkwak.java b/top-k-frequent-elements/hoyeongkwak.java new file mode 100644 index 000000000..22784cc8e --- /dev/null +++ b/top-k-frequent-elements/hoyeongkwak.java @@ -0,0 +1,24 @@ +/* +time complexity : O(nlogn) +space complexity : O(n) +*/ +class Solution { + public int[] topKFrequent(int[] nums, int k) { + HashMap numMap = new HashMap<>(); + for (int num:nums) { + if (numMap.containsKey(num)) { + int value = numMap.get(num); + numMap.put(num, value + 1); + } else { + numMap.put(num, 1); + } + } + List> entryList = new ArrayList<>(numMap.entrySet()); + entryList.sort((a, b) -> b.getValue() - a.getValue()); + int[] result = new int[k]; + for (int i = 0; i < k; i++) { + result[i] = entryList.get(i).getKey(); + } + return result; + } +} diff --git a/two-sum/hoyeongkwak.java b/two-sum/hoyeongkwak.java new file mode 100644 index 000000000..4cc003a0d --- /dev/null +++ b/two-sum/hoyeongkwak.java @@ -0,0 +1,17 @@ +/* +time complexity : O(n) +space complexity : O(n) +*/ +class Solution { + public int[] twoSum(int[] nums, int target) { + HashMap sumMap = new HashMap<>(); + for (int i = 0; i < nums.length; i++) { + int secondNum = target - nums[i]; + if (sumMap.containsKey(secondNum)) { + return new int[] { sumMap.get(secondNum), i }; + } + sumMap.put(nums[i], i); + } + return new int[] {}; + } +}