From 4d0c3f9f363818d7177b092423adac3bd100d3ea Mon Sep 17 00:00:00 2001 From: PDKhan Date: Sun, 8 Jun 2025 21:57:20 -0500 Subject: [PATCH 1/5] Missing Number Solution --- missing-number/PDKhan.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 missing-number/PDKhan.cpp diff --git a/missing-number/PDKhan.cpp b/missing-number/PDKhan.cpp new file mode 100644 index 000000000..cae451a1e --- /dev/null +++ b/missing-number/PDKhan.cpp @@ -0,0 +1,14 @@ +class Solution { + public: + int missingNumber(vector& nums) { + int sum1 = 0; + int sum2 = 0; + + for(int i = 0; i < nums.size(); i++){ + sum1 += nums[i]; + sum2 += i + 1; + } + + return sum2 - sum1; + } + }; From 7d6ff24a8d5bdc2b43dac5efa7a95de393aee53d Mon Sep 17 00:00:00 2001 From: PDKhan Date: Sun, 8 Jun 2025 21:57:43 -0500 Subject: [PATCH 2/5] Reorder List Solution --- reorder-list/PDKhan.cpp | 73 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 reorder-list/PDKhan.cpp diff --git a/reorder-list/PDKhan.cpp b/reorder-list/PDKhan.cpp new file mode 100644 index 000000000..ff6a6052b --- /dev/null +++ b/reorder-list/PDKhan.cpp @@ -0,0 +1,73 @@ +// space O(n) +class Solution { + public: + void reorderList(ListNode* head) { + ListNode* search = head->next; + ListNode* tail = head; + deque 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; + } + } + }; From 9784830cdf3bf82783476ffd51e5f3b16c36c3fe Mon Sep 17 00:00:00 2001 From: PDKhan Date: Sun, 8 Jun 2025 21:57:59 -0500 Subject: [PATCH 3/5] Graph Valid Tree Solution --- graph-valid-tree/PDKhan.cpp | 46 +++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 graph-valid-tree/PDKhan.cpp diff --git a/graph-valid-tree/PDKhan.cpp b/graph-valid-tree/PDKhan.cpp new file mode 100644 index 000000000..a1588f24b --- /dev/null +++ b/graph-valid-tree/PDKhan.cpp @@ -0,0 +1,46 @@ +class Solution { + public: + bool dfs(int curr, int parent, vector>& graph, vector& 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> &edges) { + // write your code here + if(edges.size() != n - 1) + return false; + + vector> graph (n); + vector 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; + } + }; From 02a83d5cd3edb0634b49e54996f98a1f9ae2daab Mon Sep 17 00:00:00 2001 From: PDKhan Date: Sun, 8 Jun 2025 21:58:16 -0500 Subject: [PATCH 4/5] Merge Intervals Solution --- merge-intervals/PDKhan.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 merge-intervals/PDKhan.cpp diff --git a/merge-intervals/PDKhan.cpp b/merge-intervals/PDKhan.cpp new file mode 100644 index 000000000..1dce46c12 --- /dev/null +++ b/merge-intervals/PDKhan.cpp @@ -0,0 +1,23 @@ +class Solution { + public: + vector> merge(vector>& intervals) { + if(intervals.empty()) + return {}; + + sort(intervals.begin(), intervals.end(), [](const vector& a, const vector& b){ + return a[0] < b[0]; + }); + + vector> 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; + } + }; From d18d1f0a972a0757ad85b19401343c13fe5a2813 Mon Sep 17 00:00:00 2001 From: PDKhan Date: Sun, 8 Jun 2025 21:58:42 -0500 Subject: [PATCH 5/5] Binary Tree Maximum Path Sum Solution --- binary-tree-maximum-path-sum/PDKhan.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 binary-tree-maximum-path-sum/PDKhan.cpp diff --git a/binary-tree-maximum-path-sum/PDKhan.cpp b/binary-tree-maximum-path-sum/PDKhan.cpp new file mode 100644 index 000000000..8a4bf0fb8 --- /dev/null +++ b/binary-tree-maximum-path-sum/PDKhan.cpp @@ -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; + } + };