Skip to content

Commit bc86994

Browse files
authored
Merge pull request #1598 from PDKhan/main
2 parents 4bdfd65 + daa3053 commit bc86994

File tree

5 files changed

+166
-0
lines changed

5 files changed

+166
-0
lines changed

non-overlapping-intervals/PDKhan.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
int eraseOverlapIntervals(vector<vector<int>>& intervals) {
4+
sort(intervals.begin(), intervals.end(), [](const vector<int>& a, const vector<int>& b){ return a[1] < b[1];});
5+
6+
int result = 0;
7+
int end = intervals[0][1];
8+
9+
for(int i = 1; i < intervals.size(); i++){
10+
if(intervals[i][0] < end)
11+
result++;
12+
else
13+
end = intervals[i][1];
14+
}
15+
16+
return result;
17+
}
18+
};
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution {
2+
public:
3+
/**
4+
* @param n: the number of vertices
5+
* @param edges: the edges of undirected graph
6+
* @return: the number of connected components
7+
*/
8+
void dfs(int curr, vector<vector<int>>& graph, vector<bool>& visited){
9+
visited[curr] = true;
10+
11+
for(int i = 0; i < graph[curr].size(); i++){
12+
if(visited[graph[curr][i]] == false)
13+
dfs(graph[curr][i], graph, visited);
14+
}
15+
}
16+
17+
int countComponents(int n, vector<vector<int>> &edges) {
18+
// write your code here
19+
vector<bool> visited(n, 0);
20+
vector<vector<int>> graph (n);
21+
int cnt = 0;
22+
23+
for(int i = 0; i < edges.size(); i++){
24+
graph[edges[i][0]].push_back(edges[i][1]);
25+
graph[edges[i][1]].push_back(edges[i][0]);
26+
}
27+
28+
for(int i = 0; i < n; i++){
29+
if(visited[i] == false){
30+
dfs(i, graph, visited);
31+
cnt++;
32+
}
33+
}
34+
35+
return cnt;
36+
}
37+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public:
3+
ListNode* removeNthFromEnd(ListNode* head, int n) {
4+
ListNode* prev = NULL;
5+
ListNode* curr = head;
6+
ListNode* next = head;
7+
8+
for(int i = 0; next && i < n; i++)
9+
next = next->next;
10+
11+
while(next){
12+
if(prev == nullptr)
13+
prev = head;
14+
else
15+
prev = prev->next;
16+
curr = curr->next;
17+
next = next->next;
18+
}
19+
20+
if(prev == nullptr)
21+
head = curr->next;
22+
else
23+
prev->next = curr->next;
24+
25+
delete(curr);
26+
27+
return head;
28+
}
29+
};

same-tree/PDKhan.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
bool isSameTree(TreeNode* p, TreeNode* q) {
4+
if(!p && !q)
5+
return true;
6+
else if(!p || !q)
7+
return false;
8+
else if(p->val != q->val)
9+
return false;
10+
11+
return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
12+
}
13+
};
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
class Codec {
2+
public:
3+
// Encodes a tree to a single string.
4+
string serialize(TreeNode* root) {
5+
string result;
6+
7+
if(root == nullptr)
8+
return result;
9+
10+
queue<TreeNode*> q;
11+
12+
q.push(root);
13+
14+
while(!q.empty()){
15+
TreeNode* curr = q.front();
16+
17+
q.pop();
18+
19+
if(curr){
20+
result += to_string(curr->val) + ",";
21+
q.push(curr->left);
22+
q.push(curr->right);
23+
}else{
24+
result += "NULL,";
25+
}
26+
}
27+
28+
return result;
29+
}
30+
// Decodes your encoded data to tree.
31+
TreeNode* deserialize(string data) {
32+
if(!data.size())
33+
return nullptr;
34+
35+
queue<TreeNode*> q;
36+
vector<string> values;
37+
size_t prev = 0;
38+
size_t pos = data.find(",", prev);
39+
40+
while(pos != string::npos){
41+
values.push_back(data.substr(prev, pos - prev));
42+
prev = pos + 1;
43+
pos = data.find(",", prev);
44+
}
45+
46+
int idx = 0;
47+
TreeNode* root = new TreeNode(stoi(values[idx++]));
48+
q.push(root);
49+
50+
while(!q.empty()){
51+
int left = idx++;
52+
int right = idx++;
53+
TreeNode* curr = q.front();
54+
q.pop();
55+
56+
if(left < values.size() && values[left] != "NULL"){
57+
curr->left = new TreeNode(stoi(values[left]));
58+
q.push(curr->left);
59+
}
60+
61+
if(right < values.size() && values[right] != "NULL"){
62+
curr->right = new TreeNode(stoi(values[right]));
63+
q.push(curr->right);
64+
}
65+
}
66+
67+
return root;
68+
}
69+
};

0 commit comments

Comments
 (0)