From 1c65b023edd0bf34c1615470050f46b24bbc5110 Mon Sep 17 00:00:00 2001 From: oyeong011 Date: Fri, 6 Dec 2024 11:02:35 +0900 Subject: [PATCH 1/7] contains duplicate sol --- contains-duplicate/oyeong011.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 contains-duplicate/oyeong011.cpp diff --git a/contains-duplicate/oyeong011.cpp b/contains-duplicate/oyeong011.cpp new file mode 100644 index 000000000..df718d29c --- /dev/null +++ b/contains-duplicate/oyeong011.cpp @@ -0,0 +1,14 @@ +#include +using namespace std; +class Solution { +public: + bool containsDuplicate(vector& nums) { + unordered_map mp; + for(int a : nums){ + if(++mp[a] >= 2){ + return true; + } + } + return false; + } +}; \ No newline at end of file From 9bc5e4ea20734c62c69ad73b9c7974ca9fd5c798 Mon Sep 17 00:00:00 2001 From: oyeong011 Date: Fri, 6 Dec 2024 11:03:09 +0900 Subject: [PATCH 2/7] valid palindrome sol --- valid-palindrome/oyeong011.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 valid-palindrome/oyeong011.cpp diff --git a/valid-palindrome/oyeong011.cpp b/valid-palindrome/oyeong011.cpp new file mode 100644 index 000000000..1bb0ea79c --- /dev/null +++ b/valid-palindrome/oyeong011.cpp @@ -0,0 +1,16 @@ +class Solution { +public: + bool isPalindrome(string s) { + string clean = ""; + for(char c : s) { + if(isalnum(c)) { + clean += tolower(c); + } + } + + string reversed = clean; + reverse(reversed.begin(), reversed.end()); + + return clean == reversed; + } +}; \ No newline at end of file From 5ffa46d3e5955c76aef694e2f668850cf358e14e Mon Sep 17 00:00:00 2001 From: oyeong011 Date: Fri, 6 Dec 2024 11:03:37 +0900 Subject: [PATCH 3/7] top-k-frequent-elements --- top-k-frequent-elements/oyeong011.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 top-k-frequent-elements/oyeong011.cpp diff --git a/top-k-frequent-elements/oyeong011.cpp b/top-k-frequent-elements/oyeong011.cpp new file mode 100644 index 000000000..63fff0689 --- /dev/null +++ b/top-k-frequent-elements/oyeong011.cpp @@ -0,0 +1,21 @@ +class Solution { +public: + int longestConsecutive(vector& nums) { + const int INF = 987654321; + int temp = INF, ret = 0, cur = 0; + + sort(nums.begin(), nums.end()); + for(int a : nums){ + if(a == temp)continue; + if(temp == INF || temp + 1 == a){ + cur++; temp = a; + } else { + ret = max(ret, cur); + cur = 1; + temp = a; + } + } + ret = max(ret, cur); + return ret; + } +}; \ No newline at end of file From f2027992bafffdafdfcdd85f899721f374b6e52c Mon Sep 17 00:00:00 2001 From: oyeong011 Date: Fri, 6 Dec 2024 11:03:53 +0900 Subject: [PATCH 4/7] house-robber sol --- house-robber/oyeong011.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 house-robber/oyeong011.cpp diff --git a/house-robber/oyeong011.cpp b/house-robber/oyeong011.cpp new file mode 100644 index 000000000..579136f40 --- /dev/null +++ b/house-robber/oyeong011.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + int rob(vector& nums) { + int n = nums.size(); + if(n == 0)return 0; + if(n == 1)return nums[0]; + if(n == 2)return max(nums[0], nums[1]); + + vector dp(n); + dp[0] = nums[0]; + dp[1] = max(nums[0], nums[1]); + for(int i = 2; i < n; i++){ + dp[i] = max(dp[i - 1], dp[i - 2] + nums[i]); + } + return dp[n - 1]; + } +}; \ No newline at end of file From 6322e95eda9dca4ace778dfb3f6df3985b3c9e58 Mon Sep 17 00:00:00 2001 From: oyeong011 Date: Fri, 6 Dec 2024 11:10:32 +0900 Subject: [PATCH 5/7] longest consecutive sequence sol --- longest-consecutive-sequence/oyeong011.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 longest-consecutive-sequence/oyeong011.cpp diff --git a/longest-consecutive-sequence/oyeong011.cpp b/longest-consecutive-sequence/oyeong011.cpp new file mode 100644 index 000000000..2c1b9ded4 --- /dev/null +++ b/longest-consecutive-sequence/oyeong011.cpp @@ -0,0 +1,21 @@ +class Solution { +public: + int longestConsecutive(vector& nums) { + const int INF = 987654321; + int temp = INF, ret = 0, cur = 0; + + sort(nums.begin(), nums.end()); + for(int a : nums){ + if(a == temp)continue; + if(temp == INF || temp + 1 == a){ + cur++; temp = a; + } else { + ret = max(ret, cur); + cur = 1; + temp = a; + } + } + ret = max(ret, cur); + return ret; + } +}; \ No newline at end of file From 88165eca453de66f1a700fc7f92987d226055858 Mon Sep 17 00:00:00 2001 From: oyeong011 Date: Fri, 6 Dec 2024 11:21:09 +0900 Subject: [PATCH 6/7] =?UTF-8?q?=EA=B0=9C=ED=96=89=20=EB=AC=B8=EC=9E=90=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contains-duplicate/oyeong011.cpp | 2 +- house-robber/oyeong011.cpp | 2 +- longest-consecutive-sequence/oyeong011.cpp | 2 +- top-k-frequent-elements/oyeong011.cpp | 2 +- valid-palindrome/oyeong011.cpp | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/contains-duplicate/oyeong011.cpp b/contains-duplicate/oyeong011.cpp index df718d29c..3a781dbec 100644 --- a/contains-duplicate/oyeong011.cpp +++ b/contains-duplicate/oyeong011.cpp @@ -11,4 +11,4 @@ class Solution { } return false; } -}; \ No newline at end of file +}; diff --git a/house-robber/oyeong011.cpp b/house-robber/oyeong011.cpp index 579136f40..bb108d698 100644 --- a/house-robber/oyeong011.cpp +++ b/house-robber/oyeong011.cpp @@ -14,4 +14,4 @@ class Solution { } return dp[n - 1]; } -}; \ No newline at end of file +}; diff --git a/longest-consecutive-sequence/oyeong011.cpp b/longest-consecutive-sequence/oyeong011.cpp index 2c1b9ded4..4152c4c9b 100644 --- a/longest-consecutive-sequence/oyeong011.cpp +++ b/longest-consecutive-sequence/oyeong011.cpp @@ -18,4 +18,4 @@ class Solution { ret = max(ret, cur); return ret; } -}; \ No newline at end of file +}; diff --git a/top-k-frequent-elements/oyeong011.cpp b/top-k-frequent-elements/oyeong011.cpp index 63fff0689..1326f19da 100644 --- a/top-k-frequent-elements/oyeong011.cpp +++ b/top-k-frequent-elements/oyeong011.cpp @@ -18,4 +18,4 @@ class Solution { ret = max(ret, cur); return ret; } -}; \ No newline at end of file +}; diff --git a/valid-palindrome/oyeong011.cpp b/valid-palindrome/oyeong011.cpp index 1bb0ea79c..9b028a11b 100644 --- a/valid-palindrome/oyeong011.cpp +++ b/valid-palindrome/oyeong011.cpp @@ -13,4 +13,4 @@ class Solution { return clean == reversed; } -}; \ No newline at end of file +}; From 7785c4537b1c3bcd6bcce45ed59a491084befd47 Mon Sep 17 00:00:00 2001 From: oyeong011 Date: Mon, 9 Dec 2024 00:14:11 +0900 Subject: [PATCH 7/7] top-k-frequent sol --- top-k-frequent-elements/oyeong011.cpp | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/top-k-frequent-elements/oyeong011.cpp b/top-k-frequent-elements/oyeong011.cpp index 1326f19da..7abe2cfad 100644 --- a/top-k-frequent-elements/oyeong011.cpp +++ b/top-k-frequent-elements/oyeong011.cpp @@ -1,21 +1,16 @@ class Solution { public: - int longestConsecutive(vector& nums) { - const int INF = 987654321; - int temp = INF, ret = 0, cur = 0; + vector topKFrequent(vector& nums, int k) { + map mp; + priority_queue> pq; + vector ans; - sort(nums.begin(), nums.end()); - for(int a : nums){ - if(a == temp)continue; - if(temp == INF || temp + 1 == a){ - cur++; temp = a; - } else { - ret = max(ret, cur); - cur = 1; - temp = a; - } - } - ret = max(ret, cur); - return ret; + for(auto b : nums) mp[b]++; + + for(auto p : mp) pq.push({p.second, p.first}); + + while(k--)ans.push_back(pq.top().second), pq.pop(); + + return ans; } };