Skip to content

[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

Merged
merged 5 commits into from
Aug 15, 2024
Merged

[Flynn] WEEK 01 Solutions #302

merged 5 commits into from
Aug 15, 2024

Conversation

obzva
Copy link
Contributor

@obzva obzva commented Aug 11, 2024

Explanation

Big-O 표기에 대한 설명은 저의 생각일 뿐이니, 오류를 찾으셨다면 편하게 말씀해주세요 감사합니다 :D

contains duplicate

For the size N of given input nums,

  • Time complexity: O(N)
    • ( unordered_set의 find, insert method가 평균적으로 O(1)의 시간 복잡도를 가지지만, 최악의 경우O(N)을 가집니다. )
    • ( hash collision이 일어나는 경우에 해당할 때 O(N)의 시간 복잡도가 소요될 것이라고 추측하는데, 해당 상황이 정확히 어떻게 일어나는지에 대해서는 좀 더 찾아볼 예정입니다. )
  • Space complexity: O(N) at worst

kth smallest element in a bst

For the height H of the given BST and K,

  • Time complexity: O(H) at worst
    • 최악의 경우 root의 좌측에만 자식 노드가 존재할 수 있으며, 이 때 가장 작은 TreeNode를 찾는 탐색은 O(H)의 시간 복잡도를 가집니다.
  • Space complexity: O(H + K) at worst
    • 최악의 경우 함수 콜 스택이 O(H)만큼의 공간 복잡도를 가집니다.
    • 배열 nums는 O(K)만큼의 공간 복잡도를 가집니다.

number of 1 bits

  • Time complexity: O(logN)
  • Space complexity: O(1)

palindromic substrings

  • Time complexity: O(N^3)
  • Space complexity: O(1)

top k frequent elements

  • Time complexity: O(NlogN) at worst
    • 첫번째 반복문에서 O(N)
    • 두번째 반복문에서 O(MlogM) (M은 고유한 숫자의 개수, M <= N)
    • 세번째 반복문에서 O(KlogM)
      • priority_queue의 pop은 O(logM)의 시간 복잡도를 가집니다.
    • 따라서 전체적인 시간 복잡도는 O(N + MlogM + KlogM)라고 볼 수 있으며 M이 N에 근접할 수록 O(NlogN)에 가까워집니다.
  • Space complexity: O(N) at worst

- contains duplicate
- kth smallest element in a bst
- number of 1 bits
- palindromic substrings
- top k frequent elements
@obzva
Copy link
Contributor Author

obzva commented Aug 11, 2024

바로 아래 HaJoonYu님께 reviewer 설정해드리고 싶은데, 멘션도 안 되고 reviewer 목록에도 뜨질 않네요 흠
순서가 또 바뀌었네요

@BEMELON BEMELON self-requested a review August 12, 2024 01:19
@BEMELON
Copy link
Contributor

BEMELON commented Aug 12, 2024

반갑습니다, 리뷰어가 저로 변경되었군요 😄 오늘 저녁에 확인해볼게요 🙏

@obzva
Copy link
Contributor Author

obzva commented Aug 12, 2024

네 천천히 부탁드릴게요 ㅎㅎ

Copy link
Contributor

@BEMELON BEMELON left a comment

Choose a reason for hiding this comment

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

C++ 오랜만에 보는데 깔끔하게 작성해주셔서 큰 어려움 없이 이해할 수 있었습니다 👍
고생하셨어요!

Comment on lines +5 to +8
* - iteration: O(N)
* - unorderd_set find method: O(1) on average
* - unorderd_set insert method: O(1) on average
*
Copy link
Contributor

Choose a reason for hiding this comment

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

C++ 기억이 잘 안났는데 이렇게 적어주시니 편하네요 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

c++로 푸시는 분들이 많이 안 계신 것 같아서, python이나 ts를 사용해야 하나 고민도 되네요 ㅎㅎ 감사합니다

class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
unordered_set<int> us;
Copy link
Contributor

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 이라는 것은 타입으로 모두가 알고 있으니, 변수명에는 코드의 문맥에 어올리는 내용이 들어가도 좋을 것 같아요 💪

Copy link
Contributor Author

Choose a reason for hiding this comment

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

네 다른 분들도 보시는 풀이 코드이니 BEMELON님 말씀대로 변수명을 수정해놓는게 좋겠습니다 감사합니다 :D

Comment on lines 6 to 8
* Space complexity: O(H + K) at worst
* - call stack + additional vector to save nums
*/
Copy link
Contributor

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로 취급하는걸까요?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

네 제가 본 Big-O 분석에서는 대부분 Call Stack도 고려하고 있었어요 ㅎㅎㅎ

Comment on lines 16 to 22
int start = i, end = j, flag = 0;

while (start <= end) {
if (s[start] != s[end]) {
flag = 1;
break;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

flag 대신에 bool palindrome 이였다면 읽기가 더 편하지 않았을까 싶어요 😍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

이 부분도 변수명을 수정해볼게요 :)

Copy link
Contributor

@yolophg yolophg left a comment

Choose a reason for hiding this comment

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

안녕하세요, 코치 Helena 입니다.
명확하고 간결히 잘 분석된 풀이 덕분에 더 수월히 리뷰 할 수 있었습니다!👍
첫 주부터 수고 많으셨습니다 :)
추가로, 코드 마지막 라인에 line break 추가해주시면 좋을 것 같습니다!

vector<int> topKFrequent(vector<int>& nums, int k) {
vector<int> res;
priority_queue<pair<int, int>> pq;
unorderd_map<int, int> m;
Copy link
Contributor

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으로 수정되어야 할 것 같습니다!

Copy link
Contributor Author

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
Copy link
Contributor

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)이 될 수 있다는 점도 고려해 보시면 좋을 것 같습니다 :)

Copy link
Contributor Author

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님 의견은 어떤지도 궁금합니다

Copy link
Contributor

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) 사이의 값을 가질 수 있다는 점에 대해서 생각해본 것만으로도 충분한 분석인 것 같습니다!
수고 많으셨습니다 :)

@obzva obzva requested a review from yolophg August 12, 2024 14:05
@obzva obzva merged commit 1f2b1c2 into DaleStudy:main Aug 15, 2024
@obzva obzva added the c++ label Aug 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
No open projects
Status: Completed
Development

Successfully merging this pull request may close these issues.

3 participants