diff --git a/leetcode/cpp/Array/11.cpp b/leetcode/cpp/Array/11.cpp new file mode 100644 index 00000000..d20aea43 --- /dev/null +++ b/leetcode/cpp/Array/11.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + int maxArea(vector& height) { + int left=0,right=height.size()-1,area,x,y,temp; + while(leftarea){ + area=temp; + } + if(height[left]& prices) { + if(!prices.size()) return 0; + int profit=0,max_profit=INT_MIN,min=prices[0]; + for(int i=0;imax_profit) + max_profit=profit; + } + return max_profit; + } +}; \ No newline at end of file diff --git a/leetcode/cpp/Array/15.cpp b/leetcode/cpp/Array/15.cpp new file mode 100644 index 00000000..02c2be25 --- /dev/null +++ b/leetcode/cpp/Array/15.cpp @@ -0,0 +1,34 @@ +class Solution { +public: + vector> threeSum(vector& nums) { + vector> triplets; + if(nums.size()<3) return triplets; + vector sortedNums; + copy(nums.begin(),nums.end(),back_inserter(sortedNums)); + sort(sortedNums.begin(),sortedNums.end()); + int indexA,indexB,indexC,a,sum; + for(int i=0;i0) return triplets; + if(i>0 && a==sortedNums[indexA-1]) continue; + indexB=i+1; + indexC=sortedNums.size()-1; + while(indexBindexB && sortedNums[indexC]==sortedNums[indexC-1]) indexC--; + indexB++; + indexC--; + }else if(sum<0){ + indexB++; + }else{ + indexC--; + } + } + } + return triplets; + } +}; \ No newline at end of file diff --git a/leetcode/cpp/Array/152.cpp b/leetcode/cpp/Array/152.cpp new file mode 100644 index 00000000..8e0e62dc --- /dev/null +++ b/leetcode/cpp/Array/152.cpp @@ -0,0 +1,16 @@ +class Solution { +public: + int maxProduct(vector& nums) + { + if(!nums.size()) return 0; + int mx=nums[0],mn=nums[0],ans=nums[0],x,y; + for(int i=1;i& nums, int target) { + if(nums.size()<3) return 0; + vector sortedNums; + copy(nums.begin(),nums.end(),back_inserter(sortedNums)); + sort(sortedNums.begin(),sortedNums.end()); + int ans,a,sum,diff,mindiff=INT_MAX,indexA,indexB,indexC; + for(int i=0;i0 && a==sortedNums[indexA-1]) continue; + indexB=i+1; + indexC=sortedNums.size()-1; + while(indexB& nums) { + int n=nums.size(); + if (n<=1) return n; + int j = 0; + for (int i=0; i < n-1; i++) + if (nums[i] != nums[i+1]) + nums[j++] = nums[i]; + + nums[j++] = nums[n-1]; + + return j; + } +}; \ No newline at end of file diff --git a/leetcode/cpp/Array/39.cpp b/leetcode/cpp/Array/39.cpp new file mode 100644 index 00000000..c6cab4f2 --- /dev/null +++ b/leetcode/cpp/Array/39.cpp @@ -0,0 +1,23 @@ +class Solution { +public: + void helper(vector& candidates,vector>& ans,vector& combination,int target,int start){ + if(target==0){ + ans.push_back(combination); + return; + } + for(int i=start;itarget) break; + combination.push_back(candidates[i]); + helper(candidates,ans,combination,target-candidates[i],i); + combination.pop_back(); + } + } + + vector> combinationSum(vector& candidates, int target) { + vector> ans; + vector combination; + sort(candidates.begin(),candidates.end()); + helper(candidates,ans,combination,target,0); + return ans; + } +}; \ No newline at end of file diff --git a/leetcode/cpp/Array/40.cpp b/leetcode/cpp/Array/40.cpp new file mode 100644 index 00000000..2050b014 --- /dev/null +++ b/leetcode/cpp/Array/40.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + void helper(vector& candidates,vector& combination,vector>& ans,int target,int start){ + if(target==0){ + ans.push_back(combination); + return; + } + for(int i=start;itarget) break; + if(i>start && candidates[i]==candidates[i-1]) continue; + combination.push_back(candidates[i]); + helper(candidates,combination,ans,target-candidates[i],i+1); + combination.pop_back(); + } + } + + vector> combinationSum2(vector& candidates, int target) { + vector> ans; + vector combination; + sort(candidates.begin(),candidates.end()); + helper(candidates,combination,ans,target,0); + return ans; + } +}; \ No newline at end of file diff --git a/leetcode/cpp/Array/48.cpp b/leetcode/cpp/Array/48.cpp new file mode 100644 index 00000000..ef7e1cc2 --- /dev/null +++ b/leetcode/cpp/Array/48.cpp @@ -0,0 +1,21 @@ +class Solution { +public: + void swap(int *x,int *y){ + int temp=*x; + *x=*y; + *y=temp; + } + + void rotate(vector>& matrix) { + int n=matrix.size(),temp; + for(int j=0;j<(n/2);j++){ + for(int i=j;i<(n-1-j);i++){ + temp=matrix[j][i]; + swap(&temp,&matrix[i][n-1-j]); + swap(&temp,&matrix[n-1-j][n-1-i]); + swap(&temp,&matrix[n-1-i][j]); + swap(&temp,&matrix[j][i]); + } + } + } +}; \ No newline at end of file diff --git a/leetcode/cpp/Array/621.cpp b/leetcode/cpp/Array/621.cpp new file mode 100644 index 00000000..30a2f33b --- /dev/null +++ b/leetcode/cpp/Array/621.cpp @@ -0,0 +1,16 @@ +class Solution { +public: + int leastInterval(vector& tasks, int n) { + vector count(26,0); + int max_freq=0,min_units,task_count=tasks.size(); + for(auto elem: tasks){ + count[elem-'A']++; + if(count[elem-'A']>max_freq) max_freq=count[elem-'A']; + } + min_units=(max_freq-1)*(n+1); + for(int i=0;i<26;i++){ + if(count[i]==max_freq) min_units++; + } + return max(min_units,task_count); + } +}; \ No newline at end of file diff --git a/leetcode/cpp/Binary Search/162.cpp b/leetcode/cpp/Binary Search/162.cpp new file mode 100644 index 00000000..45253883 --- /dev/null +++ b/leetcode/cpp/Binary Search/162.cpp @@ -0,0 +1,30 @@ +//brute force +class Solution { +public: + int findPeakElement(vector& nums) { + bool flag; + for(int i=0;i0) flag&=(nums[i]>nums[i-1]); + if(inums[i+1]); + if(flag) return i; + } + return -1; + } +}; + +//binary search +class Solution { +public: + int findPeakElement(vector& nums) { + int l=0,r=nums.size()-1,mid; + while(l+1nums[r]) return l; + return r; + } +}; \ No newline at end of file diff --git a/leetcode/cpp/Binary Search/278.cpp b/leetcode/cpp/Binary Search/278.cpp new file mode 100644 index 00000000..db39b587 --- /dev/null +++ b/leetcode/cpp/Binary Search/278.cpp @@ -0,0 +1,12 @@ +class Solution { +public: + int firstBadVersion(int n) { + long long int l=1,r=n,mid; + while(l<=r){ + mid=(l+r)/2; + if(isBadVersion(mid)) r=mid-1; + else l=mid+1; + } + return l; + } +}; \ No newline at end of file diff --git a/leetcode/cpp/Binary Search/287.cpp b/leetcode/cpp/Binary Search/287.cpp new file mode 100644 index 00000000..ff360622 --- /dev/null +++ b/leetcode/cpp/Binary Search/287.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + bool duplicateRange(vector &nums,int l,int r){ + int count=0; + for(int i=0;i=l && nums[i]<=r) ++count; + } + return r-l+1& nums) { + int l=1,r=nums.size()-1,mid; + while(l &x,int target){ + int m=x.size(),left=0,right=x.size(),pos; + while(left=x[pos]){ + if(pos& nums1, vector& nums2) { + int m=nums1.size(),n=nums2.size(); + unsigned int pos=0; + if(m && n) pos=findpos(nums1,nums2[0]); + if(pos) pos-=1; + int i=pos,j=0; + vector v; + if(!m) v=nums1; + else if(!n) v=nums2; + else v.insert(v.end(),nums1.begin(),nums1.begin()+i); + while(true){ + if(i==m){ + v.insert(v.end(),nums2.begin()+j,nums2.end()); + break; + } + if(j==n){ + v.insert(v.end(),nums1.begin()+i,nums1.end()); + break; + } + if(nums1[i]<=nums2[j]){ + v.push_back(nums1[i]); + i++; + }else{ + v.push_back(nums2[j]); + j++; + } + } + int size=v.size(); + int mid=(int)size/2; + if(size%2){ + return v[mid]; + } + return (v[mid]+v[mid-1])/2.0; + } +}; \ No newline at end of file diff --git a/leetcode/cpp/Divide&Conquer/53.cpp b/leetcode/cpp/Divide&Conquer/53.cpp new file mode 100644 index 00000000..af535b7f --- /dev/null +++ b/leetcode/cpp/Divide&Conquer/53.cpp @@ -0,0 +1,12 @@ +class Solution { +public: + int maxSubArray(vector& nums) { + int sum=0,ans=INT_MIN; + for(auto i:nums){ + sum+=i; + ans=max(ans,sum); + sum=max(sum,0); + } + return ans; + } +}; \ No newline at end of file diff --git a/leetcode/cpp/Tree/100.cpp b/leetcode/cpp/Tree/100.cpp new file mode 100644 index 00000000..72be06ea --- /dev/null +++ b/leetcode/cpp/Tree/100.cpp @@ -0,0 +1,46 @@ +//method1: good old recursion +class Solution { +public: +bool isSameTree(TreeNode* p, TreeNode* q) { + if(!p && !q) return true; + if(!p || !q) return false; + bool c1=isSameTree(p->left,q->left); + bool c2=isSameTree(p->right,q->right); + bool c3=(p->val==q->val); + return c1 && c2 &&c3; + } +}; + +//method2: Level Order Traversal // BFS +class Solution { +public: + void levelOrderTraversal(TreeNode* root,vector &v){ + int h=height(root); + for(int i=1;i<=h;i++) traverseLevel(root,i,v); + } + + void traverseLevel(TreeNode* node,int level,vector &v){ + if(level==1){ + if(node) v.push_back(node->val); + else v.push_back(0); + } + else{ + if(node){ + traverseLevel(node->left,level-1,v); + traverseLevel(node->right,level-1,v); + } + } + } + + int height(TreeNode* node){ + if(!node) return 0; + return max(height(node->left),height(node->right))+1; + } + + bool isSameTree(TreeNode* p, TreeNode* q) { + vector v1,v2; + levelOrderTraversal(p,v1); + levelOrderTraversal(q,v2); + return v1==v2; + } +}; \ No newline at end of file diff --git a/leetcode/cpp/Tree/101.cpp b/leetcode/cpp/Tree/101.cpp new file mode 100644 index 00000000..c338f831 --- /dev/null +++ b/leetcode/cpp/Tree/101.cpp @@ -0,0 +1,46 @@ +//method1: +class Solution { +public: + void preOrder(TreeNode* node,vector &v){ + if(node){ + v.push_back(node->val); + preOrder(node->left,v); + preOrder(node->right,v); + }else{ + v.push_back(0); + } + } + + void postOrder(TreeNode* node,vector &v){ + if(node){ + postOrder(node->left,v); + postOrder(node->right,v); + v.push_back(node->val); + }else{ + v.push_back(0); + } + } + + bool isSymmetric(TreeNode* root) { + vector v1,v2; + if(!root) return true; + preOrder(root->left,v1); + postOrder(root->right,v2); + reverse(v2.begin(),v2.end()); + return v1==v2; + } +}; + +//method2: +class Solution { +public: + bool isSym(TreeNode* left,TreeNode* right){ + if(!left || !right) return left==right; + if(left->val!=right->val) return false; + return isSym(left->left,right->right) && isSym(left->right,right->left); + } + bool isSymmetric(TreeNode* root) { + if(!root) return true; + return isSym(root->left,root->right); + } +}; \ No newline at end of file diff --git a/leetcode/cpp/Tree/102.cpp b/leetcode/cpp/Tree/102.cpp new file mode 100644 index 00000000..9e548e04 --- /dev/null +++ b/leetcode/cpp/Tree/102.cpp @@ -0,0 +1,40 @@ +/** + * 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: + void levelTraverse(TreeNode* node,int level,vector &levelElems){ + if(!node) return; + if(level==1){ + levelElems.push_back(node->val); + }else{ + levelTraverse(node->left,level-1,levelElems); + levelTraverse(node->right,level-1,levelElems); + } + } + + int height(TreeNode* node){ + if(!node) return 0; + return max(height(node->left),height(node->right))+1; + } + + vector> levelOrder(TreeNode* root) { + vector> v; + vector levelElems; + int h=height(root); + for(int level=1;level<=h;level++){ + levelElems.clear(); + levelTraverse(root,level,levelElems); + v.push_back(levelElems); + } + return v; + } +}; \ No newline at end of file diff --git a/leetcode/cpp/Tree/103.cpp b/leetcode/cpp/Tree/103.cpp new file mode 100644 index 00000000..aeb68f47 --- /dev/null +++ b/leetcode/cpp/Tree/103.cpp @@ -0,0 +1,78 @@ +//method1: using recursion +class Solution { +public: + int height(TreeNode* node){ + if(!node) return 0; + return max(height(node->left),height(node->right))+1; + } + + void levelTraverse(TreeNode* node,vector &temp,int level){ + if(!node) return; + if(level==1) temp.push_back(node->val); + levelTraverse(node->left,temp,level-1); + levelTraverse(node->right,temp,level-1); + } + + vector> zigzagLevelOrder(TreeNode* root) { + vector> v; + vector temp; + int h=height(root); + for(int level=1;level<=h;level++){ + levelTraverse(root,temp,level); + if(level%2==0) reverse(temp.begin(),temp.end()); + v.push_back(temp); + temp.clear(); + } + return v; + } +}; + +//method2: using queue +vector> zigzag (TreeNode *root){ + queue> myqueue; + vector> ans; + vector temp; + TreeNode *lc ; + pair p; + + if( root == NULL){ + return ans; + } + + myqueue.push({0,root}); + while(!myqueue.empty()){ + p= myqueue.front(); + myqueue.pop(); + + if(p.first>ans.size()){ + if(ans.size()%2 ){ + reverse(temp.begin(),temp.end()); + } + ans.push_back(temp); + temp.clear(); + temp.push_back(p.second->val); + }else{ + temp.push_back(p.second->val); + } + + if(p.second->left != NULL){ + myqueue.push({p.first+1,p.second->left}); + } + + if(p.second->right != NULL){ + myqueue.push({p.first+1,p.second->right}); + } + } + if(ans.size()%2 ){ + reverse(temp.begin(),temp.end()); + } + ans.push_back(temp); + return ans; +} + +class Solution { +public: + vector> zigzagLevelOrder(TreeNode* root) { + return zigzag(root); + } +}; \ No newline at end of file diff --git a/leetcode/cpp/Tree/104.cpp b/leetcode/cpp/Tree/104.cpp new file mode 100644 index 00000000..9cac442d --- /dev/null +++ b/leetcode/cpp/Tree/104.cpp @@ -0,0 +1,7 @@ +class Solution { +public: + int maxDepth(TreeNode* root) { + if(!root) return 0; + return max(maxDepth(root->left),maxDepth(root->right))+1; + } +}; \ No newline at end of file diff --git a/leetcode/cpp/Tree/107.cpp b/leetcode/cpp/Tree/107.cpp new file mode 100644 index 00000000..c3d2d0c2 --- /dev/null +++ b/leetcode/cpp/Tree/107.cpp @@ -0,0 +1,26 @@ +class Solution { +public: + int height(TreeNode* node){ + if(!node) return 0; + return max(height(node->left),height(node->right))+1; + } + + void traverseLevel(TreeNode* node,int level,vector &temp){ + if(!node) return; + if(level==1) temp.push_back(node->val); + traverseLevel(node->left,level-1,temp); + traverseLevel(node->right,level-1,temp); + } + + vector> levelOrderBottom(TreeNode* root) { + vector> v; + vector temp; + int h=height(root); + for(int level=h;level>0;level--){ + traverseLevel(root,level,temp); + v.push_back(temp); + temp.clear(); + } + return v; + } +}; \ No newline at end of file diff --git a/leetcode/cpp/Tree/111.cpp b/leetcode/cpp/Tree/111.cpp new file mode 100644 index 00000000..297c4971 --- /dev/null +++ b/leetcode/cpp/Tree/111.cpp @@ -0,0 +1,9 @@ +class Solution { +public: + int minDepth(TreeNode* root) { + if(!root) return 0; + if(!root->left) return minDepth(root->right)+1; + if(!root->right) return minDepth(root->left)+1; + return min(minDepth(root->left),minDepth(root->right))+1; + } +}; \ No newline at end of file diff --git a/leetcode/cpp/Tree/112.cpp b/leetcode/cpp/Tree/112.cpp new file mode 100644 index 00000000..6bb4455b --- /dev/null +++ b/leetcode/cpp/Tree/112.cpp @@ -0,0 +1,8 @@ +class Solution { +public: + bool hasPathSum(TreeNode* root, int sum) { + if(!root) return false; + if(!root->left && !root->right) return sum==root->val; + return (hasPathSum(root->left,sum-root->val) || hasPathSum(root->right,sum-root->val)); + } +}; \ No newline at end of file diff --git a/leetcode/cpp/Tree/113.cpp b/leetcode/cpp/Tree/113.cpp new file mode 100644 index 00000000..bf2eb78d --- /dev/null +++ b/leetcode/cpp/Tree/113.cpp @@ -0,0 +1,49 @@ +//method1: dfs +class Solution { +public: + void findPaths(TreeNode* node,int sum,vector> &v,vector temp){ + if(!node) return; + temp.push_back(node->val); + if(!node->left && !node->right){ + if(node->val==sum) v.push_back(temp); + } + findPaths(node->left,sum-node->val,v,temp); + findPaths(node->right,sum-node->val,v,temp); + } + + vector> pathSum(TreeNode* root, int sum) { + vector> v; + vector temp; + findPaths(root,sum,v,temp); + return v; + } +}; + +//method2: backtracking +class Solution { +public: + void backtracking(vector> &out, TreeNode* root, int sum, int check, vector&temp){ + if(root == NULL) return ; + check += root->val ; + if(check == sum && root->left == NULL && root->right == NULL) out.push_back(temp); + if (root->left) { + temp.push_back(root->left->val); + backtracking(out, root->left, sum, check,temp); + temp.pop_back(); + } + if (root->right) { + temp.push_back(root->right->val); + backtracking(out, root->right, sum, check,temp); + temp.pop_back(); + } + } + + vector> pathSum(TreeNode* root, int sum) { + vector> out ; + vector temp ; + if(root == NULL) return out ; + temp.push_back (root->val) ; + backtracking(out, root, sum, 0,temp); + return out ; + } +}; \ No newline at end of file diff --git a/leetcode/cpp/Tree/144.cpp b/leetcode/cpp/Tree/144.cpp new file mode 100644 index 00000000..c2e9b4cb --- /dev/null +++ b/leetcode/cpp/Tree/144.cpp @@ -0,0 +1,15 @@ +class Solution { +public: + void preorderTraversal(TreeNode* node,vector &pre){ + if(!node) return; + pre.push_back(node->val); + preorderTraversal(node->left,pre); + preorderTraversal(node->right,pre); + } + + vector preorderTraversal(TreeNode* root) { + vector pre; + preorderTraversal(root,pre); + return pre; + } +}; \ No newline at end of file diff --git a/leetcode/cpp/Tree/145.cpp b/leetcode/cpp/Tree/145.cpp new file mode 100644 index 00000000..2945b125 --- /dev/null +++ b/leetcode/cpp/Tree/145.cpp @@ -0,0 +1,16 @@ +//recursive approach +class Solution { +public: + void postOrderTraversal(TreeNode* node,vector &post){ + if(!node) return; + postOrderTraversal(node->left,post); + postOrderTraversal(node->right,post); + post.push_back(node->val); + } + + vector postorderTraversal(TreeNode* root) { + vector post; + postOrderTraversal(root,post); + return post; + } +}; \ No newline at end of file diff --git a/leetcode/cpp/Tree/222.cpp b/leetcode/cpp/Tree/222.cpp new file mode 100644 index 00000000..ccae0cb3 --- /dev/null +++ b/leetcode/cpp/Tree/222.cpp @@ -0,0 +1,6 @@ +class Solution { +public: + int countNodes(TreeNode* root) { + return !root? 0: countNodes(root->left)+countNodes(root->right)+1; + } +}; \ No newline at end of file diff --git a/leetcode/cpp/Tree/230.cpp b/leetcode/cpp/Tree/230.cpp new file mode 100644 index 00000000..cc53a6ad --- /dev/null +++ b/leetcode/cpp/Tree/230.cpp @@ -0,0 +1,64 @@ +/** + * 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) {} + * }; + */ + +//method1: inorder traversal +class Solution { +public: + void inorder(TreeNode* node,vector &v){ + if(node){ + inorder(node->left,v); + v.push_back(node->val); + inorder(node->right,v); + } + } + + int kthSmallest(TreeNode* root, int k) { + vector v; + inorder(root,v); + return v[k-1]; + } +}; + +//method2: using Morris Traversal +class Solution { +public: + int kthSmallest(TreeNode* root, int k) { + TreeNode* curr=root,*pre; + int ans; + while(curr){ + if(curr->left==NULL){ + k--; + if(k==0) ans=curr->val; + curr=curr->right; + }else{ + pre=curr->left; + while(pre->right && pre->right!=curr) pre=pre->right; + if(!pre->right){ + //this indicates we have pointer to the root element from where we should continue the traversing, so for inorder we jump to this root element, + //& then traverse the right subtree from there. + //to leave the tree as we found, set right element to the NULL again & jumps to root(the current root, parent of the current subtree) & continue. + pre->right=curr; + curr=curr->left; + }else{ //connecting rightmost element of the left subtree to current element, so we know where to continue when we finish with the + //left subtree, basically to maintain inorder. + //in inorder traversal after traversing all left elements root element should be traverse and then the rest of the right subtree elements. + //so for that in morris traversal we connect rightmost element to the root(of current subtree). + pre->right=NULL; + k--; + if(k==0) ans=curr->val; + curr=curr->right; + } + } + } + return ans; + } +}; \ No newline at end of file diff --git a/leetcode/cpp/Tree/94.cpp b/leetcode/cpp/Tree/94.cpp new file mode 100644 index 00000000..7def583a --- /dev/null +++ b/leetcode/cpp/Tree/94.cpp @@ -0,0 +1,65 @@ +//recursive method: +class Solution { +public: + void traverse(TreeNode* root,vector &v){ + if(root){ + traverse(root->left,v); + v.push_back(root->val); + traverse(root->right,v); + } + } + + vector inorderTraversal(TreeNode* root) { + vector v; + traverse(root,v); + return v; + } +}; + +//stack method: +class Solution{ +public: + vector inorderTraversal(TreeNode* root){ + vector v; + stack s; + auto curr=root; + while(curr || !s.empty( )){ + while(curr){ + s.push(curr); + curr=curr->left; + } + curr=s.top(); + s.pop(); + v.push_back(curr->val); + curr=curr->right; + } + return v; + } +}; + +//Morris Traversal: +class Solution{ +public: + vector inorderTraversal(TreeNode* root){ + vector v; + auto curr=root; + while(curr){ + if(curr->left==NULL){ + v.push_back(curr->val); + curr=curr->right; + }else{ + auto pre=curr->left; + while(pre->right && pre->right!=curr) pre=pre->right; + if(!pre->right){ + pre->right=curr; + curr=curr->left; + }else{ + pre->right=NULL; + v.push_back(curr->val); + curr=curr->right; + } + } + } + return v; + } +}; \ No newline at end of file diff --git a/leetcode/cpp/backtracking/1291.cpp b/leetcode/cpp/backtracking/1291.cpp new file mode 100644 index 00000000..bd748558 --- /dev/null +++ b/leetcode/cpp/backtracking/1291.cpp @@ -0,0 +1,63 @@ +//kind of iterative solution +class Solution { +public: + int digcount; + + long long int next(long long int x,int low){ + if(x%10==9){ + digcount+=1; + return initial(low); + } + long long int power=pow(10,digcount-1); + return (x%power)*10+(x%10+1); + } + + long long int initial(int low){ + long long int digit=1; + for(int i=2;i<=digcount;i++) digit=digit*10+i; + while(digit sequentialDigits(int low, int high) { + vector v; + long long int x; + setDigcount(low); + x=initial(low); + while(x<=high){ + if(x%10==0) break; + v.push_back(x); + x=next(x,low); + } + return v; + } +}; + +//backtracking solution(kind of same as previous one) +class Solution { +public: + void backtrack(vector &v, int low, int high, int num) { + if (num == 9) return; + if (num % 10 < 9) { + num = num * 10 + (num % 10) + 1; + if (num >= low && num <= high) + v.push_back(num); + + backtrack(v, low, high, num); + } + else { + int exp = log10(num), highestDigit = num / pow(10, exp); + backtrack(v, low, high, highestDigit + 1); + } + } + vector sequentialDigits(int low, int high) { + vector v; + backtrack(v, low, high, 1); + sort(begin(v), end(v)); + return v; + } +}; \ No newline at end of file diff --git a/leetcode/cpp/bit manipulation/1342.cpp b/leetcode/cpp/bit manipulation/1342.cpp new file mode 100644 index 00000000..40ab1503 --- /dev/null +++ b/leetcode/cpp/bit manipulation/1342.cpp @@ -0,0 +1,12 @@ +class Solution { +public: + int numberOfSteps (int num) { + if(num==0 || num==1) return num; + int ans=0; + while(num){ + num&1? num-=1: num>>=1; + ans+=1; + } + return ans; + } +}; \ No newline at end of file diff --git a/leetcode/cpp/bit manipulation/136.cpp b/leetcode/cpp/bit manipulation/136.cpp new file mode 100644 index 00000000..748f7b9b --- /dev/null +++ b/leetcode/cpp/bit manipulation/136.cpp @@ -0,0 +1,7 @@ +class Solution { +public: + int singleNumber(vector& nums) { + for(int i=1;i& nums) { + int result=0; + int sum,ithbit; + for(int i=0;i<32;i++){ + sum=0; + ithbit=(1<& nums) { + int result=0,n=nums.size(); + vector v(32,0); + for(int i=0;i<32;i++){ + for(int j=0;jn/2) result|=(1<& nums) { + int elem,count=0; + for(auto num: nums){ + if(count==0) elem=num; + if(elem==num) count++; + else count--; + } + return elem; + } +}; \ No newline at end of file diff --git a/leetcode/cpp/dynamic programming/1594.cpp b/leetcode/cpp/dynamic programming/1594.cpp new file mode 100644 index 00000000..ca7360fc --- /dev/null +++ b/leetcode/cpp/dynamic programming/1594.cpp @@ -0,0 +1,48 @@ +//DP solution +class Solution { +public: + int maxProductPath(vector>& grid) { + int mod=1e9+7; + int m=grid.size(),n=grid[0].size(); + vector> mn(m,vector (n)),mx(m,vector (n)); + mn[0][0]=mx[0][0]=grid[0][0]; + for(int i=1;i> &grid,int i,int j,long long curr,long long &product){ + if(i==grid.size()-1 && j==grid[0].size()-1){ + product=max(curr,product); + return; + } + if(grid[i][j]==0){ + product=max(product,(long long)0); + return; + } + if(i+1>& grid) { + long long int mod=1e9+7; + long long product=-1; + dfs(grid,0,0,grid[0][0],product); + return product%mod; + } +}; \ No newline at end of file diff --git a/leetcode/cpp/dynamic programming/198.cpp b/leetcode/cpp/dynamic programming/198.cpp new file mode 100644 index 00000000..8150dbb2 --- /dev/null +++ b/leetcode/cpp/dynamic programming/198.cpp @@ -0,0 +1,41 @@ +//top down / memoization +class Solution { +public: + int DP(vector &nums,int i,vector &dp){ + if(i>=nums.size()) return 0; + if(dp[i]!=-1) return dp[i]; + return dp[i]=max(nums[i]+DP(nums,i+2,dp),DP(nums,i+1,dp)); + } + + int rob(vector& nums) { + vector dp(nums.size()+1,-1); + return DP(nums,0,dp); + } +}; + +//bottom up/tabulation +class Solution { +public: + int rob(vector& nums) { + if(nums.empty()) return 0; + if(nums.size()==1) return nums[0]; + vector dp(nums.size()+1); + dp[0]=nums[0]; + dp[1]=max(nums[0],nums[1]); + for(int i=2;i& nums){ + int sum1=0,sum2=0; + for(int i=0;ival); + x->next=rev; + rev=x; + head=head->next; + } + return rev; + } + + vector nextLargerNodes(ListNode* head) { + if(!head) return vector (0); + auto *rev=reversell(head); + stack s; + vector ans; + if(s.empty()){ + ans.push_back(0); + s.push(rev->val); + rev=rev->next; + } + while(rev){ + while(rev && rev->valval); + rev=rev->next; + } + while(rev && rev->val>=s.top()){ + s.pop(); + if(s.empty()){ + ans.push_back(0); + s.push(rev->val); + rev=rev->next; + } + } + } + reverse(ans.begin(),ans.end()); + return ans; + } +}; + + +//brute force approach +class Solution { +public: + vector nextLargerNodes(ListNode* head) { + auto *p1=head,*p2=head; + vector v; + while(p1){ + p2=p1->next; + while(p2 && p2->val<=p1->val) p2=p2->next; + if(!p2) v.push_back(0); + else v.push_back(p2->val); + p1=p1->next; + } + return v; + } +}; \ No newline at end of file diff --git a/leetcode/cpp/linkedlist/1171.cpp b/leetcode/cpp/linkedlist/1171.cpp new file mode 100644 index 00000000..853cfa51 --- /dev/null +++ b/leetcode/cpp/linkedlist/1171.cpp @@ -0,0 +1,37 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + ListNode* removeZeroSumSublists(ListNode* head) { + ListNode *temp,*curr=head; + int sum=0,cnt; + while(curr){ + sum+=curr->val; + if(sum==0){ + curr=curr->next; + head=curr; + continue; + } + temp=head; + cnt=0; + while(temp && temp!=curr){ + if(temp) cnt+=temp->val; + if(cnt==sum){ + temp->next=curr->next; + break; + } + if(temp) temp=temp->next; + } + curr=curr->next; + } + return head; + } +}; \ No newline at end of file diff --git a/leetcode/cpp/linkedlist/138.cpp b/leetcode/cpp/linkedlist/138.cpp new file mode 100644 index 00000000..e96f8aef --- /dev/null +++ b/leetcode/cpp/linkedlist/138.cpp @@ -0,0 +1,46 @@ +/* +// Definition for a Node. +class Node { +public: + int val; + Node* next; + Node* random; + + Node(int _val) { + val = _val; + next = NULL; + random = NULL; + } +}; +*/ + +class Solution { +public: + Node* copyRandomList(Node* head) { + if(!head) return head; + Node *orighead=head; + while(head){ + Node *temp=new Node(head->val); + temp->next=head->next; + head->next=temp; + head=temp->next; + } + head=orighead; + Node *copy=head->next; + while(head){ + copy->random=head->random? head->random->next : head->random; + copy=copy->next? copy->next->next: NULL; + head=head->next->next? head->next->next: NULL; + } + head=orighead; + copy=head->next; + Node *copyhead=copy; + while(head){ + head->next=head->next? head->next->next: NULL; + head=head->next; + copy->next=copy->next? copy->next->next: NULL; + copy=copy->next; + } + return copyhead; + } +}; \ No newline at end of file diff --git a/leetcode/cpp/linkedlist/142.cpp b/leetcode/cpp/linkedlist/142.cpp new file mode 100644 index 00000000..5dac3c32 --- /dev/null +++ b/leetcode/cpp/linkedlist/142.cpp @@ -0,0 +1,30 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode(int x) : val(x), next(NULL) {} + * }; + */ +class Solution { +public: + ListNode *detectCycle(ListNode *head) { + if(head==NULL || head->next==NULL) return NULL; + ListNode *fast,*slow; + fast=head; + slow=head; + while(fast && fast->next){ + slow=slow->next; + fast=fast->next->next; + if(slow==fast){ + fast=head; + while(fast!=slow){ + fast=fast->next; + slow=slow->next; + } + return fast; + } + } + return NULL; + } +}; \ No newline at end of file diff --git a/leetcode/cpp/linkedlist/19.cpp b/leetcode/cpp/linkedlist/19.cpp new file mode 100644 index 00000000..85bbb284 --- /dev/null +++ b/leetcode/cpp/linkedlist/19.cpp @@ -0,0 +1,18 @@ +class Solution { +public: + ListNode* removeNthFromEnd(ListNode* head, int n) { + ListNode *slowptr=head,*fastptr=head,*temp; + for(int index=0;indexnext; + } + if(fastptr==NULL) return head->next; + while(fastptr->next!=NULL){ + slowptr=slowptr->next; + fastptr=fastptr->next; + } + temp=slowptr->next; + slowptr->next=slowptr->next->next; + delete temp; + return head; + } +}; \ No newline at end of file diff --git a/leetcode/cpp/linkedlist/21.cpp b/leetcode/cpp/linkedlist/21.cpp new file mode 100644 index 00000000..409e20ba --- /dev/null +++ b/leetcode/cpp/linkedlist/21.cpp @@ -0,0 +1,38 @@ +// Merged Two Sorted Linked Lists + +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { + if(!l1 && !l2) return l1; + if(!l1 && l2) return l2; + if(!l2 && l1) return l1; + ListNode *mergedList,*head; + int x,y; + mergedList=new ListNode(0); + head=mergedList; + while(l1 || l2){ + x=l1? l1->val: INT_MAX; + y=l2? l2->val: INT_MAX; + if(l1 && x<=y){ + mergedList->next=new ListNode(x); + l1=l1->next; + } + else if(l2){ + mergedList->next=new ListNode(y); + l2=l2->next; + } + mergedList=mergedList->next; + } + return head->next; + } +}; \ No newline at end of file diff --git a/leetcode/cpp/linkedlist/23.cpp b/leetcode/cpp/linkedlist/23.cpp new file mode 100644 index 00000000..3fc2a794 --- /dev/null +++ b/leetcode/cpp/linkedlist/23.cpp @@ -0,0 +1,41 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + ListNode* merge_two(ListNode* l1,ListNode* l2){ + ListNode* res=NULL; + if(!l1) return l2; + if(!l2) return l1; + if(l1->val<=l2->val){ + res=l1; + res->next=merge_two(l1->next,l2); + }else{ + res=l2; + res->next=merge_two(l1,l2->next); + } + return res; + } + + ListNode* mergeKLists(vector& lists) { + if(lists.empty()) return NULL; + int last=lists.size()-1,i,j; + while(last!=0){ + i=0;j=last; + while(ival); + node->next=rev; + rev=node; + headref=headref->next; + count++; + } + count=count/2; + while(count){ + if(head->val!=rev->val) return false; + head=head->next; + rev=rev->next; + count--; + } + return true; + } +}; + +//two pointer solution +class Solution{ +public: + ListNode* rev(ListNode* node){ + ListNode* revnode=NULL; + while(node){ + auto *x=new ListNode(node->val); + x->next=revnode; + revnode=x; + node=node->next; + } + return revnode; + } + + bool isPalindrome(ListNode *head){ + auto *fast=head,*slow=head; + while(fast && fast->next){ + slow=slow->next; + fast=fast->next->next; + } + fast=head; + slow=rev(slow); + while(slow){ + if(fast->val!=slow->val) return false; + slow=slow->next; + fast=fast->next; + } + return true; + } +}; \ No newline at end of file diff --git a/leetcode/cpp/linkedlist/24.cpp b/leetcode/cpp/linkedlist/24.cpp new file mode 100644 index 00000000..c82e1227 --- /dev/null +++ b/leetcode/cpp/linkedlist/24.cpp @@ -0,0 +1,27 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + ListNode* swapPairs(ListNode* head) { + if(!head || !head->next) return head; + ListNode *one=head,*two=head->next,*prev=NULL; + head=head->next; + while(one && two){ + one->next=two->next; + two->next=one; + if(prev) prev->next=two; + prev=one; + one=one->next; + if(one) two=one->next; + } + return head; + } +}; \ No newline at end of file diff --git a/leetcode/cpp/linkedlist/25.cpp b/leetcode/cpp/linkedlist/25.cpp new file mode 100644 index 00000000..ec30c6a7 --- /dev/null +++ b/leetcode/cpp/linkedlist/25.cpp @@ -0,0 +1,35 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + ListNode* reverseKGroup(ListNode* head, int k) { + auto *initial=head; + ListNode *prevTail=NULL; + while(head){ + ListNode *curr=NULL,*tail=NULL; + int count=k; + while(count){ + if(!head) return initial; + auto *x=new ListNode(head->val); + x->next=curr; + curr=x; + head=head->next; + if(!tail) tail=curr; + count--; + } + if(!prevTail) initial=curr; + else prevTail->next=curr; + prevTail=tail; + tail->next=head; + } + return initial; + } +}; \ No newline at end of file diff --git a/leetcode/cpp/linkedlist/328.cpp b/leetcode/cpp/linkedlist/328.cpp new file mode 100644 index 00000000..13e5a4b7 --- /dev/null +++ b/leetcode/cpp/linkedlist/328.cpp @@ -0,0 +1,30 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + ListNode* oddEvenList(ListNode* head) { + if(!head || !head->next) return head; + ListNode *even=head->next,*odd=head,*temp; + while(even){ + temp=odd->next; + odd->next=even->next; + if(!odd->next){ + odd->next=temp; + return handler; + } + odd=odd->next; + even->next=odd->next; + odd->next=temp; + even=even->next; + } + return head; + } +}; \ No newline at end of file diff --git a/leetcode/cpp/linkedlist/445.cpp b/leetcode/cpp/linkedlist/445.cpp new file mode 100644 index 00000000..213cf168 --- /dev/null +++ b/leetcode/cpp/linkedlist/445.cpp @@ -0,0 +1,46 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { + stack l1Stack,l2Stack; + ListNode *list=NULL; + int x,y,carry=0; + while(l1 || l2){ + if(l1){ + l1Stack.push(l1->val); + l1=l1->next; + } + if(l2){ + l2Stack.push(l2->val); + l2=l2->next; + } + } + while(!l1Stack.empty() || !l2Stack.empty() || carry){ + x=0; + y=0; + if(!l1Stack.empty()){ + x=l1Stack.top(); + l1Stack.pop(); + } + if(!l2Stack.empty()){ + y=l2Stack.top(); + l2Stack.pop(); + } + x=x+y+carry; + carry=(int)x/10; + auto *temp=new ListNode(x%10); + temp->next=list; + list=temp; + } + return list; + } +}; \ No newline at end of file diff --git a/leetcode/cpp/linkedlist/61.cpp b/leetcode/cpp/linkedlist/61.cpp new file mode 100644 index 00000000..aeecf49b --- /dev/null +++ b/leetcode/cpp/linkedlist/61.cpp @@ -0,0 +1,39 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + ListNode* rotateRight(ListNode* head, int k) { + if(!k || !head) return head; + ListNode* temp,*resultList; + int size=0; + temp=head; + while(temp!=NULL){ + size+=1; + temp=temp->next; + } + temp=head; + k%=size; + if(!k) return head; + size-=(k+1); + while(size){ + temp=temp->next; + size--; + } + resultList=temp->next; + temp->next=NULL; + temp=resultList; + while(temp->next!=NULL){ + temp=temp->next; + } + temp->next=head; + return resultList; + } +}; \ No newline at end of file diff --git a/leetcode/cpp/linkedlist/707.cpp b/leetcode/cpp/linkedlist/707.cpp new file mode 100644 index 00000000..54410497 --- /dev/null +++ b/leetcode/cpp/linkedlist/707.cpp @@ -0,0 +1,91 @@ +struct Node{ + int val; + Node *next; + Node(int x):val(x),next(NULL){}; +}; + +class MyLinkedList { +public: + Node *head; + int size; + /** Initialize your data structure here. */ + MyLinkedList() { + this->head=NULL; + this->size=0; + } + + /** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */ + int get(int index) { + if(index>=this->size) return -1; + auto *headref=this->head; + while(index){ + headref=headref->next; + index--; + } + return headref->val; + } + + /** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */ + void addAtHead(int val) { + auto *temp=new Node(val); + temp->next=this->head; + this->head=temp; + this->size+=1; + } + + /** Append a node of value val to the last element of the linked list. */ + void addAtTail(int val) { + auto *headref=this->head; + while(headref->next) headref=headref->next; + headref->next=new Node(val); + this->size+=1; + } + + /** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */ + void addAtIndex(int index, int val) { + if(index>this->size) return; + if(!index){ + this->addAtHead(val); + return; + } + index-=1; + auto *headref=this->head; + while(index){ + headref=headref->next; + index--; + } + auto *temp=new Node(val); + temp->next=headref->next; + headref->next=temp; + this->size+=1; + } + + /** Delete the index-th node in the linked list, if the index is valid. */ + void deleteAtIndex(int index) { + if(index>=this->size) return; + this->size-=1; + if(!index){ + this->head=this->head->next; + return; + } + index-=1; + auto *headref=this->head; + while(index){ + headref=headref->next; + index--; + } + auto *temp=headref->next; + headref->next=headref->next->next; + delete temp; + } +}; + +/** + * Your MyLinkedList object will be instantiated and called as such: + * MyLinkedList* obj = new MyLinkedList(); + * int param_1 = obj->get(index); + * obj->addAtHead(val); + * obj->addAtTail(val); + * obj->addAtIndex(index,val); + * obj->deleteAtIndex(index); + */ \ No newline at end of file diff --git a/leetcode/cpp/linkedlist/725.cpp b/leetcode/cpp/linkedlist/725.cpp new file mode 100644 index 00000000..d36eed02 --- /dev/null +++ b/leetcode/cpp/linkedlist/725.cpp @@ -0,0 +1,50 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + ListNode* getList(ListNode* head,int size){ + if(!size) return NULL; + size-=1; + ListNode* headref=head; + while(size){ + headref=headref->next; + size--; + } + head=headref->next; + headref->next=NULL; + return head; + } + + vector splitListToParts(ListNode* root, int k) { + int size=0; + auto *head=root; + vector v; + while(head){ + head=head->next; + size+=1; + } + int rem=size%k,len=size/k; + k-=rem; + while(rem){ + auto *prev=root; + root=getList(root,len+1); + v.push_back(prev); + rem--; + } + while(k){ + auto *prev=root; + root=getList(root,len); + v.push_back(prev); + k--; + } + return v; + } +}; \ No newline at end of file diff --git a/leetcode/cpp/linkedlist/817.cpp b/leetcode/cpp/linkedlist/817.cpp new file mode 100644 index 00000000..7f094dcb --- /dev/null +++ b/leetcode/cpp/linkedlist/817.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + int numComponents(ListNode* head, vector& G) { + unordered_set s; + int ans=0,count=0; + for(auto i:G){ + s.insert(i); + } + while(head){ + if(s.find(head->val)!=s.end()) count++; + else{ + if(count) ans++; + count=0; + } + head=head->next; + } + if(count) ans++; + return ans; + } +}; \ No newline at end of file diff --git a/leetcode/cpp/linkedlist/82.cpp b/leetcode/cpp/linkedlist/82.cpp new file mode 100644 index 00000000..7fe0e23f --- /dev/null +++ b/leetcode/cpp/linkedlist/82.cpp @@ -0,0 +1,32 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + ListNode* deleteDuplicates(ListNode* head) { + ListNode *l1; + l1=new ListNode(0); + auto l2=l1; + while(head){ + if(head->next && head->val==head->next->val){ + while(head->next && head->val==head->next->val){ + head=head->next; + } + head=head->next; + }else{ + l1->next=head; + l1=l1->next; + head=head->next; + } + } + l1->next=NULL; + return l2->next; + } +}; \ No newline at end of file diff --git a/leetcode/cpp/linkedlist/83.cpp b/leetcode/cpp/linkedlist/83.cpp new file mode 100644 index 00000000..700bd14c --- /dev/null +++ b/leetcode/cpp/linkedlist/83.cpp @@ -0,0 +1,24 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + ListNode* deleteDuplicates(ListNode* head) { + ListNode* curr = head; + while(curr!=NULL && curr->next!=NULL) + { + if(curr->next->val==curr->val) + curr->next=curr->next->next; + else + curr=curr->next; + } + return head; + } +}; \ No newline at end of file diff --git a/leetcode/cpp/linkedlist/86.cpp b/leetcode/cpp/linkedlist/86.cpp new file mode 100644 index 00000000..f1a83d45 --- /dev/null +++ b/leetcode/cpp/linkedlist/86.cpp @@ -0,0 +1,33 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + ListNode* partition(ListNode* head, int x) { + if(!head || !head->next) return head; + auto l1=new ListNode(); + auto l2=new ListNode(); + auto l1head=l1; + auto l2head=l2; + while(head){ + if(head->valnext=head; + l1=l1->next; + }else{ + l2->next=head; + l2=l2->next; + } + head=head->next; + } + l2->next=NULL; + l1->next=l2head->next; + return l1head->next; + } +}; \ No newline at end of file diff --git a/leetcode/cpp/linkedlist/876.cpp b/leetcode/cpp/linkedlist/876.cpp new file mode 100644 index 00000000..ae50f43a --- /dev/null +++ b/leetcode/cpp/linkedlist/876.cpp @@ -0,0 +1,32 @@ +//method1 +class Solution { +public: + ListNode* middleNode(ListNode* head) { + int size=0; + auto *headref=head; + while(headref){ + headref=headref->next; + size++; + } + size/=2; + while(size){ + head=head->next; + size--; + } + return head; + } +}; + +//method2 +class Solution { +public: + ListNode* middleNode(ListNode* head) { + auto *slow=head,*fast=head; + while(true){ + if(!fast->next) return slow; + if(!fast->next->next) return slow->next; + slow=slow->next; + fast=fast->next->next; + } + } +}; \ No newline at end of file diff --git a/leetcode/cpp/linkedlist/92.cpp b/leetcode/cpp/linkedlist/92.cpp new file mode 100644 index 00000000..550c955a --- /dev/null +++ b/leetcode/cpp/linkedlist/92.cpp @@ -0,0 +1,38 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + ListNode* reverseBetween(ListNode* head, int m, int n) { + if(!head || !head->next || m==n) return head; + auto *initial=head; + m-=1; + n-=m; + ListNode *mpos=NULL; + while(m){ + mpos=head; + head=head->next; + m--; + } + ListNode *list=NULL,*listhead=NULL; + while(n){ + auto *x=new ListNode(head->val); + if(!list) listhead=x; + x->next=list; + list=x; + head=head->next; + n--; + } + if(!mpos) initial=list; + else mpos->next=list; + listhead->next=head; + return initial; + } +}; \ No newline at end of file diff --git a/leetcode/cpp/min-max/292.cpp b/leetcode/cpp/min-max/292.cpp new file mode 100644 index 00000000..e05f99f5 --- /dev/null +++ b/leetcode/cpp/min-max/292.cpp @@ -0,0 +1,50 @@ +//method1: +class Solution { +public: + bool canWinNim(int n) { + return n%4; + } +}; + +//method2: +class Solution{ +public: + bool canWinNim(int n){ + vector dp(n+1,false); + dp[1]=dp[2]=dp[3]=true; + for(int i=4;i<=n;i++) dp[i]=!dp[i-1] || !dp[i-2] || !dp[i-3]; + return dp[n]; + } +}; + +//method3 +class Solution{ +public: + int maxMin(int n,vector& table1,vector& table2){ + if(table1[n]!=0) return table1[n]; + vector tmp; + for(int i=1;i<4;i++){ + if(n-i==0) return 1; + else if(n-i>0) tmp.push_back(minMax(n-i,table1,table2)); + } + table1[n]=*max_element(tmp.begin(),tmp.end()); + return table1[n]; + } + int minMax(int n,vector& table1,vector& table2){ + if(table2[n]!=0) return table2[n]; + vector tmp; + for(int i=1;i<4;i++){ + if(n-i==0) return -1; + else if(n-i>0) tmp.push_back(maxMin(n-i,table1,table2)); + } + table2[n]=*min_element(tmp.begin(),tmp.end()); + return table2[n]; + } + + bool canWinNim(int n){ + if(n>=134882061) + return n%4; + vector table1(n+1,0),table2(n+1,0); + return maxMin(n,table1,table2)==1? true: false; + } +}; \ No newline at end of file diff --git a/leetcode/cpp/sort/1370.cpp b/leetcode/cpp/sort/1370.cpp new file mode 100644 index 00000000..c87a0f90 --- /dev/null +++ b/leetcode/cpp/sort/1370.cpp @@ -0,0 +1,29 @@ +class Solution { +public: + string sortString(string s) { + vector freq(26,0); + string res; + int flag=1; + for(auto c: s) freq[c-'a']+=1; + while(flag){ + flag=0; + for(int i=0;i<26;i++){ + if(freq[i]){ + flag=1; + res+=i+'a'; + freq[i]-=1; + } + } + if(!flag) break; + flag=0; + for(int i=25;i>=0;i--){ + if(freq[i]){ + flag=1; + res+=i+'a'; + freq[i]-=1; + } + } + } + return res; + } +}; \ No newline at end of file diff --git a/leetcode/cpp/sort/1387.cpp b/leetcode/cpp/sort/1387.cpp new file mode 100644 index 00000000..f2cc4050 --- /dev/null +++ b/leetcode/cpp/sort/1387.cpp @@ -0,0 +1,64 @@ +//method1: +class Solution { +public: + int get(int val, vector& dp) + { + if (val < dp.size() && dp[val] != INT_MAX) + return dp[val]; + + auto next = val % 2 ? 3 * val + 1 : val / 2; + auto result = get(next, dp) + 1; + + if (val < dp.size()) + dp[val] = result; + + return result; + } + + + int getKth(int lo, int hi, int k) + { + vector dp(hi * 3 + 2, INT_MAX); + dp[1] = 0; + + vector> vals; + + for (int i = lo; i <= hi; ++i) + vals.push_back({get(i, dp), i}); + + nth_element(vals.begin(), next(vals.begin(), k - 1), vals.end()); + return vals[k - 1].second; + } +}; + +//method2: + +class Solution { +public: + int power(int x,map &powers){ + if(x==1) return 1; + if(powers.find(x)!=powers.end()) return powers[x]; + if(x%2==0){ + powers[x]=1+power(x/2,powers); + }else{ + powers[x]=1+power(3*x+1,powers); + } + return powers[x]; + } + + int getKth(int lo, int hi, int k) { + map powers; + multimap powerArray; + for(int i=lo;i<=hi;i++){ + powerArray.insert(pair (power(i,powers)-1,i)); + } + for(auto &it: powerArray){ + k--; + if(!k){ + ans=it.second; + break; + } + } + return ans; + } +}; \ No newline at end of file diff --git a/leetcode/cpp/sort/242.cpp b/leetcode/cpp/sort/242.cpp new file mode 100644 index 00000000..3f242a0a --- /dev/null +++ b/leetcode/cpp/sort/242.cpp @@ -0,0 +1,107 @@ +class Solution { +public: + string selectionSort(string s){ + int index; + for(int i=0;i=0 && keys[j+1]){ + auto temp=s[j+1]; + s[j+1]=s[j]; + s[j]=temp; + } + } + } + return s; + } + + void merge(string &s,int l,int m,int r){ + int n1=m-l+1,n2=r-m,i,j,k=l; + string s1,s2; + for(i=0;i Hash(26, 0); + + for(int i=0;i>& intervals,int l,int mid,int r){ + int n1=mid-l+1,n2=r-mid,i,j,k=l; + vector> v1,v2; + for(i=0;i>& intervals,int l,int r){ + if(l> merge(vector>& intervals) { + int i=0,curr,j,size=intervals.size(); + vector> v; + vector temp(2,0); + mergeSort(intervals,0,size-1); + while(i=*intervals[j].begin()){ + if(*intervals[j].rbegin()>maxend) maxend=*intervals[j].rbegin(); + curr+=1; + j+=1; + } + temp[0]=*intervals[i].begin(); + temp[1]=maxend; + v.push_back(temp); + i=j; + } + return v; + } +}; + +//method 2: faster than method 1 +class Solution { +public: + vector> merge(vector>& intervals) { + map mp; + for (auto &i : intervals) { + mp[i[0] << 1]++; + mp[(i[1] << 1) + 1]--; + } + int overlaps = 0, start; + vector> res; + for (auto &it : mp) { + if (!overlaps && it.second > 0) start = it.first >> 1; + overlaps += it.second; + if (!overlaps && it.second < 0) res.push_back({start, (it.first - 1) >> 1}); + } + return res; + } +}; \ No newline at end of file diff --git a/leetcode/cpp/string/1576.cpp b/leetcode/cpp/string/1576.cpp new file mode 100644 index 00000000..9e2645d8 --- /dev/null +++ b/leetcode/cpp/string/1576.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + string modifyString(string s) { + string alphabets="abcdefghijklmnopqrstuvwxyz"; + char chr; + for(int i=0;i0 && i0){ + while(chr==s[i-1]){ + chr=alphabets[rand()%26]; + } + }else{ + while(chr==s[i+1]){ + chr=alphabets[rand()%26]; + } + } + s[i]=chr; + } + } + return s; + } +}; \ No newline at end of file diff --git a/leetcode/cpp/string/1592.cpp b/leetcode/cpp/string/1592.cpp new file mode 100644 index 00000000..5adc63e3 --- /dev/null +++ b/leetcode/cpp/string/1592.cpp @@ -0,0 +1,46 @@ +class Solution { +public: + string reorderSpaces(string text) { + string s="",word; + vector words; + int spaces=0,i=0; + while(i longestsofar; + while(jmaxsize) maxsize=longestsofar.size(); + }else{ + longestsofar.erase(s[i]); + i++; + } + } + return maxsize; + } +}; \ No newline at end of file diff --git a/leetcode/cpp/string/58.cpp b/leetcode/cpp/string/58.cpp new file mode 100644 index 00000000..f8433863 --- /dev/null +++ b/leetcode/cpp/string/58.cpp @@ -0,0 +1,10 @@ +class Solution { +public: + int lengthOfLastWord(string s) { + int i=s.size()-1; + while(i>=0 && s[i]==' ') i--; + int j=i; + while(j>=0 && s[j]!=' ') j--; + return i-j; + } +}; \ No newline at end of file diff --git a/leetcode/cpp/string/6.cpp b/leetcode/cpp/string/6.cpp new file mode 100644 index 00000000..23bcf010 --- /dev/null +++ b/leetcode/cpp/string/6.cpp @@ -0,0 +1,49 @@ +//naive solution:brute force +class Solution { +public: + string convert(string s, int n) { + bool flag=true; + if(n==1) return s; + int i=0,j=0; + vector> list(n,vector(s.size(),'0')); + for(auto elem: s){ + list[i][j]=elem; + if(flag) i++; + else{ + i--; + j++; + } + if(i==0 || i==n-1) flag=!flag; + } + string ans=""; + for(int i=0;i=s.size() || numRows<=1) return s; + string apt; + int tie1=(numRows-1)*2,n=s.size(),tie2; + for(int i=0;i0 && i