Skip to content

Commit e9833eb

Browse files
committed
week1 solutions
- contains duplicate - kth smallest element in a bst - number of 1 bits - palindromic substrings - top k frequent elements
1 parent ee1ad8e commit e9833eb

File tree

5 files changed

+161
-0
lines changed

5 files changed

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

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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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, flag = 0;
17+
18+
while (start <= end) {
19+
if (s[start] != s[end]) {
20+
flag = 1;
21+
break;
22+
}
23+
start++;
24+
end--;
25+
}
26+
27+
if (!flag) res++;
28+
}
29+
}
30+
31+
return res;
32+
}
33+
};

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+
unorderd_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)