Skip to content

Commit 4c472a7

Browse files
authored
Merge pull request #346 from obzva/main
[Flynn] week2
2 parents ac59579 + 3abfd93 commit 4c472a7

File tree

5 files changed

+203
-0
lines changed

5 files changed

+203
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* For the number of given nodes N,
3+
*
4+
* Time complexity: O(N)
5+
*
6+
* Space complexity: O(N) at worst
7+
*/
8+
9+
/**
10+
* Definition for a binary tree node.
11+
* struct TreeNode {
12+
* int val;
13+
* TreeNode *left;
14+
* TreeNode *right;
15+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
16+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
17+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
18+
* };
19+
*/
20+
class Solution {
21+
public:
22+
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
23+
unordered_map<int, int> inorder_index_map;
24+
stack<TreeNode*> tree_stack;
25+
26+
for (int i = 0; i < inorder.size(); i++) inorder_index_map[inorder[i]] = i;
27+
28+
TreeNode* root = new TreeNode(preorder[0]);
29+
tree_stack.push(root);
30+
31+
for (int i = 1; i < preorder.size(); i++) {
32+
TreeNode* curr = new TreeNode(preorder[i]);
33+
34+
if (inorder_index_map[curr->val] < inorder_index_map[tree_stack.top()->val]) {
35+
tree_stack.top()->left = curr;
36+
} else {
37+
TreeNode* parent;
38+
while (!tree_stack.empty() && inorder_index_map[curr->val] > inorder_index_map[tree_stack.top()->val]) {
39+
parent = tree_stack.top();
40+
tree_stack.pop();
41+
}
42+
parent->right = curr;
43+
}
44+
tree_stack.push(curr);
45+
}
46+
47+
return root;
48+
}
49+
};

counting-bits/flynn.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Time complexity: O(N)
3+
*
4+
* Space complexity: O(1)
5+
*/
6+
7+
class Solution {
8+
public:
9+
vector<int> countBits(int n) {
10+
vector<int> res;
11+
res.push_back(0);
12+
13+
int i = 1, j = 1;
14+
while (i <= n) {
15+
res.push_back(res[i - j] + 1);
16+
i++;
17+
if (i == j * 2) j *= 2;
18+
}
19+
20+
return res;
21+
}
22+
};

decode-ways/flynn.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* For the length of the given string N,
3+
*
4+
* Time complexity: O(N)
5+
*
6+
* Space complexity: O(N)
7+
*/
8+
9+
class Solution {
10+
public:
11+
int numDecodings(string s) {
12+
if (s[0] == '0') return 0;
13+
14+
int memo[s.size() + 1];
15+
16+
fill(memo, memo + s.size() + 1, 0);
17+
memo[0] = 1;
18+
memo[1] = 1;
19+
20+
for (int i = 2; i <= s.size(); i++) {
21+
int s_i = i - 1;
22+
if (s[s_i] != '0') memo[i] = memo[i - 1];
23+
int two_digits = stoi(s.substr(s_i - 1, 2));
24+
if (10 <= two_digits && two_digits <= 26) memo[i] += memo[i - 2];
25+
}
26+
27+
return memo[s.size()];
28+
}
29+
};
30+
31+
/**
32+
* Space complexity O(1) solution
33+
*/
34+
35+
// class Solution {
36+
// public:
37+
// int numDecodings(string s) {
38+
// if (s[0] == '0') return 0;
39+
40+
// int one_digit_memo = 1, two_digit_memo = 1;
41+
42+
// for (int i = 1; i < s.size(); i++) {
43+
// int tmp = 0;
44+
// if (s[i] != '0') tmp = one_digit_memo;
45+
// int two_digits = stoi(s.substr(i - 1, 2));
46+
// if (10 <= two_digits && two_digits <= 26) tmp += two_digit_memo;
47+
// two_digit_memo = one_digit_memo;
48+
// one_digit_memo = tmp;
49+
// }
50+
51+
// return one_digit_memo;
52+
// }
53+
// };

encode-and-decode-strings/flynn.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* For the number of given strings N, and the length of the longest string M,
3+
*
4+
* Encode
5+
* - Time complexity: O(N)
6+
* - Space complexity: O(1)
7+
*
8+
* Decode
9+
* - Time complexity: O(NM)
10+
* - Space complexity: O(M)
11+
*/
12+
13+
class Codec {
14+
public:
15+
16+
// Encodes a list of strings to a single string.
17+
string encode(vector<string>& strs) {
18+
string res = "";
19+
for (auto str : strs) {
20+
res += to_string(str.size());
21+
res.push_back('.');
22+
res += str;
23+
}
24+
return res;
25+
}
26+
27+
// Decodes a single string to a list of strings.
28+
vector<string> decode(string s) {
29+
vector<string> res;
30+
31+
auto it = s.begin();
32+
while (it != s.end()) {
33+
int str_size = 0;
34+
string tmp = "";
35+
36+
while (*it != '.') {
37+
str_size = str_size * 10 + (*it - '0');
38+
it++;
39+
}
40+
41+
it++;
42+
43+
for (int i = 0; i < str_size; i++) {
44+
tmp.push_back(*it);
45+
it++;
46+
}
47+
48+
res.push_back(tmp);
49+
}
50+
51+
return res;
52+
}
53+
};
54+
55+
// Your Codec object will be instantiated and called as such:
56+
// Codec codec;
57+
// codec.decode(codec.encode(strs));

valid-anagram/flynn.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* For length of given strings N,
3+
*
4+
* Time complexity: O(N)
5+
* - iteration for given strings
6+
*
7+
* Space complexity: O(1)
8+
* - the size of the container `count` is constant
9+
*/
10+
11+
class Solution {
12+
public:
13+
bool isAnagram(string s, string t) {
14+
int count[26] = {0};
15+
16+
for (auto c : s) count[c - 'a']++;
17+
for (auto c : t) count[c - 'a']--;
18+
19+
for (int i = 0; i < 26; i++) if (count[i]) return false;
20+
return true;
21+
}
22+
};

0 commit comments

Comments
 (0)