Skip to content

Commit 60cfa5a

Browse files
authored
Merge pull request #304 from YeonguChoe/main
[์˜์šฐ] Week 1 ํ’€์ด ์ œ์ถœ
2 parents ca6a63c + a457dae commit 60cfa5a

File tree

5 files changed

+130
-0
lines changed

5 files changed

+130
-0
lines changed

โ€Žcontains-duplicate/yeongu.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Time complexity: o(n log n) ์™œ๋ƒํ•˜๋ฉด, for ๋ฌธ์ด n์ด๊ณ  find ์—ฐ์‚ฐ์ด log n์ด๊ธฐ ๋•Œ๋ฌธ.
2+
// Space complexity: O(n) ์™œ๋ƒํ•˜๋ฉด, ๋‹ค ๋‹ค๋ฅธ๊ฒฝ์šฐ n
3+
4+
class Solution {
5+
public:
6+
bool containsDuplicate(vector<int>& nums) {
7+
set<int> unique;
8+
for (int i : nums) {
9+
if (unique.find(i) != unique.end()) {
10+
return true;
11+
}
12+
unique.insert(i);
13+
}
14+
return false;
15+
}
16+
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
TC: O(n log n) ์™œ๋ƒํ•˜๋ฉด, heap์— push์—ฐ์‚ฐ์ด log n์ด๊ณ  ๊ฐ ๋…ธ๋“œ๋งˆ๋‹ค ์‹คํ–‰ํ•˜๋ฏ€๋กœ.
2+
SC: O(n) queue๊ฐ€ n๊นŒ์ง€ ์ปค์งˆ์ˆ˜ ์žˆ์Œ.
3+
4+
int kthSmallest(TreeNode* root, int k) {
5+
priority_queue<int, vector<int>, greater<int>> minHeap;
6+
7+
// bfs
8+
queue<TreeNode*> q;
9+
if (root != nullptr) {
10+
q.push(root);
11+
};
12+
while (!q.empty()) {
13+
minHeap.push(q.front()->val);
14+
if (q.front()->left != nullptr) {
15+
q.push(q.front()->left);
16+
}
17+
if (q.front()->right != nullptr) {
18+
q.push(q.front()->right);
19+
}
20+
q.pop();
21+
}
22+
23+
for (int i = 0; i < k - 1; i++) {
24+
minHeap.pop();
25+
}
26+
return minHeap.top();
27+
}

โ€Žnumber-of-1-bits/yeongu.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Time complexity: O(n) ์™œ๋ƒํ•˜๋ฉด, while๋ฌธ
2+
// Space complexity: O(1)
3+
4+
class Solution {
5+
public:
6+
int hammingWeight(int n) {
7+
int cnt = 0;
8+
while(n>0){
9+
int val = n%2;
10+
cnt+=val;
11+
n/=2;
12+
}
13+
return cnt;
14+
}
15+
};

โ€Žpalindromic-substrings/yeongu.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// TC: O(n^2) For๋ฌธ ์•ˆ์—์„œ while๋ฌธ ์‚ฌ์šฉํ•˜๋Š” ํ•จ์ˆ˜ ํ˜ธ์ถœ
2+
// SC: O(1)
3+
4+
class Solution {
5+
public:
6+
int find_palindrome1(string& original_text, int index) {
7+
int cnt = 0;
8+
int left_ptr = index, right_ptr = index;
9+
10+
while (left_ptr >= 0 and right_ptr < original_text.size() and
11+
original_text[left_ptr] == original_text[right_ptr]) {
12+
cnt += 1;
13+
left_ptr -= 1;
14+
right_ptr += 1;
15+
}
16+
return cnt;
17+
}
18+
19+
int find_palindrome2(string& original_text, int index) {
20+
int cnt = 0;
21+
int left_ptr = index, right_ptr = index + 1;
22+
23+
while (left_ptr >= 0 and right_ptr < original_text.size() and
24+
original_text[left_ptr] == original_text[right_ptr]) {
25+
cnt++;
26+
left_ptr--;
27+
right_ptr++;
28+
}
29+
return cnt;
30+
}
31+
32+
int countSubstrings(string& s) {
33+
int output = 0;
34+
for (int i = 0; i < s.size(); i++) {
35+
output += find_palindrome1(s, i);
36+
}
37+
for (int i = 0; i < s.size() - 1; i++) {
38+
output += find_palindrome2(s, i);
39+
}
40+
return output;
41+
}
42+
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// TC: O(n logn)
2+
// SC: O(n)
3+
4+
vector<int> topKFrequent(vector<int>& nums, int k) {
5+
unordered_map<int, int> frequency;
6+
for (int n : nums) {
7+
frequency[n] += 1;
8+
}
9+
auto comparator = [&frequency](int n1, int n2) {
10+
return frequency[n1] > frequency[n2];
11+
};
12+
13+
priority_queue<int, vector<int>, decltype(comparator)> min_heap(
14+
comparator);
15+
16+
for (auto& entry : frequency) {
17+
min_heap.push(entry.first);
18+
if (min_heap.size() > k) {
19+
min_heap.pop();
20+
}
21+
}
22+
23+
vector<int> output;
24+
25+
for (int i = 0; i < k; i++) {
26+
output.push_back(min_heap.top());
27+
min_heap.pop();
28+
}
29+
return output;
30+
}

0 commit comments

Comments
ย (0)