Skip to content

Commit 635ceb9

Browse files
authored
[crispy] Week 1 Solution explanation (#313)
* solve: contains-duplicate * renmae to fit with contribution guide * feat: add complexity * solve: number of 1 bit * docs: add tc and sc * solve: top k frequent elements * solve: kth smallest in bst * solve: palindromic substring * feat: add dp version of palindromic substring
1 parent 0cabff3 commit 635ceb9

File tree

5 files changed

+194
-0
lines changed

5 files changed

+194
-0
lines changed

contains-duplicate/heozeop.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Time Complexity : O(n)
2+
// Spatial Complexity : O(n)
3+
4+
#include <set>
5+
6+
class Solution {
7+
public:
8+
bool containsDuplicate(vector<int>& nums) {
9+
set<int> count;
10+
11+
for(int num : nums) {
12+
if (count.find(num) != count.end()) {
13+
return true;
14+
}
15+
count.insert(num);
16+
}
17+
18+
return false;
19+
}
20+
};
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Time Complexity: O(nlogn)
2+
// Spatial Complexity O(n)
3+
4+
#include <vector>
5+
#include <queue>
6+
/**
7+
* Definition for a binary tree node.
8+
* struct TreeNode {
9+
* int val;
10+
* TreeNode *left;
11+
* TreeNode *right;
12+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
13+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
14+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
15+
* };
16+
*/
17+
class Solution {
18+
public:
19+
int kthSmallest(TreeNode* root, int k) {
20+
queue<TreeNode*> q;
21+
q.push(root);
22+
23+
vector<int> numbers;
24+
TreeNode *curNode, *nextNode;
25+
while(!q.empty()) {
26+
curNode = q.front();
27+
q.pop();
28+
29+
numbers.push_back(curNode->val);
30+
31+
nextNode = curNode->left;
32+
if(nextNode != nullptr) {
33+
q.push(nextNode);
34+
}
35+
36+
nextNode = curNode->right;
37+
if(nextNode != nullptr) {
38+
q.push(nextNode);
39+
}
40+
}
41+
42+
sort(numbers.begin(), numbers.end());
43+
return numbers[k - 1];
44+
}
45+
};
46+

number-of-1-bits/heozeop.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Time Complexity: O(log(n))
2+
// Spatial Complexity: O(1)
3+
4+
class Solution {
5+
public:
6+
int hammingWeight(int n) {
7+
int numberOf1Bit = 0;
8+
while(n) {
9+
if(n % 2) {
10+
numberOf1Bit += 1;
11+
}
12+
13+
n = n >> 1;
14+
}
15+
16+
return numberOf1Bit;
17+
}
18+
};
19+

palindromic-substrings/heozeop.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Time Complexity: O(n^3)
2+
// Spatial Complexity: O(1)
3+
4+
// #include <set>
5+
// #include <string>
6+
7+
class Solution {
8+
private:
9+
bool isPalindrom(string s) {
10+
int sLength = s.length();
11+
12+
for(int i = 0; i < sLength / 2; ++i) {
13+
if(s[i] != s[sLength - 1 - i]) {
14+
return false;
15+
}
16+
}
17+
18+
return true;
19+
}
20+
21+
public:
22+
int countSubstrings(string s) {
23+
int answer = 0;
24+
25+
int sLength = s.length();
26+
string temp;
27+
for(int i = 1; i <= sLength; ++i) {
28+
for(int j = 0; j + i <= sLength; ++j) {
29+
temp = s.substr(j, i);
30+
31+
if(this->isPalindrom(temp)) {
32+
++answer;
33+
}
34+
}
35+
}
36+
37+
return answer;
38+
}
39+
};
40+
41+
// DP version
42+
// O(n^2)
43+
// #include <set>
44+
// #include <vector>
45+
46+
class Solution2 {
47+
public:
48+
int countSubstrings(string str) {
49+
int answer = 0;
50+
51+
int n = str.length();
52+
vector<vector<bool>> dp(n + 1, vector<bool>(n + 1, false));
53+
54+
for(int e = 0; e < n; ++e) {
55+
for(int s = 0; s <= e; ++s) {
56+
if (str[s] != str[e]) {
57+
continue;
58+
}
59+
60+
if (s != e && s + 1 != e && !dp[s + 1][e - 1]) {
61+
continue;
62+
}
63+
64+
++answer;
65+
dp[s][e] = true;
66+
}
67+
}
68+
69+
return answer;
70+
}
71+
};

top-k-frequent-elements/heozeop.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Time complexity O(nlogn)
2+
// Spatal Complexity O(n)
3+
4+
#include <map>
5+
#include <vector>
6+
7+
using namespace std;
8+
9+
10+
bool cmp(const pair<int,int>& a, const pair<int,int>& b) {
11+
return a.second > b.second;
12+
}
13+
14+
class Solution {
15+
public:
16+
vector<int> topKFrequent(vector<int>& nums, int k) {
17+
map<int, int> countMap;
18+
19+
for(int num : nums) {
20+
if(countMap.find(num) == countMap.end()) {
21+
countMap.insert({num, 0});
22+
}
23+
24+
++countMap[num];
25+
}
26+
27+
vector<pair<int,int>> vec(countMap.begin(), countMap.end());
28+
sort(vec.begin(), vec.end(), cmp);
29+
30+
vector<int> answer;
31+
for(int i = 0; i < k; ++i) {
32+
answer.push_back(vec[i].first);
33+
}
34+
35+
return answer;
36+
}
37+
};
38+

0 commit comments

Comments
 (0)