diff --git a/Day-21/q3: Unique Paths/Shubh_Krishna_solution.cpp b/Day-21/q3: Unique Paths/Shubh_Krishna_solution.cpp new file mode 100644 index 00000000..a28d8e55 --- /dev/null +++ b/Day-21/q3: Unique Paths/Shubh_Krishna_solution.cpp @@ -0,0 +1,20 @@ +int uniquePaths(int m, int n) { + vector> dp(m, vector(n, -1)); + return solve(m-1,n-1,dp); +} + +int solve(int m,int n, vector> &dp){ + if(m==0 && n==0){ + return 1; + } + if(m<0 || n<0){ + return 0; + } + if(dp[m][n]!=-1){ + return dp[m][n]; + } + + int up=solve(m-1,n,dp); + int left=solve(m,n-1,dp); + return dp[m][n]=up+left; +} diff --git a/Day-22/q3: Sort List/Shubh_Krishna_solution.cpp b/Day-22/q3: Sort List/Shubh_Krishna_solution.cpp new file mode 100644 index 00000000..23c796c0 --- /dev/null +++ b/Day-22/q3: Sort List/Shubh_Krishna_solution.cpp @@ -0,0 +1,36 @@ +ListNode *mergeList(ListNode *l1, ListNode *l2){ + ListNode *prev=new ListNode(-1), *curr=prev; + while(l1 and l2){ + if(l1->val<=l2->val){ + curr->next=l1; + l1=l1->next; + }else{ + curr->next=l2; + l2=l2->next; + } + curr=curr->next; + } + if(l1){ + curr->next=l1; + l1=l1->next; + } + if(l2){ + curr->next=l2; + l2=l2->next; + } +return prev->next; +} + +ListNode* sortList(ListNode* head){ + if(!head or !head->next) return head; + ListNode *slow=head, *fast=head, *temp=NULL; + while(fast and fast->next){ + temp=slow; + slow=slow->next; + fast=fast->next->next; + } + temp->next=NULL; + + ListNode *point1=sortList(head), *point2=sortList(slow); +return mergeList(point1,point2); +} diff --git a/Day-23/q1: Minimum falling path sum/Shubh_Krishna_solution.cpp b/Day-23/q1: Minimum falling path sum/Shubh_Krishna_solution.cpp new file mode 100644 index 00000000..371509ce --- /dev/null +++ b/Day-23/q1: Minimum falling path sum/Shubh_Krishna_solution.cpp @@ -0,0 +1,33 @@ + int minFallingPathSum(vector>& matrix) { + int n=matrix.size(); + int m=matrix[0].size(); + vector>dp(n,vector(m,0)); + + for(int j=0;j=0) + ld=matrix[i][j] + dp[i-1][j-1]; + if(j+1 searchRange(vector& nums, int target) { + int n = nums.size() - 1; + int low = 0; + int high = n; + int first = -1; + int last = -1; + while(high >= low){ + int mid = low + (high - low) / 2; + + if(nums[mid] == target){ + if(mid == 0 || nums[mid - 1] != target){ + first = last = mid; + break; + }else{ + high = mid - 1; + } + } + else if(nums[mid] > target){ + high = mid-1; + }else{ + low = mid+1; + } + } + + high = n; + + while(high >= low){ + int mid = low + (high - low) / 2; + + if(nums[mid] == target){ + if(mid == n || nums[mid + 1] != target){ + last = mid; + break; + }else{ + low = mid + 1; + } + } + else if(nums[mid] > target){ + high = mid-1; + }else{ + low = mid+1; + } + } + return {first, last}; + } +}; diff --git a/Day-27/q1: Maximum Length of a Concatenated String with Unique Characters/Shubh_Krishna_solution.cpp b/Day-27/q1: Maximum Length of a Concatenated String with Unique Characters/Shubh_Krishna_solution.cpp new file mode 100644 index 00000000..0661f04d --- /dev/null +++ b/Day-27/q1: Maximum Length of a Concatenated String with Unique Characters/Shubh_Krishna_solution.cpp @@ -0,0 +1,24 @@ +int maxLength(vector& arr) { + vector dp = {0}; + int res = 0; + + for (const string& s : arr) { + int a = 0, dup = 0; + for (char c : s) { + dup |= a & (1 << (c - 'a')); + a |= 1 << (c - 'a'); + } + + if (dup > 0) + continue; + + for (int i = dp.size() - 1; i >= 0; i--) { + if ((dp[i] & a) > 0) + continue; + dp.push_back(dp[i] | a); + res = max(res, __builtin_popcount(dp[i] | a)); + } + } + + return res; +}