Skip to content

Commit d4c95ce

Browse files
authored
Merge pull request #1566 from PDKhan/main
[PDKhan] WEEK 11 solutions
2 parents 210fabc + d18d1f0 commit d4c95ce

File tree

5 files changed

+178
-0
lines changed

5 files changed

+178
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public:
3+
int dfs(TreeNode* root, int& result){
4+
if(root == nullptr)
5+
return 0;
6+
7+
int left = max(0, dfs(root->left, result));
8+
int right = max(0, dfs(root->right, result));
9+
int sum = root->val + left + right;
10+
11+
result = max(result, sum);
12+
13+
return root->val + max(left, right);
14+
}
15+
16+
int maxPathSum(TreeNode* root) {
17+
int result = INT_MIN;
18+
19+
dfs(root, result);
20+
return result;
21+
}
22+
};

graph-valid-tree/PDKhan.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
class Solution {
2+
public:
3+
bool dfs(int curr, int parent, vector<vector<int>>& graph, vector<int>& visited){
4+
if(visited[curr])
5+
return false;
6+
7+
visited[curr] = 1;
8+
9+
for(int i = 0; i < graph[curr].size(); i++){
10+
if(graph[curr][i] == parent)
11+
continue;
12+
13+
if(dfs(graph[curr][i], curr, graph, visited) == false)
14+
return false;
15+
}
16+
17+
return true;
18+
}
19+
20+
bool validTree(int n, vector<vector<int>> &edges) {
21+
// write your code here
22+
if(edges.size() != n - 1)
23+
return false;
24+
25+
vector<vector<int>> graph (n);
26+
vector<int> visited (n, 0);
27+
28+
for(int i = 0; i < edges.size(); i++){
29+
int first = edges[i][0];
30+
int second = edges[i][1];
31+
32+
graph[first].push_back(second);
33+
graph[second].push_back(first);
34+
}
35+
36+
if(dfs(0, -1, graph, visited) == false)
37+
return false;
38+
39+
for(int i = 0; i < n; i++){
40+
if(visited[i] == 0)
41+
return false;
42+
}
43+
44+
return true;
45+
}
46+
};

merge-intervals/PDKhan.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> merge(vector<vector<int>>& intervals) {
4+
if(intervals.empty())
5+
return {};
6+
7+
sort(intervals.begin(), intervals.end(), [](const vector<int>& a, const vector<int>& b){
8+
return a[0] < b[0];
9+
});
10+
11+
vector<vector<int>> result;
12+
result.push_back(intervals[0]);
13+
14+
for(int i = 0; i < intervals.size(); i++){
15+
if(result.back()[1] >= intervals[i][0])
16+
result.back()[1] = max(result.back()[1], intervals[i][1]);
17+
else
18+
result.push_back(intervals[i]);
19+
}
20+
21+
return result;
22+
}
23+
};

missing-number/PDKhan.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
int missingNumber(vector<int>& nums) {
4+
int sum1 = 0;
5+
int sum2 = 0;
6+
7+
for(int i = 0; i < nums.size(); i++){
8+
sum1 += nums[i];
9+
sum2 += i + 1;
10+
}
11+
12+
return sum2 - sum1;
13+
}
14+
};

reorder-list/PDKhan.cpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// space O(n)
2+
class Solution {
3+
public:
4+
void reorderList(ListNode* head) {
5+
ListNode* search = head->next;
6+
ListNode* tail = head;
7+
deque<ListNode*> q;
8+
9+
while(search){
10+
ListNode* next = search->next;
11+
12+
search->next = NULL;
13+
q.push_back(search);
14+
search = next;
15+
}
16+
17+
for(int i = 0; !q.empty(); i++){
18+
if(i % 2 == 0){
19+
tail->next = q.back();
20+
q.pop_back();
21+
}else{
22+
tail->next = q.front();
23+
q.pop_front();
24+
}
25+
26+
tail = tail->next;
27+
}
28+
}
29+
};
30+
31+
32+
// space O(1)
33+
class Solution {
34+
public:
35+
void reorderList(ListNode* head) {
36+
if(head == NULL || head->next == NULL)
37+
return;
38+
39+
ListNode* slow = head;
40+
ListNode* fast = head;
41+
42+
while(fast->next && fast->next->next){
43+
slow = slow->next;
44+
fast = fast->next->next;
45+
}
46+
47+
ListNode* prev = nullptr;
48+
ListNode* curr = slow->next;
49+
50+
while(curr){
51+
ListNode* next = curr->next;
52+
curr->next = prev;
53+
prev = curr;
54+
curr = next;
55+
}
56+
57+
slow->next = nullptr;
58+
59+
ListNode* first = head;
60+
ListNode* second = prev;
61+
62+
while(first && second){
63+
ListNode* next1 = first->next;
64+
ListNode* next2 = second->next;
65+
66+
first->next = second;
67+
second->next = next1;
68+
69+
first = next1;
70+
second = next2;
71+
}
72+
}
73+
};

0 commit comments

Comments
 (0)