From 87bb8ead91b6f7306993c0b9ca6a2ad38cba05ba Mon Sep 17 00:00:00 2001 From: crispy Date: Mon, 12 Aug 2024 20:57:25 +0900 Subject: [PATCH 1/9] solve: contains-duplicate --- contains-duplicate/crispy.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 contains-duplicate/crispy.cpp diff --git a/contains-duplicate/crispy.cpp b/contains-duplicate/crispy.cpp new file mode 100644 index 000000000..72cc29bdd --- /dev/null +++ b/contains-duplicate/crispy.cpp @@ -0,0 +1,18 @@ + +#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; + } +}; From e4fef828516adfb99601c6942346d176c0dfffbf Mon Sep 17 00:00:00 2001 From: crispy Date: Mon, 12 Aug 2024 21:01:32 +0900 Subject: [PATCH 2/9] renmae to fit with contribution guide --- contains-duplicate/{crispy.cpp => heozeop.cpp} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename contains-duplicate/{crispy.cpp => heozeop.cpp} (100%) diff --git a/contains-duplicate/crispy.cpp b/contains-duplicate/heozeop.cpp similarity index 100% rename from contains-duplicate/crispy.cpp rename to contains-duplicate/heozeop.cpp From 8f9edb205699b80ac81e37d4f2952ba6910f14f3 Mon Sep 17 00:00:00 2001 From: crispy Date: Mon, 12 Aug 2024 21:06:48 +0900 Subject: [PATCH 3/9] feat: add complexity --- contains-duplicate/heozeop.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/contains-duplicate/heozeop.cpp b/contains-duplicate/heozeop.cpp index 72cc29bdd..8bd82c1e4 100644 --- a/contains-duplicate/heozeop.cpp +++ b/contains-duplicate/heozeop.cpp @@ -1,3 +1,5 @@ +// Time Complexity : O(n) +// Spatial Complexity : O(n) #include From 8966444e332b640dc31dfadbd2de49b60b076515 Mon Sep 17 00:00:00 2001 From: crispy Date: Wed, 14 Aug 2024 17:51:09 +0900 Subject: [PATCH 4/9] solve: number of 1 bit --- number-of-1-bits/heozeop.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 number-of-1-bits/heozeop.cpp diff --git a/number-of-1-bits/heozeop.cpp b/number-of-1-bits/heozeop.cpp new file mode 100644 index 000000000..00bbb5af5 --- /dev/null +++ b/number-of-1-bits/heozeop.cpp @@ -0,0 +1,16 @@ +class Solution { +public: + int hammingWeight(int n) { + int numberOf1Bit = 0; + while(n) { + if(n % 2) { + numberOf1Bit += 1; + } + + n = n >> 1; + } + + return numberOf1Bit; + } +}; + From 3f96089b300eae6f6da1931f0323101385bce8b0 Mon Sep 17 00:00:00 2001 From: crispy Date: Wed, 14 Aug 2024 17:52:24 +0900 Subject: [PATCH 5/9] docs: add tc and sc --- number-of-1-bits/heozeop.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/number-of-1-bits/heozeop.cpp b/number-of-1-bits/heozeop.cpp index 00bbb5af5..550590e79 100644 --- a/number-of-1-bits/heozeop.cpp +++ b/number-of-1-bits/heozeop.cpp @@ -1,3 +1,6 @@ +// Time Complexity: O(log(n)) +// Spatial Complexity: O(1) + class Solution { public: int hammingWeight(int n) { From ec611d98647c5a39a47269b9cc080d5879858590 Mon Sep 17 00:00:00 2001 From: crispy Date: Wed, 14 Aug 2024 18:22:48 +0900 Subject: [PATCH 6/9] solve: top k frequent elements --- top-k-frequent-elements/heozeop.cpp | 38 +++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 top-k-frequent-elements/heozeop.cpp 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; + } +}; + From c844df2c2814bd5d4f6ae02af1929614a2a83d03 Mon Sep 17 00:00:00 2001 From: crispy Date: Wed, 14 Aug 2024 22:07:38 +0900 Subject: [PATCH 7/9] solve: kth smallest in bst --- kth-smallest-element-in-a-bst/heozeop.cpp | 46 +++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 kth-smallest-element-in-a-bst/heozeop.cpp 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]; + } +}; + From 42d54501de5fe746a99b41d59eda55d2311258f3 Mon Sep 17 00:00:00 2001 From: crispy Date: Wed, 14 Aug 2024 23:07:14 +0900 Subject: [PATCH 8/9] solve: palindromic substring --- palindromic-substrings/heozeop.cpp | 40 ++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 palindromic-substrings/heozeop.cpp diff --git a/palindromic-substrings/heozeop.cpp b/palindromic-substrings/heozeop.cpp new file mode 100644 index 000000000..9e8f9169d --- /dev/null +++ b/palindromic-substrings/heozeop.cpp @@ -0,0 +1,40 @@ +// 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; + } +}; + From 1250979714d106d1bdaa9e6a10224d50d1a77888 Mon Sep 17 00:00:00 2001 From: crispy Date: Wed, 14 Aug 2024 23:32:47 +0900 Subject: [PATCH 9/9] feat: add dp version of palindromic substring --- palindromic-substrings/heozeop.cpp | 35 ++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/palindromic-substrings/heozeop.cpp b/palindromic-substrings/heozeop.cpp index 9e8f9169d..0405179b9 100644 --- a/palindromic-substrings/heozeop.cpp +++ b/palindromic-substrings/heozeop.cpp @@ -1,8 +1,8 @@ // Time Complexity: O(n^3) // Spatial Complexity: O(1) -#include -#include +// #include +// #include class Solution { private: @@ -38,3 +38,34 @@ class Solution { } }; +// 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; + } +};