-
-
Notifications
You must be signed in to change notification settings - Fork 195
[Flynn] WEEK 01 Solutions #302
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
Conversation
- contains duplicate - kth smallest element in a bst - number of 1 bits - palindromic substrings - top k frequent elements
|
반갑습니다, 리뷰어가 저로 변경되었군요 😄 오늘 저녁에 확인해볼게요 🙏 |
네 천천히 부탁드릴게요 ㅎㅎ |
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.
C++ 오랜만에 보는데 깔끔하게 작성해주셔서 큰 어려움 없이 이해할 수 있었습니다 👍
고생하셨어요!
* - iteration: O(N) | ||
* - unorderd_set find method: O(1) on average | ||
* - unorderd_set insert method: O(1) on average | ||
* |
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.
C++ 기억이 잘 안났는데 이렇게 적어주시니 편하네요 👍
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.
c++로 푸시는 분들이 많이 안 계신 것 같아서, python이나 ts를 사용해야 하나 고민도 되네요 ㅎㅎ 감사합니다
contains-duplicate/flynn.cpp
Outdated
class Solution { | ||
public: | ||
bool containsDuplicate(vector<int>& nums) { | ||
unordered_set<int> us; |
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.
us
가 unordered_set의 약자일까요? unordered_set
이라는 것은 타입으로 모두가 알고 있으니, 변수명에는 코드의 문맥에 어올리는 내용이 들어가도 좋을 것 같아요 💪
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.
네 다른 분들도 보시는 풀이 코드이니 BEMELON님 말씀대로 변수명을 수정해놓는게 좋겠습니다 감사합니다 :D
* Space complexity: O(H + K) at worst | ||
* - call stack + additional vector to save nums | ||
*/ |
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.
Call Stack까지 고려하셨군요 😮 👍
저도 잘 몰라서 여쭤보는데요, 보통 재귀를 돌게되면 Call Stack도 Space Complexity로 취급하는걸까요?
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.
네 제가 본 Big-O 분석에서는 대부분 Call Stack도 고려하고 있었어요 ㅎㅎㅎ
palindromic-substrings/flynn.cpp
Outdated
int start = i, end = j, flag = 0; | ||
|
||
while (start <= end) { | ||
if (s[start] != s[end]) { | ||
flag = 1; | ||
break; | ||
} |
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.
flag
대신에 bool palindrome
이였다면 읽기가 더 편하지 않았을까 싶어요 😍
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.
안녕하세요, 코치 Helena 입니다.
명확하고 간결히 잘 분석된 풀이 덕분에 더 수월히 리뷰 할 수 있었습니다!👍
첫 주부터 수고 많으셨습니다 :)
추가로, 코드 마지막 라인에 line break 추가해주시면 좋을 것 같습니다!
top-k-frequent-elements/flynn.cpp
Outdated
vector<int> topKFrequent(vector<int>& nums, int k) { | ||
vector<int> res; | ||
priority_queue<pair<int, int>> pq; | ||
unorderd_map<int, int> m; |
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.
typo가 있네요! unorderd_map -> unordered_map으로 수정되어야 할 것 같습니다!
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.
찾아주셔서 감사합니다 :D
/** | ||
* For the height H of the given BST, | ||
* | ||
* Time complexity: O(H) at worst |
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.
최악의 경우, BST의 높이가 가장 클 때 탐색에 걸리는 시간이 O(H)가 될 수 있겠군요! 추가적으로, 현재 코드에서는 중위 순회 시 트리의 모든 노드를 순회할 수 있어 시간 복잡도가 O(N)이 될 수 있다는 점도 고려해 보시면 좋을 것 같습니다 :)
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.
이야기해주신 내용을 보고 다시 생각해보니 K와 H의 대소관계에 따라 시간 및 공간 복잡도가 달라질 수 있을 것 같아 수정해보았습니다
감사합니다 :D
K > H일 때, 중위 순회 함수를 재귀적으로 호출하더라도 vector nums의 사이즈가 K가 된다면 함수를 바로 return하고 있어서 시간 복잡도는 O(K)라고 생각했는데 Helena님 의견은 어떤지도 궁금합니다
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.
@obzva 변경해주신 코멘트 확인하였는데, 훨씬 더 디테일해진 분석이군요!👍
말씀주신대로 K > H일 때 nums 크기가 K에 도달하면 더 이상 탐색을 하지 않고, 이것은 k번째 노드를 찾기 위해 트리의 일부 탐색 경우를 고려한 것이어서 시간 복잡도가 O(K)가 된다는 부분에 저도 매우 동의합니다.
제가, 추가적으로 드렸던 의견은 중위 순회가 트리의 모든 노드를 탐색할 수 있기 때문에 최악의 경우 시간 복잡도가 O(N)이 될 수 있다는 점에 대해서도 같이 생각해보시면 좋을 것 같아 말씀드려보았습니다 :)
이 코드의 시간 복잡도는 트리의 구조와 k 값에 따라 O(H)에서 O(N) 사이의 값을 가질 수 있다는 점에 대해서 생각해본 것만으로도 충분한 분석인 것 같습니다!
수고 많으셨습니다 :)
Explanation
Big-O 표기에 대한 설명은 저의 생각일 뿐이니, 오류를 찾으셨다면 편하게 말씀해주세요 감사합니다 :D
contains duplicate
For the size N of given input nums,
kth smallest element in a bst
For the height H of the given BST and K,
number of 1 bits
palindromic substrings
top k frequent elements