Skip to content

Commit 43c7341

Browse files
authored
Merge branch 'DaleStudy:main' into main
2 parents 81fdc17 + 1f2b1c2 commit 43c7341

File tree

5 files changed

+166
-0
lines changed

5 files changed

+166
-0
lines changed

contains-duplicate/flynn.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* for given size of input nums N,
3+
*
4+
* Time complexity: O(N)
5+
* - iteration: O(N)
6+
* - unorderd_set find method: O(1) on average
7+
* - unorderd_set insert method: O(1) on average
8+
*
9+
* Space complexity: O(N)
10+
*/
11+
12+
class Solution {
13+
public:
14+
bool containsDuplicate(vector<int>& nums) {
15+
unordered_set<int> unique_numbers;
16+
17+
auto it = nums.begin();
18+
while (it != nums.end()) {
19+
if (unique_numbers.find(*it) == unique_numbers.end()) unique_numbers.insert(*(it++));
20+
else return true;
21+
}
22+
return false;
23+
}
24+
};
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* For the height H of the given BST,
3+
*
4+
* Time complexity: O(max(H, K))
5+
* - if H > K, O(H) at worst
6+
* - else, O(K)
7+
*
8+
* Space complexity: O(H > K ? H + K : K)
9+
* - additional vector to save nums O(K)
10+
* - if H > K, call stack O(H)
11+
* - else, O(K)
12+
*/
13+
14+
/**
15+
* Definition for a binary tree node.
16+
* struct TreeNode {
17+
* int val;
18+
* TreeNode *left;
19+
* TreeNode *right;
20+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
21+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
22+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
23+
* };
24+
*/
25+
class Solution {
26+
public:
27+
void inorder(TreeNode* node, vector<int>& v, int max_size) {
28+
if (!node || v.size() == max_size) return;
29+
30+
if (node->left) inorder(node->left, v, max_size);
31+
v.push_back(node->val);
32+
if (node->right) inorder(node->right, v, max_size);
33+
}
34+
35+
int kthSmallest(TreeNode* root, int k) {
36+
vector<int> nums;
37+
inorder(root, nums, k);
38+
39+
return nums[k - 1];
40+
}
41+
};

number-of-1-bits/flynn.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* For given integer N,
3+
*
4+
* Time complexity: O(logN)
5+
*
6+
* Space complexity: O(1)
7+
*/
8+
9+
class Solution {
10+
public:
11+
int hammingWeight(int n) {
12+
int res = 0;
13+
14+
while (n) {
15+
if (n & 1) res++;
16+
n >>= 1;
17+
}
18+
19+
return res;
20+
}
21+
};

palindromic-substrings/flynn.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* For the length N of given string s,
3+
*
4+
* Time complexity: O(N^3)
5+
*
6+
* Space complexity: O(1)
7+
*/
8+
9+
class Solution {
10+
public:
11+
int countSubstrings(string s) {
12+
int res = 0;
13+
14+
for (int i = 0; i < s.size(); i++) {
15+
for (int j = i; j < s.size(); j++) {
16+
int start = i, end = j;
17+
bool is_palindrome = true;
18+
19+
while (start <= end) {
20+
if (s[start] != s[end]) {
21+
is_palindrome = false;
22+
break;
23+
}
24+
start++;
25+
end--;
26+
}
27+
28+
if (is_palindrome) res++;
29+
}
30+
}
31+
32+
return res;
33+
}
34+
};

top-k-frequent-elements/flynn.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* For given size of nums N and K,
3+
*
4+
* Time complexity: O(N + MlogM + KlogM) <= O(NlogN)
5+
* - the first iteration: O(N)
6+
* - N times of
7+
* - unordered_map find: O(1) on average
8+
* - unordered_map insert: O(1) on average
9+
* - the second iteration: O(MlogM) such that M is the number of unique numbers
10+
* - M times of
11+
* - priority_queue push: O(logM)
12+
* - the last iteration of making result: O(KlogM)
13+
* - K times of
14+
* - priority_queue pop: O(logM)
15+
*
16+
* Space complexity: O(N) at worst
17+
*/
18+
19+
class Solution {
20+
public:
21+
vector<int> topKFrequent(vector<int>& nums, int k) {
22+
vector<int> res;
23+
priority_queue<pair<int, int>> pq;
24+
unordered_map<int, int> m;
25+
26+
auto nums_it = nums.begin();
27+
while (nums_it != nums.end()) {
28+
if (m.find(*nums_it) == m.end()) m.insert({*nums_it, 1});
29+
else m[*nums_it]++;
30+
nums_it++;
31+
}
32+
33+
auto m_it = m.begin();
34+
while (m_it != m.end()) {
35+
pq.push({(*m_it).second, (*m_it).first});
36+
m_it++;
37+
}
38+
39+
while (k--) {
40+
res.push_back(pq.top().second);
41+
pq.pop();
42+
}
43+
44+
return res;
45+
}
46+
};

0 commit comments

Comments
 (0)