From 6b470e61fb4b89c370e594ea97285aef08ecc976 Mon Sep 17 00:00:00 2001 From: Vimal Anand <78069624+Vimal2023@users.noreply.github.com> Date: Wed, 18 Oct 2023 20:27:21 +0530 Subject: [PATCH 1/2] Create 3sum-leetcode.cpp Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0. --- 03-Arrays/3sum-leetcode.cpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 03-Arrays/3sum-leetcode.cpp diff --git a/03-Arrays/3sum-leetcode.cpp b/03-Arrays/3sum-leetcode.cpp new file mode 100644 index 0000000..3cc3ff6 --- /dev/null +++ b/03-Arrays/3sum-leetcode.cpp @@ -0,0 +1,29 @@ +//Optimized Approach - O(n^2 logn + nlogn) - o(n^2 logn) time and O(n) space +class Solution { +public: + vector<vector<int>> threeSum(vector<int>& nums) { + int target = 0; + sort(nums.begin(), nums.end()); + set<vector<int>> s; + vector<vector<int>> output; + for (int i = 0; i < nums.size(); i++){ + int j = i + 1; + int k = nums.size() - 1; + while (j < k) { + int sum = nums[i] + nums[j] + nums[k]; + if (sum == target) { + s.insert({nums[i], nums[j], nums[k]}); + j++; + k--; + } else if (sum < target) { + j++; + } else { + k--; + } + } + } + for(auto triplets : s) + output.push_back(triplets); + return output; + } +}; From f9859981ebffdafeac50e5dd03700cb680744325 Mon Sep 17 00:00:00 2001 From: Vimal Anand <78069624+Vimal2023@users.noreply.github.com> Date: Wed, 18 Oct 2023 20:38:25 +0530 Subject: [PATCH 2/2] Create Path-Sum-II.cpp Given the root of a binary tree and an integer targetSum, return all root-to-leaf paths where the sum of the node values in the path equals targetSum. Each path should be returned as a list of the node values, not node references. A root-to-leaf path is a path starting from the root and ending at any leaf node. A leaf is a node with no children. --- 10- Recursion & Backtracking/Path-Sum-II.cpp | 43 ++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 10- Recursion & Backtracking/Path-Sum-II.cpp diff --git a/10- Recursion & Backtracking/Path-Sum-II.cpp b/10- Recursion & Backtracking/Path-Sum-II.cpp new file mode 100644 index 0000000..18da2e4 --- /dev/null +++ b/10- Recursion & Backtracking/Path-Sum-II.cpp @@ -0,0 +1,43 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + + vector<vector<int>> res; //to store final result + + void traverse(TreeNode* root, vector <int> &path, int rem){ + if(!root) return; //return at NULL + + if(!root->left && !root->right){ //leaf node reached + path.push_back(root->val); //do + if(rem == root->val) res.push_back(path); + //if remaining sum is same as value of root we have valid path + path.pop_back(); // undo + return; + } + + int val = root->val; //subtract the contribution of this value from rem (remaining sum) + path.push_back(val); //do + traverse(root->left, path, rem-val); //recurse left subtree + traverse(root->right, path, rem-val); //recurse right subtree + path.pop_back(); //undo + + } + + vector<vector<int>> pathSum(TreeNode* root, int targetSum) { + res.clear(); //reset res + if(!root) return res; //if root itself NULL there are no paths + vector <int> path; //to store the path + traverse(root, path, targetSum); + return res; + } +};