diff --git a/2 sum solution b/2 sum solution new file mode 100644 index 0000000..a41c582 --- /dev/null +++ b/2 sum solution @@ -0,0 +1,30 @@ +class Solution { +public: + vector twoSum(vector& nums, int target) + { + int size = nums.size(); + + // x, target-x -> add to target. + + map pos_map; + + for(int i=0; i{i, pos_map[to_find]-1}; + + } + + pos_map[curr_num] = i+1; + // done :) + + } + + return vector{-1,-1}; + } diff --git a/39.cpp b/39.cpp new file mode 100644 index 0000000..d1586a1 --- /dev/null +++ b/39.cpp @@ -0,0 +1,55 @@ +//https://leetcode.com/problems/combination-sum/ +//Difficulty Level: Medium +//Tags: Array, Backtracking +//since we have to print all, and not just count or max or min, we won't use DP but backtracking + +class Solution { +public: + vector> combinationSum(vector& candidates, int target) + { + sort(candidates.begin(), candidates.end()); + + //Note that we don't have duplicates in the array, else we had to delete the duplicates here bcoz + //we can already take each element multiple times, so duplicate elements don't make a difference + + int index = candidates.size()-1; + while(index >=0 && candidates[index] > target) + { + index--; + } + + vector v; + vector> res; //stores result + backtrack(candidates, target, 0, index, v, res); + return res; + } + + void backtrack(vector candidates, int target, int curr_sum, int index, vector v, vector>& res) + { + if(curr_sum == target) //if the sum of elements of v add up to target, push v to result vector + { + res.push_back(v); + return; + } + + //check all the elements <= target - curr_sum + for(int i=index; i>=0; i--) + { + curr_sum += candidates[i]; + + if(curr_sum > target) //don't include the element if sum is exceeding the target + { + curr_sum -= candidates[i]; + continue; + } + v.push_back(candidates[i]); + + //backtrack to find rest of the elements of v + //note that we have passed 'i' and not 'i+1' since we could include the same element any no. of times + backtrack(candidates, target, curr_sum, i, v, res); + + curr_sum -= candidates[i]; + v.pop_back(); + } + } +}; \ No newline at end of file diff --git a/56.cpp b/56.cpp new file mode 100644 index 0000000..bb016e0 --- /dev/null +++ b/56.cpp @@ -0,0 +1,21 @@ +class Solution { +public: + vector> merge(vector>& intervals) { + vectorarr(2); + vector>ans; + if(intervals.size()==0) return ans; + sort(intervals.begin(),intervals.end()); + arr[0]=intervals[0][0];arr[1]=intervals[0][1]; + for(int i=1;i=intervals[i][0]) + arr[1]=max(intervals[i][1],arr[1]); + else + { ans.push_back({arr[0],arr[1]}); + arr[0]=intervals[i][0]; + arr[1]=intervals[i][1]; + } + } + ans.push_back({arr[0],arr[1]}); + return ans; + } +}; \ No newline at end of file diff --git a/59.cpp b/59.cpp new file mode 100644 index 0000000..f29428d --- /dev/null +++ b/59.cpp @@ -0,0 +1,35 @@ +class Solution { +public: + + vector> generateMatrix(int n) { + vector> matrix; + matrix.resize(n, std::vector(n, -1)); + + int top = 0; + int bottom = n-1; + int left = 0; + int right = n-1; + int val = 1; + while (left<=right) { + + for (int i = left;i<=right;i++) { + matrix[left][i] = val++; + } + top++; + + for (int i = top;i<=bottom;i++) { + matrix[i][right] = val++; + } + right--; + for (int i = right;i>=left;i--) { + matrix[bottom][i] = val++; + } + bottom--; + for (int i = bottom;i>=top;i--) { + matrix[i][left] = val++; + } + left++; + } + return matrix; + } +}; \ No newline at end of file diff --git a/60.cpp b/60.cpp new file mode 100644 index 0000000..ed28542 --- /dev/null +++ b/60.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + string getPermutation(int n, int k) { + string st(n,'0'); + int fact=1; + for(int i=1;i<=n;i++){ + fact*=i; + st[i-1]+=(i); + } + for(int i=0;i=i+1;j--){ + st[j]=st[j-1]; + } + st[i]=c; + cout<& A) { + int n=A.size(); + stacks; + vectornsr; + for(int i=n-1;i>=0;i--) + { + if(s.empty()) + { + nsr.push_back(n); + } + else if(A[s.top()]=A[i]) + { + while(!s.empty()&&A[s.top()]>=A[i]) + { + s.pop(); + } + if(s.empty()) + { + nsr.push_back(n); + } + else + nsr.push_back(s.top()); + } + s.push(i); + } + reverse(nsr.begin(),nsr.end()); + stackss; + vectornsl; + for(int i=0;i=A[i]) + { + while(!ss.empty()&&A[ss.top()]>=A[i]) + ss.pop(); + if(ss.empty()) + { + nsl.push_back(-1); + } + else + nsl.push_back(ss.top()); + } + ss.push(i); + } + int ans=0; + for(int i=0;is; + vectornsr; + for(int i=n-1;i>=0;i--) + { + if(s.empty()) + { + nsr.push_back(n); + } + else if(A[s.top()]=A[i]) + { + while(!s.empty()&&A[s.top()]>=A[i]) + { + s.pop(); + } + if(s.empty()) + { + nsr.push_back(n); + } + else + nsr.push_back(s.top()); + } + s.push(i); + } + reverse(nsr.begin(),nsr.end()); + stackss; + vectornsl; + for(int i=0;i=A[i]) + { + while(!ss.empty()&&A[ss.top()]>=A[i]) + ss.pop(); + if(ss.empty()) + { + nsl.push_back(-1); + } + else + nsl.push_back(ss.top()); + } + ss.push(i); + } + int ans=0; + for(int i=0;i& nums1, vector& nums2) + { + int n = nums1.size(),m = nums2.size(); + + int l = 0,h = n; + while(l<=h) + { + int part1= (l+h)/2; + int part2=(n+m+1)/2 - part1; + + int l1=(part1==0)? INT_MIN: nums1[part1-1]; + int r1= (part1==n)? INT_MAX: nums1[part1]; + int l2=(part2==0)? INT_MIN: nums2[part2-1]; + int r2= (part2==m)? INT_MAX: nums2[part2]; + + if(l1<=r2 && l2<=r1) + { + if((n+m)%2) + return (double)max(l1,l2); + else + { + return (double)(max(l1,l2)+ min(r1,r2))/2.0; + } + } + + else if(l1>r2) + h=part1-1; + else + l=part1+1; + } + return 0; + } + double findMedianSortedArrays(vector& nums1, vector& nums2) { + int n=nums1.size(),m=nums2.size(); + if(n>m)return find(nums2,nums1); + else return find(nums1,nums2); + + } + +}; \ No newline at end of file diff --git a/hactoberfest.cpp b/hactoberfest.cpp new file mode 100644 index 0000000..c24eb72 --- /dev/null +++ b/hactoberfest.cpp @@ -0,0 +1 @@ +cout<<"hactoberfest2020"; diff --git a/leetcode.com-explore-challenge-card-september-leetcoding-challenge-555-week-2-september-8th-september-14th-3453 b/leetcode.com-explore-challenge-card-september-leetcoding-challenge-555-week-2-september-8th-september-14th-3453 new file mode 100644 index 0000000..4914d53 --- /dev/null +++ b/leetcode.com-explore-challenge-card-september-leetcoding-challenge-555-week-2-september-8th-september-14th-3453 @@ -0,0 +1,25 @@ +/** + * 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 { + int sum1(TreeNode* root , int sum){ + if(!root) return 0; + sum = (sum << 1) + root->val; + if(!root->right && !root->left) return sum; + return sum1(root->left , sum) + sum1(root->right , sum); + } +public: + int sumRootToLeaf(TreeNode* root) { + return sum1(root , 0); + } +}; + + diff --git a/leetcode.com-explore-challenge-card-september-leetcoding-challenge-555-week-2-september-8th-september-14th-3456 b/leetcode.com-explore-challenge-card-september-leetcoding-challenge-555-week-2-september-8th-september-14th-3456 new file mode 100644 index 0000000..bc7649a --- /dev/null +++ b/leetcode.com-explore-challenge-card-september-leetcoding-challenge-555-week-2-september-8th-september-14th-3456 @@ -0,0 +1,27 @@ +class Solution { +public: + int maxProduct(vector& nums) { + + int prev_max_product = nums[0] , prev_min_product = nums[0]; + int curr_max_product = nums[0] , curr_min_product = nums[0]; + int result = nums[0]; + + for(int i = 1; i < nums.size() ; i++){ + + curr_max_product = max(prev_max_product*nums[i] , prev_min_product*nums[i]); + curr_max_product = max(curr_max_product , nums[i]); + + curr_min_product = min(prev_max_product*nums[i] , prev_min_product*nums[i]); + curr_min_product = min(curr_min_product , nums[i]); + + result = max(curr_max_product , result); + + prev_max_product = curr_max_product; + prev_min_product = curr_min_product; + + } + return result; + } +}; + + diff --git a/leetcode.com-explore-challenge-card-september-leetcoding-challenge-555-week-2-september-8th-september-14th-3458 b/leetcode.com-explore-challenge-card-september-leetcoding-challenge-555-week-2-september-8th-september-14th-3458 new file mode 100644 index 0000000..62cf206 --- /dev/null +++ b/leetcode.com-explore-challenge-card-september-leetcoding-challenge-555-week-2-september-8th-september-14th-3458 @@ -0,0 +1,24 @@ +class Solution { +public: + vector> insert(vector>& intervals, vector& newInterval) { + + vector> result; + int i = 0 , n = intervals.size(); + + while(i < n && intervals[i][1] < newInterval[0]) result.push_back(intervals[i++]); + + vector mi = newInterval; + while(i < n && intervals[i][0] <= newInterval[1]){ + mi[0] = min(mi[0] , intervals[i][0]); + mi[1] = max(mi[1] , intervals[i++][1]); + } + + result.push_back(mi); + + while(i < n ) result.push_back(intervals[i++]); + + return result; + } +}; + +