From 3feb45545a86e8502cd00c24d467a2351d6dc477 Mon Sep 17 00:00:00 2001 From: hyeok Date: Mon, 10 Nov 2025 02:36:13 +0900 Subject: [PATCH 01/11] contains duplicated solution --- contains-duplicate/dylan-jung.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 contains-duplicate/dylan-jung.cpp diff --git a/contains-duplicate/dylan-jung.cpp b/contains-duplicate/dylan-jung.cpp new file mode 100644 index 0000000000..fbe563892b --- /dev/null +++ b/contains-duplicate/dylan-jung.cpp @@ -0,0 +1,12 @@ +class Solution { + public: + bool containsDuplicate(vector& nums) { + unordered_set s; + for(auto item: nums) { + if(s.count(item) > 0) return true; + s.insert(item); + } + return false; + } + }; + \ No newline at end of file From 83f9b70cc4e2fee93594a9370068d477d596c5a7 Mon Sep 17 00:00:00 2001 From: hyeok Date: Mon, 10 Nov 2025 02:47:30 +0900 Subject: [PATCH 02/11] fix: contains duplicated solution --- contains-duplicate/dylan-jung.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/contains-duplicate/dylan-jung.cpp b/contains-duplicate/dylan-jung.cpp index fbe563892b..ad9a2229b2 100644 --- a/contains-duplicate/dylan-jung.cpp +++ b/contains-duplicate/dylan-jung.cpp @@ -9,4 +9,3 @@ class Solution { return false; } }; - \ No newline at end of file From 8c86a4cdefa0b2cc5b11803efea4b81a4cb7b1f2 Mon Sep 17 00:00:00 2001 From: hyeok Date: Mon, 10 Nov 2025 11:23:33 +0900 Subject: [PATCH 03/11] two-sum solution --- contains-duplicate/dylan-jung.cpp | 18 +++++++++--------- two-sum/dylan-jung.cpp | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 9 deletions(-) create mode 100644 two-sum/dylan-jung.cpp diff --git a/contains-duplicate/dylan-jung.cpp b/contains-duplicate/dylan-jung.cpp index ad9a2229b2..149505f06c 100644 --- a/contains-duplicate/dylan-jung.cpp +++ b/contains-duplicate/dylan-jung.cpp @@ -1,11 +1,11 @@ class Solution { - public: - bool containsDuplicate(vector& nums) { - unordered_set s; - for(auto item: nums) { - if(s.count(item) > 0) return true; - s.insert(item); - } - return false; +public: + bool containsDuplicate(vector& nums) { + unordered_set s; + for(auto item: nums) { + if(s.count(item) > 0) return true; + s.insert(item); } - }; + return false; + } +}; diff --git a/two-sum/dylan-jung.cpp b/two-sum/dylan-jung.cpp new file mode 100644 index 0000000000..30e4b7e50b --- /dev/null +++ b/two-sum/dylan-jung.cpp @@ -0,0 +1,18 @@ +class Solution { +public: + vector twoSum(vector& nums, int target) { + vector> arr; + arr.reserve(nums.size()); + for(int i = 0; i < nums.size(); i++) { + arr.push_back({nums[i], i}); + } + int s = 0; int e = nums.size()-1; + sort(arr.begin(), arr.end()); + while(s < e) { + if(get<0>(arr[s]) + get<0>(arr[e]) > target) e-=1; + else if (get<0>(arr[s]) + get<0>(arr[e]) < target) s+=1; + else return {get<1>(arr[s]), get<1>(arr[e])}; + } + return {}; + } +}; From 0d8c4b8068adb2016f9f99e341f95be12587b27b Mon Sep 17 00:00:00 2001 From: hyeok Date: Mon, 10 Nov 2025 12:24:16 +0900 Subject: [PATCH 04/11] top-k-freq-elems solutions --- top-k-frequent-elements/dylan-jung.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 top-k-frequent-elements/dylan-jung.cpp diff --git a/top-k-frequent-elements/dylan-jung.cpp b/top-k-frequent-elements/dylan-jung.cpp new file mode 100644 index 0000000000..944f0f1177 --- /dev/null +++ b/top-k-frequent-elements/dylan-jung.cpp @@ -0,0 +1,23 @@ +class Solution { +public: + vector topKFrequent(vector& nums, int k) { + unordered_map m; + map> freq; + for(auto i: nums) m[i] += 1; + for(auto item: m) { + auto [k, v] = item; + freq[v].push_back(k); + } + + vector ans; + ans.reserve(k); + auto it = freq.rbegin(); + while(k > 0) { + auto kth = it->second; + ans.insert(ans.end(), kth.begin(), kth.end()); + k-=kth.size(); + it++; + } + return ans; + } +}; From 394d5f3b9b95fccf3387a36218f31abbed88d382 Mon Sep 17 00:00:00 2001 From: hyeok Date: Mon, 10 Nov 2025 12:28:44 +0900 Subject: [PATCH 05/11] longest-consecutive-sequence solution --- longest-consecutive-sequence/dylan-jung.cpp | 22 +++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 longest-consecutive-sequence/dylan-jung.cpp diff --git a/longest-consecutive-sequence/dylan-jung.cpp b/longest-consecutive-sequence/dylan-jung.cpp new file mode 100644 index 0000000000..269a50d690 --- /dev/null +++ b/longest-consecutive-sequence/dylan-jung.cpp @@ -0,0 +1,22 @@ +class Solution { +public: + int longestConsecutive(vector& nums) { + auto s = unordered_set(); + for(int it : nums) { + s.insert(it); + } + + int m = 0; + for(int it : s) { + if(s.count(it-1) > 0) continue; + + int cnt = 0; + while(s.count(it+cnt) > 0){ + cnt++; + } + m = max(m, cnt); + } + + return m; + } +}; From a51dccacc553dafe054e06678ca5b438f80c0223 Mon Sep 17 00:00:00 2001 From: hyeok Date: Mon, 10 Nov 2025 15:35:06 +0900 Subject: [PATCH 06/11] house-robber solution --- house-robber/dylan-jung.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 house-robber/dylan-jung.cpp diff --git a/house-robber/dylan-jung.cpp b/house-robber/dylan-jung.cpp new file mode 100644 index 0000000000..5a6b364d25 --- /dev/null +++ b/house-robber/dylan-jung.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + int dp[100] = { 0 }; + int rob(vector& nums) { + dp[0] = nums[0]; + if(nums.size() >= 2) { + dp[1] = nums[1]; + } + for(int i = 2; i < nums.size(); i++) { + for(int j = 0; j < i-1; j++) { + dp[i] = max(dp[j] + nums[i], dp[i]); + } + } + int m = -1; + for(int i = 0; i < nums.size(); i++) { + m = max(dp[i], m); + } + return m; + } +}; From f01aeb313355c66176eba87e58ee57453cd92caf Mon Sep 17 00:00:00 2001 From: hyeok Date: Sun, 16 Nov 2025 11:44:15 +0900 Subject: [PATCH 07/11] fix two some --- two-sum/dylan-jung.cpp | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/two-sum/dylan-jung.cpp b/two-sum/dylan-jung.cpp index 30e4b7e50b..e1a723947b 100644 --- a/two-sum/dylan-jung.cpp +++ b/two-sum/dylan-jung.cpp @@ -1,17 +1,16 @@ +// TC: O(N), SC: O(N) + class Solution { public: vector twoSum(vector& nums, int target) { - vector> arr; - arr.reserve(nums.size()); + unordered_map m; for(int i = 0; i < nums.size(); i++) { - arr.push_back({nums[i], i}); + m[nums[i]] = i; } - int s = 0; int e = nums.size()-1; - sort(arr.begin(), arr.end()); - while(s < e) { - if(get<0>(arr[s]) + get<0>(arr[e]) > target) e-=1; - else if (get<0>(arr[s]) + get<0>(arr[e]) < target) s+=1; - else return {get<1>(arr[s]), get<1>(arr[e])}; + for(int first = 0; first < nums.size(); first++) { + if(m.count(target - nums[first]) > 0 && first != m[target - nums[first]]) { + return {first, m[target - nums[first]]}; + } } return {}; } From 4aad6fffdb36b55673c470490448d53903953e2e Mon Sep 17 00:00:00 2001 From: hyeok Date: Sun, 16 Nov 2025 11:46:38 +0900 Subject: [PATCH 08/11] fix add comment at contains duplicate --- contains-duplicate/dylan-jung.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/contains-duplicate/dylan-jung.cpp b/contains-duplicate/dylan-jung.cpp index 149505f06c..0030be096c 100644 --- a/contains-duplicate/dylan-jung.cpp +++ b/contains-duplicate/dylan-jung.cpp @@ -1,3 +1,5 @@ +// TC: O(N), SC: O(N) + class Solution { public: bool containsDuplicate(vector& nums) { From 844a96d0bdbb422ffe5c97bc385b8b39c27294cf Mon Sep 17 00:00:00 2001 From: hyeok Date: Sun, 16 Nov 2025 12:04:27 +0900 Subject: [PATCH 09/11] fix top-k-freq-elements --- top-k-frequent-elements/dylan-jung.cpp | 32 ++++++++++++++++---------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/top-k-frequent-elements/dylan-jung.cpp b/top-k-frequent-elements/dylan-jung.cpp index 944f0f1177..4c0b62ec7b 100644 --- a/top-k-frequent-elements/dylan-jung.cpp +++ b/top-k-frequent-elements/dylan-jung.cpp @@ -1,23 +1,31 @@ +// TC: O(N), SC: O(N) class Solution { public: vector topKFrequent(vector& nums, int k) { - unordered_map m; - map> freq; - for(auto i: nums) m[i] += 1; - for(auto item: m) { - auto [k, v] = item; - freq[v].push_back(k); + unordered_map freq; + freq.reserve(nums.size() * 2); + for (int x : nums) { + ++freq[x]; + } + + int n = nums.size(); + vector> bucket(n + 1); + for (auto& p : freq) { + int num = p.first; + int cnt = p.second; + bucket[cnt].push_back(num); } vector ans; ans.reserve(k); - auto it = freq.rbegin(); - while(k > 0) { - auto kth = it->second; - ans.insert(ans.end(), kth.begin(), kth.end()); - k-=kth.size(); - it++; + + for (int count = n; count >= 1 && ans.size() < k; --count) { + for (int num : bucket[count]) { + ans.push_back(num); + if (ans.size() == k) break; + } } + return ans; } }; From f297013700bdce5b6ce127911fabfb092e0a8dd7 Mon Sep 17 00:00:00 2001 From: hyeok Date: Sun, 16 Nov 2025 12:07:04 +0900 Subject: [PATCH 10/11] add comment on longest-consecutive-sequence --- longest-consecutive-sequence/dylan-jung.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/longest-consecutive-sequence/dylan-jung.cpp b/longest-consecutive-sequence/dylan-jung.cpp index 269a50d690..9e366735c7 100644 --- a/longest-consecutive-sequence/dylan-jung.cpp +++ b/longest-consecutive-sequence/dylan-jung.cpp @@ -1,3 +1,5 @@ +// TC: O(N), SC: O(N) + class Solution { public: int longestConsecutive(vector& nums) { From d9582634f6702a07c438affc39faffcd6595500f Mon Sep 17 00:00:00 2001 From: hyeok Date: Sun, 16 Nov 2025 12:09:22 +0900 Subject: [PATCH 11/11] fix: house-robber --- house-robber/dylan-jung.cpp | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/house-robber/dylan-jung.cpp b/house-robber/dylan-jung.cpp index 5a6b364d25..071fd50c0f 100644 --- a/house-robber/dylan-jung.cpp +++ b/house-robber/dylan-jung.cpp @@ -1,20 +1,18 @@ +// TC: O(N), SC: O(1) class Solution { public: - int dp[100] = { 0 }; int rob(vector& nums) { - dp[0] = nums[0]; - if(nums.size() >= 2) { - dp[1] = nums[1]; + if (nums.size() == 1) return nums[0]; + + int prev2 = nums[0]; // dp[i-2] + int prev1 = max(nums[0], nums[1]); // dp[i-1] + + for (int i = 2; i < nums.size(); i++) { + int cur = max(prev1, prev2 + nums[i]); + prev2 = prev1; + prev1 = cur; } - for(int i = 2; i < nums.size(); i++) { - for(int j = 0; j < i-1; j++) { - dp[i] = max(dp[j] + nums[i], dp[i]); - } - } - int m = -1; - for(int i = 0; i < nums.size(); i++) { - m = max(dp[i], m); - } - return m; + + return prev1; } };