From e9833ebff650a99f263d7d060aef0ed17671370e Mon Sep 17 00:00:00 2001 From: obzva Date: Mon, 12 Aug 2024 00:10:15 +0900 Subject: [PATCH 1/5] week1 solutions - contains duplicate - kth smallest element in a bst - number of 1 bits - palindromic substrings - top k frequent elements --- contains-duplicate/flynn.cpp | 24 +++++++++++++ kth-smallest-element-in-a-bst/flynn.cpp | 37 ++++++++++++++++++++ number-of-1-bits/flynn.cpp | 21 +++++++++++ palindromic-substrings/flynn.cpp | 33 ++++++++++++++++++ top-k-frequent-elements/flynn.cpp | 46 +++++++++++++++++++++++++ 5 files changed, 161 insertions(+) create mode 100644 contains-duplicate/flynn.cpp create mode 100644 kth-smallest-element-in-a-bst/flynn.cpp create mode 100644 number-of-1-bits/flynn.cpp create mode 100644 palindromic-substrings/flynn.cpp create mode 100644 top-k-frequent-elements/flynn.cpp diff --git a/contains-duplicate/flynn.cpp b/contains-duplicate/flynn.cpp new file mode 100644 index 000000000..63200eb92 --- /dev/null +++ b/contains-duplicate/flynn.cpp @@ -0,0 +1,24 @@ +/** + * for given size of input nums N, + * + * Time complexity: O(N) + * - iteration: O(N) + * - unorderd_set find method: O(1) on average + * - unorderd_set insert method: O(1) on average + * + * Space complexity: O(N) + */ + +class Solution { +public: + bool containsDuplicate(vector& nums) { + unordered_set us; + + auto it = nums.begin(); + while (it != nums.end()) { + if (us.find(*it) == us.end()) us.insert(*(it++)); + else return true; + } + return false; + } +}; \ No newline at end of file diff --git a/kth-smallest-element-in-a-bst/flynn.cpp b/kth-smallest-element-in-a-bst/flynn.cpp new file mode 100644 index 000000000..01b4bec9f --- /dev/null +++ b/kth-smallest-element-in-a-bst/flynn.cpp @@ -0,0 +1,37 @@ +/** + * For the height H of the given BST, + * + * Time complexity: O(H) at worst + * + * Space complexity: O(H + K) at worst + * - call stack + additional vector to save nums + */ + +/** + * 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: + void inorder(TreeNode* node, vector& v, int max_size) { + if (!node || v.size() == max_size) return; + + if (node->left) inorder(node->left, v, max_size); + v.push_back(node->val); + if (node->right) inorder(node->right, v, max_size); + } + + int kthSmallest(TreeNode* root, int k) { + vector nums; + inorder(root, nums, k); + + return nums[k - 1]; + } +}; \ No newline at end of file diff --git a/number-of-1-bits/flynn.cpp b/number-of-1-bits/flynn.cpp new file mode 100644 index 000000000..4ef70dd87 --- /dev/null +++ b/number-of-1-bits/flynn.cpp @@ -0,0 +1,21 @@ +/** + * For given integer N, + * + * Time complexity: O(logN) + * + * Space complexity: O(1) + */ + +class Solution { +public: + int hammingWeight(int n) { + int res = 0; + + while (n) { + if (n & 1) res++; + n >>= 1; + } + + return res; + } +}; \ No newline at end of file diff --git a/palindromic-substrings/flynn.cpp b/palindromic-substrings/flynn.cpp new file mode 100644 index 000000000..988e4f437 --- /dev/null +++ b/palindromic-substrings/flynn.cpp @@ -0,0 +1,33 @@ +/** + * For the length N of given string s, + * + * Time complexity: O(N^3) + * + * Space complexity: O(1) + */ + +class Solution { +public: + int countSubstrings(string s) { + int res = 0; + + for (int i = 0; i < s.size(); i++) { + for (int j = i; j < s.size(); j++) { + int start = i, end = j, flag = 0; + + while (start <= end) { + if (s[start] != s[end]) { + flag = 1; + break; + } + start++; + end--; + } + + if (!flag) res++; + } + } + + return res; + } +}; \ No newline at end of file diff --git a/top-k-frequent-elements/flynn.cpp b/top-k-frequent-elements/flynn.cpp new file mode 100644 index 000000000..86b2924fe --- /dev/null +++ b/top-k-frequent-elements/flynn.cpp @@ -0,0 +1,46 @@ +/** + * For given size of nums N and K, + * + * Time complexity: O(N + MlogM + KlogM) <= O(NlogN) + * - the first iteration: O(N) + * - N times of + * - unordered_map find: O(1) on average + * - unordered_map insert: O(1) on average + * - the second iteration: O(MlogM) such that M is the number of unique numbers + * - M times of + * - priority_queue push: O(logM) + * - the last iteration of making result: O(KlogM) + * - K times of + * - priority_queue pop: O(logM) + * + * Space complexity: O(N) at worst + */ + +class Solution { +public: + vector topKFrequent(vector& nums, int k) { + vector res; + priority_queue> pq; + unorderd_map m; + + auto nums_it = nums.begin(); + while (nums_it != nums.end()) { + if (m.find(*nums_it) == m.end()) m.insert({*nums_it, 1}); + else m[*nums_it]++; + nums_it++; + } + + auto m_it = m.begin(); + while (m_it != m.end()) { + pq.push({(*m_it).second, (*m_it).first}); + m_it++; + } + + while (k--) { + res.push_back(pq.top().second); + pq.pop(); + } + + return res; + } +}; \ No newline at end of file From 59d14959f509519b3100fc3415fd9641bd0bdbc6 Mon Sep 17 00:00:00 2001 From: obzva Date: Mon, 12 Aug 2024 22:37:45 +0900 Subject: [PATCH 2/5] Refactor: Change variable names --- contains-duplicate/flynn.cpp | 4 ++-- palindromic-substrings/flynn.cpp | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/contains-duplicate/flynn.cpp b/contains-duplicate/flynn.cpp index 63200eb92..c45432e12 100644 --- a/contains-duplicate/flynn.cpp +++ b/contains-duplicate/flynn.cpp @@ -12,11 +12,11 @@ class Solution { public: bool containsDuplicate(vector& nums) { - unordered_set us; + unordered_set unique_numbers; auto it = nums.begin(); while (it != nums.end()) { - if (us.find(*it) == us.end()) us.insert(*(it++)); + if (unique_numbers.find(*it) == unique_numbers.end()) unique_numbers.insert(*(it++)); else return true; } return false; diff --git a/palindromic-substrings/flynn.cpp b/palindromic-substrings/flynn.cpp index 988e4f437..bbd428818 100644 --- a/palindromic-substrings/flynn.cpp +++ b/palindromic-substrings/flynn.cpp @@ -13,18 +13,19 @@ class Solution { for (int i = 0; i < s.size(); i++) { for (int j = i; j < s.size(); j++) { - int start = i, end = j, flag = 0; + int start = i, end = j; + bool is_palindrome = true; while (start <= end) { if (s[start] != s[end]) { - flag = 1; + is_palindrome = false; break; } start++; end--; } - if (!flag) res++; + if (is_palindrome) res++; } } From b5b10ee6b87833890eb87684226b38ea07cb5b4c Mon Sep 17 00:00:00 2001 From: obzva Date: Mon, 12 Aug 2024 22:50:51 +0900 Subject: [PATCH 3/5] Fix: Typo --- top-k-frequent-elements/flynn.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/top-k-frequent-elements/flynn.cpp b/top-k-frequent-elements/flynn.cpp index 86b2924fe..a5a54260c 100644 --- a/top-k-frequent-elements/flynn.cpp +++ b/top-k-frequent-elements/flynn.cpp @@ -21,7 +21,7 @@ class Solution { vector topKFrequent(vector& nums, int k) { vector res; priority_queue> pq; - unorderd_map m; + unordered_map m; auto nums_it = nums.begin(); while (nums_it != nums.end()) { From 7662c683548206a158f0d1c3b1dca0ab3fae993a Mon Sep 17 00:00:00 2001 From: obzva Date: Mon, 12 Aug 2024 22:59:37 +0900 Subject: [PATCH 4/5] Comment: Big-O analysis --- kth-smallest-element-in-a-bst/flynn.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/kth-smallest-element-in-a-bst/flynn.cpp b/kth-smallest-element-in-a-bst/flynn.cpp index 01b4bec9f..1ab20fdfc 100644 --- a/kth-smallest-element-in-a-bst/flynn.cpp +++ b/kth-smallest-element-in-a-bst/flynn.cpp @@ -1,10 +1,14 @@ /** * For the height H of the given BST, * - * Time complexity: O(H) at worst + * Time complexity: O(max(H, K)) + * - if H > K, O(H) at worst + * - else, O(K) * - * Space complexity: O(H + K) at worst - * - call stack + additional vector to save nums + * Space complexity: O(H > K ? H + K : K) + * - additional vector to save nums O(K) + * - if H > K, call stack O(H) + * - else, O(K) */ /** From 4c50d543b0cb8e31ffd893e7cf9e26f9dbec0e40 Mon Sep 17 00:00:00 2001 From: obzva Date: Mon, 12 Aug 2024 23:01:08 +0900 Subject: [PATCH 5/5] Refactor: new line at the end of the code --- contains-duplicate/flynn.cpp | 2 +- kth-smallest-element-in-a-bst/flynn.cpp | 2 +- number-of-1-bits/flynn.cpp | 2 +- palindromic-substrings/flynn.cpp | 2 +- top-k-frequent-elements/flynn.cpp | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/contains-duplicate/flynn.cpp b/contains-duplicate/flynn.cpp index c45432e12..11404be57 100644 --- a/contains-duplicate/flynn.cpp +++ b/contains-duplicate/flynn.cpp @@ -21,4 +21,4 @@ class Solution { } return false; } -}; \ No newline at end of file +}; diff --git a/kth-smallest-element-in-a-bst/flynn.cpp b/kth-smallest-element-in-a-bst/flynn.cpp index 1ab20fdfc..10b4e7a3b 100644 --- a/kth-smallest-element-in-a-bst/flynn.cpp +++ b/kth-smallest-element-in-a-bst/flynn.cpp @@ -38,4 +38,4 @@ class Solution { return nums[k - 1]; } -}; \ No newline at end of file +}; diff --git a/number-of-1-bits/flynn.cpp b/number-of-1-bits/flynn.cpp index 4ef70dd87..ac095d247 100644 --- a/number-of-1-bits/flynn.cpp +++ b/number-of-1-bits/flynn.cpp @@ -18,4 +18,4 @@ class Solution { return res; } -}; \ No newline at end of file +}; diff --git a/palindromic-substrings/flynn.cpp b/palindromic-substrings/flynn.cpp index bbd428818..99ccfe48f 100644 --- a/palindromic-substrings/flynn.cpp +++ b/palindromic-substrings/flynn.cpp @@ -31,4 +31,4 @@ class Solution { return res; } -}; \ No newline at end of file +}; diff --git a/top-k-frequent-elements/flynn.cpp b/top-k-frequent-elements/flynn.cpp index a5a54260c..26cc6b7c6 100644 --- a/top-k-frequent-elements/flynn.cpp +++ b/top-k-frequent-elements/flynn.cpp @@ -43,4 +43,4 @@ class Solution { return res; } -}; \ No newline at end of file +};