From fcb31f4ae3b7f7c3c9382a033050e66e45add247 Mon Sep 17 00:00:00 2001 From: sonshn Date: Fri, 26 Jun 2026 15:05:04 +0900 Subject: [PATCH 1/6] contains-duplicate solution --- contains-duplicate/sonshn.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 contains-duplicate/sonshn.java diff --git a/contains-duplicate/sonshn.java b/contains-duplicate/sonshn.java new file mode 100644 index 0000000000..ef71d2b966 --- /dev/null +++ b/contains-duplicate/sonshn.java @@ -0,0 +1,16 @@ +/** + * HashSet을 사용하여 배열에 중복된 값이 있는지 확인 + * 시간 복잡도: O(n), 공간 복잡도: O(n) + */ +class Solution { + public boolean containsDuplicate(int[] nums) { + HashSet distinctSet = new HashSet<>(); + + for (int i = 0; i < nums.length; i++) { + if (distinctSet.contains(nums[i])) return true; + distinctSet.add(nums[i]); + } + + return false; + } +} From 6c69e25fccedcc0ac478a819b9e98b3e4f5fd1e1 Mon Sep 17 00:00:00 2001 From: sonshn Date: Fri, 26 Jun 2026 15:12:55 +0900 Subject: [PATCH 2/6] two-sum solution --- two-sum/sonshn.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 two-sum/sonshn.java diff --git a/two-sum/sonshn.java b/two-sum/sonshn.java new file mode 100644 index 0000000000..8e114d6238 --- /dev/null +++ b/two-sum/sonshn.java @@ -0,0 +1,17 @@ +class Solution { + public int[] twoSum(int[] nums, int target) { + int length = nums.length; + + for (int first = 0; first < length - 1; first++) { + for (int second = first + 1; second < length; second++) { + int sum = nums[first] + nums[second]; + + if (sum == target) { + return new int[]{first, second}; + } + } + } + + return new int[0]; + } +} From 9d7e093a97bf6a9e1cb99f824f5b734d26a3ec45 Mon Sep 17 00:00:00 2001 From: sonshn Date: Fri, 26 Jun 2026 16:32:26 +0900 Subject: [PATCH 3/6] top-k-frequent-elements solution --- top-k-frequent-elements/sonshn.java | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 top-k-frequent-elements/sonshn.java diff --git a/top-k-frequent-elements/sonshn.java b/top-k-frequent-elements/sonshn.java new file mode 100644 index 0000000000..59d4b7a93c --- /dev/null +++ b/top-k-frequent-elements/sonshn.java @@ -0,0 +1,29 @@ + +/** + * + * 시간 복잡도: O(n log n) + * 공간 복잡도: O(n) + */ +class Solution { + public int[] topKFrequent(int[] nums, int k) { + + Map counter = new HashMap<>(); + + for (int num : nums) { + counter.put(num, counter.getOrDefault(num, 0) + 1); + } + + List list = new ArrayList<>(counter.keySet()); + + // 빈도수 내림차순 정렬 + list.sort((a, b) -> Integer.compare(counter.get(b), counter.get(a))); + + int[] result = new int[k]; + + for (int i = 0; i < k; i++) { + result[i] = list.get(i); + } + + return result; + } +} From 7766ac3ec5bb27733a4ff41aef17bc12540eacea Mon Sep 17 00:00:00 2001 From: sonshn Date: Sat, 27 Jun 2026 10:23:02 +0900 Subject: [PATCH 4/6] =?UTF-8?q?top-k-frequent-elements=20=ED=92=80?= =?UTF-8?q?=EC=9D=B4=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- top-k-frequent-elements/sonshn.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/top-k-frequent-elements/sonshn.java b/top-k-frequent-elements/sonshn.java index 59d4b7a93c..8d4e0cbf8e 100644 --- a/top-k-frequent-elements/sonshn.java +++ b/top-k-frequent-elements/sonshn.java @@ -1,5 +1,16 @@ /** + * 풀이 + * 1. 빈도수 계산 + * - HashMap을 사용하여 각 숫자의 등장 횟수를 저장 + * - Key는 숫자, Value는 해당 숫자의 빈도수 + * 2. 고유 숫자 저장 + * - counter.keySet()을 ArrayList로 변환하여 중복 없이 숫자들만 저장 + * 3. 빈도수 기준 내림차순 정렬 + * - list.sort((a, b) -> Integer.compare(counter.get(b), counter.get(a))) + * - HashMap에 저장된 빈도수를 기준으로 많이 등장한 숫자가 앞에 오도록 정렬 + * 4. 상위 k개 반환 + * - 정렬된 리스트의 앞에서부터 k개의 숫자를 result 배열에 담아 반환 * * 시간 복잡도: O(n log n) * 공간 복잡도: O(n) From 32eb8b6144d8e2086650b27e022a97e0dce5c198 Mon Sep 17 00:00:00 2001 From: sonshn Date: Sat, 27 Jun 2026 10:27:42 +0900 Subject: [PATCH 5/6] longest-consecutive-sequence solution --- longest-consecutive-sequence/sonshn.java | 35 ++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 longest-consecutive-sequence/sonshn.java diff --git a/longest-consecutive-sequence/sonshn.java b/longest-consecutive-sequence/sonshn.java new file mode 100644 index 0000000000..14cbde3455 --- /dev/null +++ b/longest-consecutive-sequence/sonshn.java @@ -0,0 +1,35 @@ +import java.util.*; + +/** + * Finds the length of the longest consecutive elements sequence in an unsorted array. + * + * 시간 복잡도: O(n log n), 공간 복잡도: O(1) + */ +class Solution { + public int longestConsecutive(int[] nums) { + if (nums == null || nums.length == 0) { + return 0; + } + + Arrays.sort(nums); + + int longest = 0; + int length = 1; + + for (int i = 0; i < nums.length - 1; i++) { + if (nums[i] == nums[i + 1]) { + continue; + } + + if (nums[i] + 1 == nums[i + 1]) { + length++; + } else { + longest = Math.max(longest, length); + length = 1; + } + } + + longest = Math.max(longest, length); + return longest; + } +} From a1e4ddedae3f1d0494a702345e6865ce6f9dbda8 Mon Sep 17 00:00:00 2001 From: sonshn Date: Sat, 27 Jun 2026 18:34:05 +0900 Subject: [PATCH 6/6] house-robber solution --- house-robber/sonshn.java | 21 +++++++++++++++++++++ longest-consecutive-sequence/sonshn.java | 2 +- 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 house-robber/sonshn.java diff --git a/house-robber/sonshn.java b/house-robber/sonshn.java new file mode 100644 index 0000000000..d967dcd11a --- /dev/null +++ b/house-robber/sonshn.java @@ -0,0 +1,21 @@ +/** + * DFS를 이용한 재귀 풀이 + * + * 시간 복잡도: O(2^n), 공간 복잡도: O(n) + */ +class Solution { + public int rob(int[] nums) { + return dfs(nums, 0); + } + + private int dfs(int[] nums, int start) { + if (start >= nums.length) { + return 0; + } + + return Math.max( + nums[start] + dfs(nums, start + 2), + dfs(nums, start + 1) + ); + } +} diff --git a/longest-consecutive-sequence/sonshn.java b/longest-consecutive-sequence/sonshn.java index 14cbde3455..e224f6ac8e 100644 --- a/longest-consecutive-sequence/sonshn.java +++ b/longest-consecutive-sequence/sonshn.java @@ -1,7 +1,7 @@ import java.util.*; /** - * Finds the length of the longest consecutive elements sequence in an unsorted array. + * 주어진 배열에서 연속된 정수의 가장 긴 길이를 찾는 문제 * * 시간 복잡도: O(n log n), 공간 복잡도: O(1) */