Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Flynn] week2 #346

Merged
merged 10 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<int>& preorder, vector<int>& inorder) {
unordered_map<int, int> inorder_index_map;
stack<TreeNode*> 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);
}
Comment on lines +28 to +45
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 풀이 너무 아름답습니다! 😍
혹시 다음 모임 때 다른 멤버들한테 설명 좀 해주실 수 있으실까요?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

네 ㅎㅎ 준비해보겠습니다
(+ line 여러개에 대해 코멘트를 남길 수도 있군요.. 배워갑니다!)


return root;
}
};
22 changes: 22 additions & 0 deletions counting-bits/flynn.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Time complexity: O(N)
*
* Space complexity: O(1)
*/

class Solution {
public:
vector<int> countBits(int n) {
vector<int> 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;
}
};
53 changes: 53 additions & 0 deletions decode-ways/flynn.cpp
Original file line number Diff line number Diff line change
@@ -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;
// }
// };
57 changes: 57 additions & 0 deletions encode-and-decode-strings/flynn.cpp
Original file line number Diff line number Diff line change
@@ -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<string>& 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<string> decode(string s) {
vector<string> res;

auto it = s.begin();
DaleSeo marked this conversation as resolved.
Show resolved Hide resolved
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));
22 changes: 22 additions & 0 deletions valid-anagram/flynn.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
};