From 9788a73f3b2b7f45ef08921094641f48594521e8 Mon Sep 17 00:00:00 2001 From: ys-han00 Date: Fri, 7 Nov 2025 08:57:43 +0900 Subject: [PATCH 1/5] contains duplicate solution --- contains-duplicate/ys-han00.cpp | 36 +++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 contains-duplicate/ys-han00.cpp diff --git a/contains-duplicate/ys-han00.cpp b/contains-duplicate/ys-han00.cpp new file mode 100644 index 000000000..49076ecbc --- /dev/null +++ b/contains-duplicate/ys-han00.cpp @@ -0,0 +1,36 @@ +#include + +class Solution { +public: + bool containsDuplicate(vector& nums) { + sort(nums.begin(), nums.end()); + + for(int i = 0; i < nums.size() - 1; i++) + if(nums[i] == nums[i+1]) + return true; + + return false; + } +}; + +// 첫 시도 및 틀린 풀이 +// 틀린 이유: +// leetcode는 처음 풀어보는데, 백준보다 시간 제한이 엄격함 +// -> 20억 배열 선언하는것만으로도 시간초과 발생 +// +// class Solution { +// public: +// bool containsDuplicate(vector& nums) { +// vector check(2'000'000'001, false); +// int offset = 1'000'000'000; + +// for(int i = 0; i < nums.size(); i++) { +// int idx = nums[i] + offset; + +// if(check[idx] == true) +// return true; +// check[idx] = true; +// } +// return false; +// } +// }; \ No newline at end of file From 2b03b71e6aea7a33971175d0cd59baef946224fc Mon Sep 17 00:00:00 2001 From: ys-han00 Date: Fri, 7 Nov 2025 09:01:55 +0900 Subject: [PATCH 2/5] contains duplicate solution(add newline) --- contains-duplicate/ys-han00.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contains-duplicate/ys-han00.cpp b/contains-duplicate/ys-han00.cpp index 49076ecbc..a2ee58ef7 100644 --- a/contains-duplicate/ys-han00.cpp +++ b/contains-duplicate/ys-han00.cpp @@ -33,4 +33,4 @@ class Solution { // } // return false; // } -// }; \ No newline at end of file +// }; From fb1125322c6d87799351c4fb6a8d94fd0802d0ae Mon Sep 17 00:00:00 2001 From: ys-han00 Date: Sat, 8 Nov 2025 21:46:08 +0900 Subject: [PATCH 3/5] #237 & #240 & #264 solutions --- house-robber/ys-han00.cpp | 24 +++++++++++++++++++ longest-consecutive-sequence/ys-han00.cpp | 25 +++++++++++++++++++ top-k-frequent-elements/ys-han00.cpp | 29 +++++++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 house-robber/ys-han00.cpp create mode 100644 longest-consecutive-sequence/ys-han00.cpp create mode 100644 top-k-frequent-elements/ys-han00.cpp diff --git a/house-robber/ys-han00.cpp b/house-robber/ys-han00.cpp new file mode 100644 index 000000000..892370b9e --- /dev/null +++ b/house-robber/ys-han00.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + int rob(vector& nums) { + vector dp(nums.size(), 0); + + dp[0] = nums[0]; + if(dp.size() == 1) + return dp[0]; + + dp[1] = nums[1]; + int ans = max(dp[0], dp[1]); + + for(int i = 2; i < dp.size(); i++) { + int maxi = -1; + for(int j = 0; j < i - 1; j++) + maxi = max(dp[j], maxi); + dp[i] = maxi + nums[i]; + ans = max(ans, dp[i]); + } + + return ans; + } +}; + diff --git a/longest-consecutive-sequence/ys-han00.cpp b/longest-consecutive-sequence/ys-han00.cpp new file mode 100644 index 000000000..c72cb78e2 --- /dev/null +++ b/longest-consecutive-sequence/ys-han00.cpp @@ -0,0 +1,25 @@ +#include + +class Solution { +public: + int longestConsecutive(vector& nums) { + if(nums.size() == 0) + return 0; + + sort(nums.begin(), nums.end()); + + int ans = 1, cnt = 1; + for(int i = 1; i < nums.size(); i++) { + if(nums[i] == nums[i - 1]) + continue; + if(nums[i] - 1 == nums[i - 1]) + cnt++; + else + cnt = 1; + ans = max(cnt, ans); + } + + return ans; + } +}; + diff --git a/top-k-frequent-elements/ys-han00.cpp b/top-k-frequent-elements/ys-han00.cpp new file mode 100644 index 000000000..c37c5b37f --- /dev/null +++ b/top-k-frequent-elements/ys-han00.cpp @@ -0,0 +1,29 @@ +#include + +class Solution { +public: + vector topKFrequent(vector& nums, int k) { + map count; + + for(int i = 0; i < nums.size(); i++) { + if(count.find(nums[i]) == count.end()) + count[nums[i]] = 1; + else + count[nums[i]]++; + } + + vector> cnt(count.begin(), count.end()); + sort(cnt.begin(), cnt.end(), cmp); + + vector ans; + for(int i = 0; i < k; i++) + ans.push_back(cnt[i].first); + + return ans; + } + + static bool cmp(const pair& a, const pair& b) { + return a.second > b.second; + } +}; + From f4a09349557b0e9a2d13b0afb3cc23a69a2d6b30 Mon Sep 17 00:00:00 2001 From: ys-han00 Date: Sat, 8 Nov 2025 21:50:29 +0900 Subject: [PATCH 4/5] #219 solution --- two-sum/ys-han00.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 two-sum/ys-han00.cpp diff --git a/two-sum/ys-han00.cpp b/two-sum/ys-han00.cpp new file mode 100644 index 000000000..379d113a0 --- /dev/null +++ b/two-sum/ys-han00.cpp @@ -0,0 +1,21 @@ +#include + +class Solution { +public: + vector twoSum(vector& nums, int target) { + vector> num_idx; + for(int i = 0; i < nums.size(); i++) + num_idx.push_back({nums[i], i}); + + sort(num_idx.begin(), num_idx.end()); + + int left = 0, right = nums.size() - 1; + while(1) { + if (num_idx[left].first + num_idx[right].first == target) + return vector({num_idx[left].second, num_idx[right].second}); + if(num_idx[left].first + num_idx[right].first < target) left++; + else right--; + } + } +}; + From f65456e5b26453b7b516887a417072bc2587657f Mon Sep 17 00:00:00 2001 From: ys-han00 Date: Sat, 8 Nov 2025 21:52:53 +0900 Subject: [PATCH 5/5] contains duplicate solution(add new line) --- contains-duplicate/ys-han00.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/contains-duplicate/ys-han00.cpp b/contains-duplicate/ys-han00.cpp index a2ee58ef7..b9031ab98 100644 --- a/contains-duplicate/ys-han00.cpp +++ b/contains-duplicate/ys-han00.cpp @@ -34,3 +34,4 @@ class Solution { // return false; // } // }; +