diff --git a/contains-duplicate/oyeong011.cpp b/contains-duplicate/oyeong011.cpp new file mode 100644 index 000000000..3a781dbec --- /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; + } +}; diff --git a/house-robber/oyeong011.cpp b/house-robber/oyeong011.cpp new file mode 100644 index 000000000..bb108d698 --- /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]; + } +}; diff --git a/longest-consecutive-sequence/oyeong011.cpp b/longest-consecutive-sequence/oyeong011.cpp new file mode 100644 index 000000000..4152c4c9b --- /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; + } +}; diff --git a/top-k-frequent-elements/oyeong011.cpp b/top-k-frequent-elements/oyeong011.cpp new file mode 100644 index 000000000..7abe2cfad --- /dev/null +++ b/top-k-frequent-elements/oyeong011.cpp @@ -0,0 +1,16 @@ +class Solution { +public: + vector topKFrequent(vector& nums, int k) { + map mp; + priority_queue> pq; + vector ans; + + 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; + } +}; diff --git a/valid-palindrome/oyeong011.cpp b/valid-palindrome/oyeong011.cpp new file mode 100644 index 000000000..9b028a11b --- /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; + } +};