diff --git a/construct-binary-tree-from-preorder-and-inorder-traversal/flynn.cpp b/construct-binary-tree-from-preorder-and-inorder-traversal/flynn.cpp new file mode 100644 index 000000000..9f7593623 --- /dev/null +++ b/construct-binary-tree-from-preorder-and-inorder-traversal/flynn.cpp @@ -0,0 +1,49 @@ +/** + * For the number of given nodes N, + * + * Time complexity: O(N) + * + * Space complexity: O(N) at worst + */ + +/** + * 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: + TreeNode* buildTree(vector& preorder, vector& inorder) { + unordered_map inorder_index_map; + stack tree_stack; + + for (int i = 0; i < inorder.size(); i++) inorder_index_map[inorder[i]] = i; + + TreeNode* root = new TreeNode(preorder[0]); + tree_stack.push(root); + + for (int i = 1; i < preorder.size(); i++) { + TreeNode* curr = new TreeNode(preorder[i]); + + if (inorder_index_map[curr->val] < inorder_index_map[tree_stack.top()->val]) { + tree_stack.top()->left = curr; + } else { + TreeNode* parent; + while (!tree_stack.empty() && inorder_index_map[curr->val] > inorder_index_map[tree_stack.top()->val]) { + parent = tree_stack.top(); + tree_stack.pop(); + } + parent->right = curr; + } + tree_stack.push(curr); + } + + return root; + } +}; diff --git a/counting-bits/flynn.cpp b/counting-bits/flynn.cpp new file mode 100644 index 000000000..5067bda98 --- /dev/null +++ b/counting-bits/flynn.cpp @@ -0,0 +1,22 @@ +/** + * Time complexity: O(N) + * + * Space complexity: O(1) + */ + +class Solution { +public: + vector countBits(int n) { + vector res; + res.push_back(0); + + int i = 1, j = 1; + while (i <= n) { + res.push_back(res[i - j] + 1); + i++; + if (i == j * 2) j *= 2; + } + + return res; + } +}; diff --git a/decode-ways/flynn.cpp b/decode-ways/flynn.cpp new file mode 100644 index 000000000..05d9b6eec --- /dev/null +++ b/decode-ways/flynn.cpp @@ -0,0 +1,53 @@ +/** + * For the length of the given string N, + * + * Time complexity: O(N) + * + * Space complexity: O(N) + */ + +class Solution { +public: + int numDecodings(string s) { + if (s[0] == '0') return 0; + + int memo[s.size() + 1]; + + fill(memo, memo + s.size() + 1, 0); + memo[0] = 1; + memo[1] = 1; + + for (int i = 2; i <= s.size(); i++) { + int s_i = i - 1; + if (s[s_i] != '0') memo[i] = memo[i - 1]; + int two_digits = stoi(s.substr(s_i - 1, 2)); + if (10 <= two_digits && two_digits <= 26) memo[i] += memo[i - 2]; + } + + return memo[s.size()]; + } +}; + +/** + * Space complexity O(1) solution + */ + +// class Solution { +// public: +// int numDecodings(string s) { +// if (s[0] == '0') return 0; + +// int one_digit_memo = 1, two_digit_memo = 1; + +// for (int i = 1; i < s.size(); i++) { +// int tmp = 0; +// if (s[i] != '0') tmp = one_digit_memo; +// int two_digits = stoi(s.substr(i - 1, 2)); +// if (10 <= two_digits && two_digits <= 26) tmp += two_digit_memo; +// two_digit_memo = one_digit_memo; +// one_digit_memo = tmp; +// } + +// return one_digit_memo; +// } +// }; diff --git a/encode-and-decode-strings/flynn.cpp b/encode-and-decode-strings/flynn.cpp new file mode 100644 index 000000000..955572279 --- /dev/null +++ b/encode-and-decode-strings/flynn.cpp @@ -0,0 +1,57 @@ +/** + * For the number of given strings N, and the length of the longest string M, + * + * Encode + * - Time complexity: O(N) + * - Space complexity: O(1) + * + * Decode + * - Time complexity: O(NM) + * - Space complexity: O(M) + */ + +class Codec { +public: + + // Encodes a list of strings to a single string. + string encode(vector& strs) { + string res = ""; + for (auto str : strs) { + res += to_string(str.size()); + res.push_back('.'); + res += str; + } + return res; + } + + // Decodes a single string to a list of strings. + vector decode(string s) { + vector res; + + auto it = s.begin(); + while (it != s.end()) { + int str_size = 0; + string tmp = ""; + + while (*it != '.') { + str_size = str_size * 10 + (*it - '0'); + it++; + } + + it++; + + for (int i = 0; i < str_size; i++) { + tmp.push_back(*it); + it++; + } + + res.push_back(tmp); + } + + return res; + } +}; + +// Your Codec object will be instantiated and called as such: +// Codec codec; +// codec.decode(codec.encode(strs)); diff --git a/valid-anagram/flynn.cpp b/valid-anagram/flynn.cpp new file mode 100644 index 000000000..b6df7007e --- /dev/null +++ b/valid-anagram/flynn.cpp @@ -0,0 +1,22 @@ +/** + * For length of given strings N, + * + * Time complexity: O(N) + * - iteration for given strings + * + * Space complexity: O(1) + * - the size of the container `count` is constant + */ + +class Solution { +public: + bool isAnagram(string s, string t) { + int count[26] = {0}; + + for (auto c : s) count[c - 'a']++; + for (auto c : t) count[c - 'a']--; + + for (int i = 0; i < 26; i++) if (count[i]) return false; + return true; + } +};