Skip to content

[PDKhan] WEEK 11 solutions #1566

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 15, 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
22 changes: 22 additions & 0 deletions binary-tree-maximum-path-sum/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution {
public:
int dfs(TreeNode* root, int& result){
if(root == nullptr)
return 0;

int left = max(0, dfs(root->left, result));
int right = max(0, dfs(root->right, result));
int sum = root->val + left + right;

result = max(result, sum);

return root->val + max(left, right);
}

int maxPathSum(TreeNode* root) {
int result = INT_MIN;

dfs(root, result);
return result;
}
};
46 changes: 46 additions & 0 deletions graph-valid-tree/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class Solution {
public:
bool dfs(int curr, int parent, vector<vector<int>>& graph, vector<int>& visited){
if(visited[curr])
return false;

visited[curr] = 1;

for(int i = 0; i < graph[curr].size(); i++){
if(graph[curr][i] == parent)
continue;

if(dfs(graph[curr][i], curr, graph, visited) == false)
return false;
}

return true;
}

bool validTree(int n, vector<vector<int>> &edges) {
// write your code here
if(edges.size() != n - 1)
return false;

vector<vector<int>> graph (n);
vector<int> visited (n, 0);

for(int i = 0; i < edges.size(); i++){
int first = edges[i][0];
int second = edges[i][1];

graph[first].push_back(second);
graph[second].push_back(first);
}

if(dfs(0, -1, graph, visited) == false)
return false;

for(int i = 0; i < n; i++){
if(visited[i] == 0)
return false;
}

return true;
}
};
23 changes: 23 additions & 0 deletions merge-intervals/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution {
public:
vector<vector<int>> merge(vector<vector<int>>& intervals) {
if(intervals.empty())
return {};

sort(intervals.begin(), intervals.end(), [](const vector<int>& a, const vector<int>& b){
return a[0] < b[0];
});

vector<vector<int>> result;
result.push_back(intervals[0]);

for(int i = 0; i < intervals.size(); i++){
if(result.back()[1] >= intervals[i][0])
result.back()[1] = max(result.back()[1], intervals[i][1]);
else
result.push_back(intervals[i]);
}

return result;
}
};
14 changes: 14 additions & 0 deletions missing-number/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution {
public:
int missingNumber(vector<int>& nums) {
int sum1 = 0;
int sum2 = 0;

for(int i = 0; i < nums.size(); i++){
sum1 += nums[i];
sum2 += i + 1;
}

return sum2 - sum1;
}
};
73 changes: 73 additions & 0 deletions reorder-list/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// space O(n)
class Solution {
public:
void reorderList(ListNode* head) {
ListNode* search = head->next;
ListNode* tail = head;
deque<ListNode*> q;

while(search){
ListNode* next = search->next;

search->next = NULL;
q.push_back(search);
search = next;
}

for(int i = 0; !q.empty(); i++){
if(i % 2 == 0){
tail->next = q.back();
q.pop_back();
}else{
tail->next = q.front();
q.pop_front();
}

tail = tail->next;
}
}
};


// space O(1)
class Solution {
public:
void reorderList(ListNode* head) {
if(head == NULL || head->next == NULL)
return;

ListNode* slow = head;
ListNode* fast = head;

while(fast->next && fast->next->next){
slow = slow->next;
fast = fast->next->next;
}

ListNode* prev = nullptr;
ListNode* curr = slow->next;

while(curr){
ListNode* next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}

slow->next = nullptr;

ListNode* first = head;
ListNode* second = prev;

while(first && second){
ListNode* next1 = first->next;
ListNode* next2 = second->next;

first->next = second;
second->next = next1;

first = next1;
second = next2;
}
}
};