From 9ad0ee804fe95c8aa0d73ec153364dcebf1c2ad9 Mon Sep 17 00:00:00 2001 From: seungyeonchoi Date: Wed, 11 Nov 2026 15:36:51 -0500 Subject: [PATCH 1/3] contains duplicate solution Co-Authored-By: Claude Opus 5.5 --- contains-duplicate/seungyeonchoi.java | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 contains-duplicate/seungyeonchoi.java diff --git a/contains-duplicate/seungyeonchoi.java b/contains-duplicate/seungyeonchoi.java new file mode 100644 index 0000000000..c1f20fc5d2 --- /dev/null +++ b/contains-duplicate/seungyeonchoi.java @@ -0,0 +1,22 @@ +class Solution { + public boolean containsDuplicate(int[] nums) { + Set duplicates = new HashSet<>(); + for (int i=0; i Date: Fri, 13 Nov 2026 23:40:15 -0500 Subject: [PATCH 2/3] top k frequent elements solution Co-Authored-By: Claude Opus 5.5 --- top-k-frequent-elements/seungyeonchoi.java | 26 ++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 top-k-frequent-elements/seungyeonchoi.java diff --git a/top-k-frequent-elements/seungyeonchoi.java b/top-k-frequent-elements/seungyeonchoi.java new file mode 100644 index 0000000000..fdbe5f0cd5 --- /dev/null +++ b/top-k-frequent-elements/seungyeonchoi.java @@ -0,0 +1,26 @@ +class Solution { + public int[] topKFrequent(int[] nums, int k) { + /* + Approach + 1. using map, key -> num, value -> count + 2. sort + */ + + Map count = new HashMap<>(); + for (int num: nums) { // O(n) + count.put(num, count.getOrDefault(num,0)+1); + } + + return count.entrySet().stream() + .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())) // O(nlogn) + .map(Map.Entry::getKey) + .limit(k) + .mapToInt(Integer::intValue) + .toArray(); + /* + Complexity + - time: O(nlogn) // sort + - space: O(n) // declare map? + */ + } +} From 2bc24ac21f008b207ee0c00c28f72578387373c5 Mon Sep 17 00:00:00 2001 From: seungyeonchoi Date: Sat, 14 Nov 2026 00:19:21 -0500 Subject: [PATCH 3/3] top k frequent elements bucket sort solution Co-Authored-By: Claude Opus 5.5 --- top-k-frequent-elements/seungyeonchoi.java | 57 +++++++++++++++++----- 1 file changed, 46 insertions(+), 11 deletions(-) diff --git a/top-k-frequent-elements/seungyeonchoi.java b/top-k-frequent-elements/seungyeonchoi.java index fdbe5f0cd5..1742b7cee5 100644 --- a/top-k-frequent-elements/seungyeonchoi.java +++ b/top-k-frequent-elements/seungyeonchoi.java @@ -1,7 +1,7 @@ class Solution { public int[] topKFrequent(int[] nums, int k) { /* - Approach + Approach 1 1. using map, key -> num, value -> count 2. sort */ @@ -11,16 +11,51 @@ public int[] topKFrequent(int[] nums, int k) { count.put(num, count.getOrDefault(num,0)+1); } - return count.entrySet().stream() - .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())) // O(nlogn) - .map(Map.Entry::getKey) - .limit(k) - .mapToInt(Integer::intValue) - .toArray(); - /* - Complexity - - time: O(nlogn) // sort - - space: O(n) // declare map? + // return count.entrySet().stream() + // .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())) + // .map(Map.Entry::getKey) + // .limit(k) + // .mapToInt(Integer::intValue) + // .toArray(); + // /* + // Complexity + // - time: O(nlogn) // sort + // - space: O(n) // declare map? + // */ + + + /* + Approach 2 + 1. using map, key -> num, value -> count + 2. Initialize a 2D dynamic array (buckets) and group numbers by their frequency */ + List> buckets = new ArrayList<>(); + for (int i = 0; i <= nums.length; i++) { + buckets.add(new ArrayList<>()); + } + for (int key: count.keySet()) { + buckets.get(count.get(key)).add(key); + } + + int[] result = new int[k]; + int idx = 0; + for (int i = buckets.size()-1;i>=0;i--) { + for (int j = 0; j < buckets.get(i).size(); j++) { + if (idx o(n) + o(n) + o (n+1+ n) -> o(4n+1) + - space: O(n) -> dynamic 2d array (buckets) + */ } }