-
-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
* | ||
* Space complexity: O(N) | ||
*/ | ||
|
||
class Solution { | ||
public: | ||
bool containsDuplicate(vector<int>& nums) { | ||
unordered_set<int> unique_numbers; | ||
|
||
auto it = nums.begin(); | ||
while (it != nums.end()) { | ||
if (unique_numbers.find(*it) == unique_numbers.end()) unique_numbers.insert(*(it++)); | ||
else return true; | ||
} | ||
return false; | ||
} | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* For the height H of the given BST, | ||
* | ||
* Time complexity: O(max(H, K)) | ||
* - if H > K, O(H) at worst | ||
* - else, O(K) | ||
* | ||
* Space complexity: O(H > K ? H + K : K) | ||
* - additional vector to save nums O(K) | ||
* - if H > K, call stack O(H) | ||
* - else, O(K) | ||
*/ | ||
|
||
/** | ||
* 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]; | ||
} | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* 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; | ||
bool is_palindrome = true; | ||
|
||
while (start <= end) { | ||
if (s[start] != s[end]) { | ||
is_palindrome = false; | ||
break; | ||
} | ||
start++; | ||
end--; | ||
} | ||
|
||
if (is_palindrome) res++; | ||
} | ||
} | ||
|
||
return res; | ||
} | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
unordered_map<int, int> m; | ||
|
||
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; | ||
} | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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를 사용해야 하나 고민도 되네요 ㅎㅎ 감사합니다