diff --git a/contains-duplicate/heozeop.cpp b/contains-duplicate/heozeop.cpp new file mode 100644 index 000000000..8bd82c1e4 --- /dev/null +++ b/contains-duplicate/heozeop.cpp @@ -0,0 +1,20 @@ +// Time Complexity : O(n) +// Spatial Complexity : O(n) + +#include + +class Solution { +public: + bool containsDuplicate(vector& nums) { + set count; + + for(int num : nums) { + if (count.find(num) != count.end()) { + return true; + } + count.insert(num); + } + + return false; + } +}; diff --git a/kth-smallest-element-in-a-bst/heozeop.cpp b/kth-smallest-element-in-a-bst/heozeop.cpp new file mode 100644 index 000000000..ade7951d1 --- /dev/null +++ b/kth-smallest-element-in-a-bst/heozeop.cpp @@ -0,0 +1,46 @@ +// Time Complexity: O(nlogn) +// Spatial Complexity O(n) + +#include +#include +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + int kthSmallest(TreeNode* root, int k) { + queue q; + q.push(root); + + vector numbers; + TreeNode *curNode, *nextNode; + while(!q.empty()) { + curNode = q.front(); + q.pop(); + + numbers.push_back(curNode->val); + + nextNode = curNode->left; + if(nextNode != nullptr) { + q.push(nextNode); + } + + nextNode = curNode->right; + if(nextNode != nullptr) { + q.push(nextNode); + } + } + + sort(numbers.begin(), numbers.end()); + return numbers[k - 1]; + } +}; + diff --git a/number-of-1-bits/heozeop.cpp b/number-of-1-bits/heozeop.cpp new file mode 100644 index 000000000..550590e79 --- /dev/null +++ b/number-of-1-bits/heozeop.cpp @@ -0,0 +1,19 @@ +// Time Complexity: O(log(n)) +// Spatial Complexity: O(1) + +class Solution { +public: + int hammingWeight(int n) { + int numberOf1Bit = 0; + while(n) { + if(n % 2) { + numberOf1Bit += 1; + } + + n = n >> 1; + } + + return numberOf1Bit; + } +}; + diff --git a/palindromic-substrings/heozeop.cpp b/palindromic-substrings/heozeop.cpp new file mode 100644 index 000000000..0405179b9 --- /dev/null +++ b/palindromic-substrings/heozeop.cpp @@ -0,0 +1,71 @@ +// Time Complexity: O(n^3) +// Spatial Complexity: O(1) + +// #include +// #include + +class Solution { +private: + bool isPalindrom(string s) { + int sLength = s.length(); + + for(int i = 0; i < sLength / 2; ++i) { + if(s[i] != s[sLength - 1 - i]) { + return false; + } + } + + return true; + } + +public: + int countSubstrings(string s) { + int answer = 0; + + int sLength = s.length(); + string temp; + for(int i = 1; i <= sLength; ++i) { + for(int j = 0; j + i <= sLength; ++j) { + temp = s.substr(j, i); + + if(this->isPalindrom(temp)) { + ++answer; + } + } + } + + return answer; + } +}; + +// DP version +// O(n^2) +// #include +// #include + +class Solution2 { +public: + int countSubstrings(string str) { + int answer = 0; + + int n = str.length(); + vector> dp(n + 1, vector(n + 1, false)); + + for(int e = 0; e < n; ++e) { + for(int s = 0; s <= e; ++s) { + if (str[s] != str[e]) { + continue; + } + + if (s != e && s + 1 != e && !dp[s + 1][e - 1]) { + continue; + } + + ++answer; + dp[s][e] = true; + } + } + + return answer; + } +}; diff --git a/top-k-frequent-elements/heozeop.cpp b/top-k-frequent-elements/heozeop.cpp new file mode 100644 index 000000000..7d59c4bf3 --- /dev/null +++ b/top-k-frequent-elements/heozeop.cpp @@ -0,0 +1,38 @@ +// Time complexity O(nlogn) +// Spatal Complexity O(n) + +#include +#include + +using namespace std; + + +bool cmp(const pair& a, const pair& b) { + return a.second > b.second; +} + +class Solution { +public: + vector topKFrequent(vector& nums, int k) { + map countMap; + + for(int num : nums) { + if(countMap.find(num) == countMap.end()) { + countMap.insert({num, 0}); + } + + ++countMap[num]; + } + + vector> vec(countMap.begin(), countMap.end()); + sort(vec.begin(), vec.end(), cmp); + + vector answer; + for(int i = 0; i < k; ++i) { + answer.push_back(vec[i].first); + } + + return answer; + } +}; +