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; + } + }; 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; + } + }; 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; + } + }; 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; + } + }; 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; + } + } + };