Skip to content

[PDKhan] WEEK 12 solutions #1598

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions non-overlapping-intervals/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution {
public:
int eraseOverlapIntervals(vector<vector<int>>& intervals) {
sort(intervals.begin(), intervals.end(), [](const vector<int>& a, const vector<int>& b){ return a[1] < b[1];});

int result = 0;
int end = intervals[0][1];

for(int i = 1; i < intervals.size(); i++){
if(intervals[i][0] < end)
result++;
else
end = intervals[i][1];
}

return result;
}
};
37 changes: 37 additions & 0 deletions number-of-connected-components-in-an-undirected-graph/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class Solution {
public:
/**
* @param n: the number of vertices
* @param edges: the edges of undirected graph
* @return: the number of connected components
*/
void dfs(int curr, vector<vector<int>>& graph, vector<bool>& visited){
visited[curr] = true;

for(int i = 0; i < graph[curr].size(); i++){
if(visited[graph[curr][i]] == false)
dfs(graph[curr][i], graph, visited);
}
}

int countComponents(int n, vector<vector<int>> &edges) {
// write your code here
vector<bool> visited(n, 0);
vector<vector<int>> graph (n);
int cnt = 0;

for(int i = 0; i < edges.size(); i++){
graph[edges[i][0]].push_back(edges[i][1]);
graph[edges[i][1]].push_back(edges[i][0]);
}

for(int i = 0; i < n; i++){
if(visited[i] == false){
dfs(i, graph, visited);
cnt++;
}
}

return cnt;
}
};
29 changes: 29 additions & 0 deletions remove-nth-node-from-end-of-list/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* prev = NULL;
ListNode* curr = head;
ListNode* next = head;

for(int i = 0; next && i < n; i++)
next = next->next;

while(next){
if(prev == nullptr)
prev = head;
else
prev = prev->next;
curr = curr->next;
next = next->next;
}

if(prev == nullptr)
head = curr->next;
else
prev->next = curr->next;

delete(curr);

return head;
}
};
13 changes: 13 additions & 0 deletions same-tree/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution {
public:
bool isSameTree(TreeNode* p, TreeNode* q) {
if(!p && !q)
return true;
else if(!p || !q)
return false;
else if(p->val != q->val)
return false;

return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
}
};
69 changes: 69 additions & 0 deletions serialize-and-deserialize-binary-tree/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
class Codec {
public:
// Encodes a tree to a single string.
string serialize(TreeNode* root) {
string result;

if(root == nullptr)
return result;

queue<TreeNode*> q;

q.push(root);

while(!q.empty()){
TreeNode* curr = q.front();

q.pop();

if(curr){
result += to_string(curr->val) + ",";
q.push(curr->left);
q.push(curr->right);
}else{
result += "NULL,";
}
}

return result;
}
// Decodes your encoded data to tree.
TreeNode* deserialize(string data) {
if(!data.size())
return nullptr;

queue<TreeNode*> q;
vector<string> values;
size_t prev = 0;
size_t pos = data.find(",", prev);

while(pos != string::npos){
values.push_back(data.substr(prev, pos - prev));
prev = pos + 1;
pos = data.find(",", prev);
}

int idx = 0;
TreeNode* root = new TreeNode(stoi(values[idx++]));
q.push(root);

while(!q.empty()){
int left = idx++;
int right = idx++;
TreeNode* curr = q.front();
q.pop();

if(left < values.size() && values[left] != "NULL"){
curr->left = new TreeNode(stoi(values[left]));
q.push(curr->left);
}

if(right < values.size() && values[right] != "NULL"){
curr->right = new TreeNode(stoi(values[right]));
q.push(curr->right);
}
}

return root;
}
};