-
-
Notifications
You must be signed in to change notification settings - Fork 203
[영우] Week 1 풀이 제출 #304
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
[영우] Week 1 풀이 제출 #304
Changes from all commits
a74616d
ec54f9b
6c4376e
d4c79cc
68bc62b
9401aaa
a457dae
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Time complexity: o(n log n) 왜냐하면, for 문이 n이고 find 연산이 log n이기 때문. | ||
// Space complexity: O(n) 왜냐하면, 다 다른경우 n | ||
|
||
class Solution { | ||
public: | ||
bool containsDuplicate(vector<int>& nums) { | ||
set<int> unique; | ||
for (int i : nums) { | ||
if (unique.find(i) != unique.end()) { | ||
return true; | ||
} | ||
unique.insert(i); | ||
} | ||
return false; | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
TC: O(n log n) 왜냐하면, heap에 push연산이 log n이고 각 노드마다 실행하므로. | ||
SC: O(n) queue가 n까지 커질수 있음. | ||
|
||
int kthSmallest(TreeNode* root, int k) { | ||
priority_queue<int, vector<int>, greater<int>> minHeap; | ||
|
||
// bfs | ||
queue<TreeNode*> q; | ||
if (root != nullptr) { | ||
q.push(root); | ||
}; | ||
while (!q.empty()) { | ||
minHeap.push(q.front()->val); | ||
if (q.front()->left != nullptr) { | ||
q.push(q.front()->left); | ||
} | ||
if (q.front()->right != nullptr) { | ||
q.push(q.front()->right); | ||
} | ||
q.pop(); | ||
} | ||
|
||
for (int i = 0; i < k - 1; i++) { | ||
minHeap.pop(); | ||
} | ||
return minHeap.top(); | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 해당 문제도 시 공간 복잡도 넣어주시면 좋을 것 같습니다! |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Time complexity: O(n) 왜냐하면, while문 | ||
// Space complexity: O(1) | ||
|
||
class Solution { | ||
public: | ||
int hammingWeight(int n) { | ||
int cnt = 0; | ||
while(n>0){ | ||
int val = n%2; | ||
cnt+=val; | ||
n/=2; | ||
} | ||
return cnt; | ||
} | ||
}; |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For 문안에 while 문을 넣는다고 반드시 O(n^2)이 될까요?ㅎㅎ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 제가 보기에도 O(N^2)같은데, Sam님 의견이 궁금합니다 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// TC: O(n^2) For문 안에서 while문 사용하는 함수 호출 | ||
// SC: O(1) | ||
|
||
class Solution { | ||
public: | ||
int find_palindrome1(string& original_text, int index) { | ||
int cnt = 0; | ||
int left_ptr = index, right_ptr = index; | ||
|
||
while (left_ptr >= 0 and right_ptr < original_text.size() and | ||
original_text[left_ptr] == original_text[right_ptr]) { | ||
cnt += 1; | ||
left_ptr -= 1; | ||
right_ptr += 1; | ||
} | ||
return cnt; | ||
} | ||
|
||
int find_palindrome2(string& original_text, int index) { | ||
int cnt = 0; | ||
int left_ptr = index, right_ptr = index + 1; | ||
|
||
while (left_ptr >= 0 and right_ptr < original_text.size() and | ||
original_text[left_ptr] == original_text[right_ptr]) { | ||
cnt++; | ||
left_ptr--; | ||
right_ptr++; | ||
} | ||
return cnt; | ||
} | ||
|
||
int countSubstrings(string& s) { | ||
int output = 0; | ||
for (int i = 0; i < s.size(); i++) { | ||
output += find_palindrome1(s, i); | ||
} | ||
for (int i = 0; i < s.size() - 1; i++) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 안녕하세요 영우님, 풀이 잘 보았습니다 :D 제가 보기엔 두번째 for문이 불필요해보입니다 |
||
output += find_palindrome2(s, i); | ||
} | ||
return output; | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// TC: O(n logn) | ||
// SC: O(n) | ||
|
||
vector<int> topKFrequent(vector<int>& nums, int k) { | ||
unordered_map<int, int> frequency; | ||
for (int n : nums) { | ||
frequency[n] += 1; | ||
} | ||
auto comparator = [&frequency](int n1, int n2) { | ||
return frequency[n1] > frequency[n2]; | ||
}; | ||
|
||
priority_queue<int, vector<int>, decltype(comparator)> min_heap( | ||
comparator); | ||
Comment on lines
+13
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. STL을 쓰는 것도 좋지만 한 번 구현해서 써보시면 STL 사용금지인 코딩테스트에서 도움되실 것 같습니다 |
||
|
||
for (auto& entry : frequency) { | ||
min_heap.push(entry.first); | ||
if (min_heap.size() > k) { | ||
min_heap.pop(); | ||
} | ||
} | ||
|
||
vector<int> output; | ||
|
||
for (int i = 0; i < k; i++) { | ||
output.push_back(min_heap.top()); | ||
min_heap.pop(); | ||
} | ||
return output; | ||
} |
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.
요것도 시 공간 복잡도 추가부탁드립니다!