-
-
Notifications
You must be signed in to change notification settings - Fork 260
[Flynn] week2 #346
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
Conversation
- rewrite in cpp
- line break
DaleSeo
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
무시하셔도 되는 아주 사소한 피드백 드립니다 🙂
DaleSeo
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 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); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이 풀이 너무 아름답습니다! 😍
혹시 다음 모임 때 다른 멤버들한테 설명 좀 해주실 수 있으실까요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
네 ㅎㅎ 준비해보겠습니다
(+ line 여러개에 대해 코멘트를 남길 수도 있군요.. 배워갑니다!)

Problems
Description
Valid Anagram
각 알파벳이 몇 번 등장하는지 기록할 수 있도록 길이 26의 int 배열
count를 만듭니다.문자열
s에 대한 반복문에서 각 알파벳의 등장 횟수를 기록합니다.문자열
t에 대한 반복문에서는 각 알파벳이 등장할 때마다count의 기록을 차감합니다.마지막 반복문에서
count에 기록된 값이0이 아닌 알파벳이 있다면false를 반환하고, 별 일 없이 반복문을 탈출한다면true를 반환합니다.Counting Bits
정답으로 반환해야하는 배열
res의 indexi에 따라 아래와 같은 규칙을 가진다는 걸 확인할 수 있습니다.이해를 돕기 위해 아래 표를 보면, 정수 4 ~ 7 의 이진 표현은 0 ~ 3 의 이진 표현 왼쪽에 1이 추가된 것이라는 사실을 알 수 있습니다.
Encode and Decode Strings
Chunked transfer encoding를 이용했습니다.
각 문자열마다 아래와 같은 방식으로 인코딩/디코딩했습니다.
주어진 모든 문자열의 길이의 합을 N, 가장 길이가 긴 문자열의 길이를 M이라고 했을 때 Big-O 분석은 다음과 같습니다.
tmp의 크기가 최대M입니다Construct Binary Tree from Preorder and Inorder Traversal
stack을 이용한iterative solution입니다.inorder traverse와preorder traverse의 성질을 이용하면,stack의 top이현재 node의parent인지 아닌지를 판단할 수 있습니다.parent의 left는inorder 배열에서 parent보다 왼편에 있는 요소 중 가장 오른편에 있는 요소여야 합니다.stack의 top이inorder배열에서현재 node보다 더 오른편에 있다면,stack의 top이 현재 node보다 오른편에 있는 요소 중 가장 왼편에 있는 요소임이 분명하기 때문에stack의 top은 현재 node의 부모라고 판단할 수 있습니다.왜냐하면 아래 코드에서 우리는 각 요소를
preorder배열의 순서대로 조회하고 있기 때문입니다.비슷하게,
parent의 right는inorder 배열에서 parent보다 오른편에 있는 요소 중 가장 왼편에 있는 요소여야 합니다.현재
stack의 top은stack에 쌓여있는 요소들 중inorder 배열 상 가장 왼편에 있는 요소입니다.따라서
parent에 조건에 부합한 요소가 나올 때 까지pop을 반복해주면현재 node의 부모를 찾을 수 있습니다.inorder배열의 각 요소에 index를 mapping하기 위해inorder_index_map을 만들었고 이건 O(N)의 시간 복잡도를 가집니다.preorder배열의 순서대로 요소를 조회하며Tree를 생성합니다.push혹은pop연산이 실행되는 경우가 있지만, 각 연산은 요소당 최대 1회씩만 실행되기 때문에 이 반복문 또한 O(N)의 시간 복잡도를 가집니다.inorder_index_map이 O(N)의 공간 복잡도를 가집니다tree_stack또한 O(N)의 공간 복잡도를 가집니다. Tree가 왼편으로 심하게 치우쳐 있을 경우도 존재하기 때문입니다.Decode Ways
BFS방식으로 풀이할 경우,11111...11111111과 같은 test case에 대해 O(2^N)의 시간복잡도를 가지므로 제한 시간 안에 문제를 풀 수 없습니다.memo라는 배열을 정의합니다.만약
s[i - 1]이0이 아니라면,s.substr(0, i - 1)를 decode할 방법들을 그대로 사용할 수 있으므로memo[i]는memo[i - 1]입니다.또한 만약
s.substr(i - 2, 2)이 10 이상 26이하의 정수라면s.substr(0, i - 2)를 decode하는 방법을 추가할 수 있으므로memo[i] += memo[i - 2]입니다.