Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
24 changes: 24 additions & 0 deletions contains-duplicate/flynn.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* for given size of input nums N,
*
* Time complexity: O(N)
* - iteration: O(N)
* - unorderd_set find method: O(1) on average
* - unorderd_set insert method: O(1) on average
*
Comment on lines +5 to +8
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를 사용해야 하나 고민도 되네요 ㅎㅎ 감사합니다

* Space complexity: O(N)
*/

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


auto it = nums.begin();
while (it != nums.end()) {
if (us.find(*it) == us.end()) us.insert(*(it++));
else return true;
}
return false;
}
};
37 changes: 37 additions & 0 deletions kth-smallest-element-in-a-bst/flynn.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* 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) 사이의 값을 가질 수 있다는 점에 대해서 생각해본 것만으로도 충분한 분석인 것 같습니다!
수고 많으셨습니다 :)

*
* 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도 고려하고 있었어요 ㅎㅎㅎ


/**
* 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:
void inorder(TreeNode* node, vector<int>& v, int max_size) {
if (!node || v.size() == max_size) return;

if (node->left) inorder(node->left, v, max_size);
v.push_back(node->val);
if (node->right) inorder(node->right, v, max_size);
}

int kthSmallest(TreeNode* root, int k) {
vector<int> nums;
inorder(root, nums, k);

return nums[k - 1];
}
};
21 changes: 21 additions & 0 deletions number-of-1-bits/flynn.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* For given integer N,
*
* Time complexity: O(logN)
*
* Space complexity: O(1)
*/

class Solution {
public:
int hammingWeight(int n) {
int res = 0;

while (n) {
if (n & 1) res++;
n >>= 1;
}

return res;
}
};
33 changes: 33 additions & 0 deletions palindromic-substrings/flynn.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* For the length N of given string s,
*
* Time complexity: O(N^3)
*
* Space complexity: O(1)
*/

class Solution {
public:
int countSubstrings(string s) {
int res = 0;

for (int i = 0; i < s.size(); i++) {
for (int j = i; j < s.size(); j++) {
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.

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

start++;
end--;
}

if (!flag) res++;
}
}

return res;
}
};
46 changes: 46 additions & 0 deletions top-k-frequent-elements/flynn.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* For given size of nums N and K,
*
* Time complexity: O(N + MlogM + KlogM) <= O(NlogN)
* - the first iteration: O(N)
* - N times of
* - unordered_map find: O(1) on average
* - unordered_map insert: O(1) on average
* - the second iteration: O(MlogM) such that M is the number of unique numbers
* - M times of
* - priority_queue push: O(logM)
* - the last iteration of making result: O(KlogM)
* - K times of
* - priority_queue pop: O(logM)
*
* Space complexity: O(N) at worst
*/

class Solution {
public:
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


auto nums_it = nums.begin();
while (nums_it != nums.end()) {
if (m.find(*nums_it) == m.end()) m.insert({*nums_it, 1});
else m[*nums_it]++;
nums_it++;
}

auto m_it = m.begin();
while (m_it != m.end()) {
pq.push({(*m_it).second, (*m_it).first});
m_it++;
}

while (k--) {
res.push_back(pq.top().second);
pq.pop();
}

return res;
}
};