From 49eb9b23a1186ea1c832cf9b5274926b6b20f3f8 Mon Sep 17 00:00:00 2001 From: Shubh Krishna <135266175+Shubh-Krishna@users.noreply.github.com> Date: Sat, 27 Jan 2024 18:06:12 +0530 Subject: [PATCH 01/25] Create Shubh_Krishna_solution.cpp --- .../Shubh_Krishna_solution.cpp | 1 + 1 file changed, 1 insertion(+) create mode 100644 Day-26/q3: Find first and last position of element in sorted array/Shubh_Krishna_solution.cpp diff --git a/Day-26/q3: Find first and last position of element in sorted array/Shubh_Krishna_solution.cpp b/Day-26/q3: Find first and last position of element in sorted array/Shubh_Krishna_solution.cpp new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/Day-26/q3: Find first and last position of element in sorted array/Shubh_Krishna_solution.cpp @@ -0,0 +1 @@ + From 31f636ea7b0f080c19801a7e44d745ad2b5f94df Mon Sep 17 00:00:00 2001 From: Shubh Krishna <135266175+Shubh-Krishna@users.noreply.github.com> Date: Sat, 27 Jan 2024 18:06:26 +0530 Subject: [PATCH 02/25] Update Shubh_Krishna_solution.cpp --- .../Shubh_Krishna_solution.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Day-26/q3: Find first and last position of element in sorted array/Shubh_Krishna_solution.cpp b/Day-26/q3: Find first and last position of element in sorted array/Shubh_Krishna_solution.cpp index 8b137891..e4f2e66c 100644 --- a/Day-26/q3: Find first and last position of element in sorted array/Shubh_Krishna_solution.cpp +++ b/Day-26/q3: Find first and last position of element in sorted array/Shubh_Krishna_solution.cpp @@ -1 +1,9 @@ - +class Solution { +public: + vector searchRange(vector& nums, int target) { + int l = lower_bound(nums.begin(), nums.end(), target) - nums.begin(); + int r = lower_bound(nums.begin(), nums.end(), target + 1) - nums.begin(); + if (l == r) return {-1, -1}; + return {l, r - 1}; + } +}; From 64f1cad20ab98011d564ffc7442dac325325129e Mon Sep 17 00:00:00 2001 From: Shubh Krishna <135266175+Shubh-Krishna@users.noreply.github.com> Date: Sat, 27 Jan 2024 18:06:38 +0530 Subject: [PATCH 03/25] Update Shubh_Krishna_solution.cpp --- .../Shubh_Krishna_solution.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/Day-26/q3: Find first and last position of element in sorted array/Shubh_Krishna_solution.cpp b/Day-26/q3: Find first and last position of element in sorted array/Shubh_Krishna_solution.cpp index e4f2e66c..8b06b013 100644 --- a/Day-26/q3: Find first and last position of element in sorted array/Shubh_Krishna_solution.cpp +++ b/Day-26/q3: Find first and last position of element in sorted array/Shubh_Krishna_solution.cpp @@ -6,4 +6,3 @@ class Solution { if (l == r) return {-1, -1}; return {l, r - 1}; } -}; From 738aef069dd6d86db558de47b08c3eede7dc3c2d Mon Sep 17 00:00:00 2001 From: Shubh Krishna <135266175+Shubh-Krishna@users.noreply.github.com> Date: Sat, 27 Jan 2024 18:06:49 +0530 Subject: [PATCH 04/25] Update Shubh_Krishna_solution.cpp --- .../Shubh_Krishna_solution.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Day-26/q3: Find first and last position of element in sorted array/Shubh_Krishna_solution.cpp b/Day-26/q3: Find first and last position of element in sorted array/Shubh_Krishna_solution.cpp index 8b06b013..fa373c5b 100644 --- a/Day-26/q3: Find first and last position of element in sorted array/Shubh_Krishna_solution.cpp +++ b/Day-26/q3: Find first and last position of element in sorted array/Shubh_Krishna_solution.cpp @@ -1,5 +1,4 @@ -class Solution { -public: + vector searchRange(vector& nums, int target) { int l = lower_bound(nums.begin(), nums.end(), target) - nums.begin(); int r = lower_bound(nums.begin(), nums.end(), target + 1) - nums.begin(); From 2fd87bab6e3976d9f631782023d8e5dcff0387cd Mon Sep 17 00:00:00 2001 From: Shubh Krishna <135266175+Shubh-Krishna@users.noreply.github.com> Date: Sat, 27 Jan 2024 18:07:09 +0530 Subject: [PATCH 05/25] Update Shubh_Krishna_solution.cpp --- .../Shubh_Krishna_solution.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Day-26/q3: Find first and last position of element in sorted array/Shubh_Krishna_solution.cpp b/Day-26/q3: Find first and last position of element in sorted array/Shubh_Krishna_solution.cpp index fa373c5b..596b2d37 100644 --- a/Day-26/q3: Find first and last position of element in sorted array/Shubh_Krishna_solution.cpp +++ b/Day-26/q3: Find first and last position of element in sorted array/Shubh_Krishna_solution.cpp @@ -1,7 +1,6 @@ - - vector searchRange(vector& nums, int target) { - int l = lower_bound(nums.begin(), nums.end(), target) - nums.begin(); - int r = lower_bound(nums.begin(), nums.end(), target + 1) - nums.begin(); +vector searchRange(vector& nums, int target) { + int l = lower_bound(nums.begin(), nums.end(), target) - nums.begin(); + int r = lower_bound(nums.begin(), nums.end(), target + 1) - nums.begin(); if (l == r) return {-1, -1}; return {l, r - 1}; } From 4d5139af9110edf9c52a1b87a658de27413bf29f Mon Sep 17 00:00:00 2001 From: Shubh Krishna <135266175+Shubh-Krishna@users.noreply.github.com> Date: Sat, 27 Jan 2024 18:07:27 +0530 Subject: [PATCH 06/25] Update Shubh_Krishna_solution.cpp --- .../Shubh_Krishna_solution.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Day-26/q3: Find first and last position of element in sorted array/Shubh_Krishna_solution.cpp b/Day-26/q3: Find first and last position of element in sorted array/Shubh_Krishna_solution.cpp index 596b2d37..ba9153ed 100644 --- a/Day-26/q3: Find first and last position of element in sorted array/Shubh_Krishna_solution.cpp +++ b/Day-26/q3: Find first and last position of element in sorted array/Shubh_Krishna_solution.cpp @@ -1,6 +1,6 @@ vector searchRange(vector& nums, int target) { int l = lower_bound(nums.begin(), nums.end(), target) - nums.begin(); int r = lower_bound(nums.begin(), nums.end(), target + 1) - nums.begin(); - if (l == r) return {-1, -1}; - return {l, r - 1}; - } + if (l == r) return {-1, -1}; + return {l, r - 1}; +} From 1eb34948906377cc42c56f882e5fcfc4f3c8937d Mon Sep 17 00:00:00 2001 From: Shubh Krishna <135266175+Shubh-Krishna@users.noreply.github.com> Date: Sat, 27 Jan 2024 18:09:19 +0530 Subject: [PATCH 07/25] Update Shubh_Krishna_solution.cpp --- .../Shubh_Krishna_solution.cpp | 57 +++++++++++++++++-- 1 file changed, 51 insertions(+), 6 deletions(-) diff --git a/Day-26/q3: Find first and last position of element in sorted array/Shubh_Krishna_solution.cpp b/Day-26/q3: Find first and last position of element in sorted array/Shubh_Krishna_solution.cpp index ba9153ed..ae064212 100644 --- a/Day-26/q3: Find first and last position of element in sorted array/Shubh_Krishna_solution.cpp +++ b/Day-26/q3: Find first and last position of element in sorted array/Shubh_Krishna_solution.cpp @@ -1,6 +1,51 @@ -vector searchRange(vector& nums, int target) { - int l = lower_bound(nums.begin(), nums.end(), target) - nums.begin(); - int r = lower_bound(nums.begin(), nums.end(), target + 1) - nums.begin(); - if (l == r) return {-1, -1}; - return {l, r - 1}; -} +class Solution { +public: + vector searchRange(vector& nums, int target) { + vector ans; + int start =0 ; + int n = + int end = nums.size() -1; + int flag = 0; + while(end>=start){ + int mid = (start+end)/2; + + if(nums[mid]== target){ + if(nums[mid-1]!= target || mid ==0){ + ans.push_back(mid); + flag = 1; + break; + }else{ + end = mid-1; + } + }else if(nums[mid]>target){ + end = mid-1; + }else{ + start = mid+1; + } + } + if(flag == 0){ + ans.push_back(-1); + } + flag =0; + while(end>=start){ + int mid = (start+end)/2; + + if(nums[mid]== target){ + if(nums[mid+1]!= target || mid ==(nums.size()-1)){ + ans.push_back(mid); + return ans; + }else{ + start = mid+1; + } + }else if(nums[mid]>target){ + end = mid-1; + }else{ + start = mid+1; + } + } + if(flag == 0){ + ans.push_back(-1); + } + return ans; + } +}; From 4d978d6b639d3b88d64cd534467fff18463f3c36 Mon Sep 17 00:00:00 2001 From: Shubh Krishna <135266175+Shubh-Krishna@users.noreply.github.com> Date: Sat, 27 Jan 2024 18:12:17 +0530 Subject: [PATCH 08/25] Update Shubh_Krishna_solution.cpp --- .../Shubh_Krishna_solution.cpp | 67 +++++++++---------- 1 file changed, 32 insertions(+), 35 deletions(-) diff --git a/Day-26/q3: Find first and last position of element in sorted array/Shubh_Krishna_solution.cpp b/Day-26/q3: Find first and last position of element in sorted array/Shubh_Krishna_solution.cpp index ae064212..dbc5a46e 100644 --- a/Day-26/q3: Find first and last position of element in sorted array/Shubh_Krishna_solution.cpp +++ b/Day-26/q3: Find first and last position of element in sorted array/Shubh_Krishna_solution.cpp @@ -1,51 +1,48 @@ class Solution { public: vector searchRange(vector& nums, int target) { - vector ans; - int start =0 ; - int n = - int end = nums.size() -1; - int flag = 0; - while(end>=start){ - int mid = (start+end)/2; + 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(nums[mid-1]!= target || mid ==0){ - ans.push_back(mid); - flag = 1; + if(nums[mid] == target){ + if(mid == 0 || nums[mid - 1] != target){ + first = last = mid; break; }else{ - end = mid-1; - } - }else if(nums[mid]>target){ - end = mid-1; + high = mid - 1; + } + } + else if(nums[mid] > target){ + high = mid-1; }else{ - start = mid+1; + low = mid+1; } } - if(flag == 0){ - ans.push_back(-1); - } - flag =0; - while(end>=start){ - int mid = (start+end)/2; - if(nums[mid]== target){ - if(nums[mid+1]!= target || mid ==(nums.size()-1)){ - ans.push_back(mid); - return ans; + 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{ - start = mid+1; + low = mid + 1; } - }else if(nums[mid]>target){ - end = mid-1; + } + else if(nums[mid] > target){ + high = mid-1; }else{ - start = mid+1; + low = mid+1; } } - if(flag == 0){ - ans.push_back(-1); - } - return ans; + return {first, last}; } -}; +}; From c57f62eb3d5539bcf073604d9dbd30148116f06f Mon Sep 17 00:00:00 2001 From: Shubh Krishna <135266175+Shubh-Krishna@users.noreply.github.com> Date: Sat, 27 Jan 2024 18:17:18 +0530 Subject: [PATCH 09/25] Create Shubh_Krishna_solution.cpp --- .../Shubh_Krishna_solution.cpp | 1 + 1 file changed, 1 insertion(+) create mode 100644 Day-27/q1: Maximum Length of a Concatenated String with Unique Characters/Shubh_Krishna_solution.cpp 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..8b137891 --- /dev/null +++ b/Day-27/q1: Maximum Length of a Concatenated String with Unique Characters/Shubh_Krishna_solution.cpp @@ -0,0 +1 @@ + From 1868cbb375c59fa3c703b9847ea8b65d32b2a31f Mon Sep 17 00:00:00 2001 From: Shubh Krishna <135266175+Shubh-Krishna@users.noreply.github.com> Date: Sat, 27 Jan 2024 18:17:47 +0530 Subject: [PATCH 10/25] Update Shubh_Krishna_solution.cpp --- .../Shubh_Krishna_solution.cpp | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) 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 index 8b137891..13648584 100644 --- 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 @@ -1 +1,27 @@ - +class Solution { +public: + 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; + } +}; From 460819f0067b557b337cf3a59230c629dc3622b3 Mon Sep 17 00:00:00 2001 From: Shubh Krishna <135266175+Shubh-Krishna@users.noreply.github.com> Date: Sat, 27 Jan 2024 18:18:01 +0530 Subject: [PATCH 11/25] Update Shubh_Krishna_solution.cpp --- .../Shubh_Krishna_solution.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) 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 index 13648584..701ec17b 100644 --- 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 @@ -1,5 +1,4 @@ -class Solution { -public: + int maxLength(vector& arr) { vector dp = {0}; int res = 0; From c776804672d006b94ef423e9a0556796562739ea Mon Sep 17 00:00:00 2001 From: Shubh Krishna <135266175+Shubh-Krishna@users.noreply.github.com> Date: Sat, 27 Jan 2024 18:18:57 +0530 Subject: [PATCH 12/25] Update Shubh_Krishna_solution.cpp --- .../Shubh_Krishna_solution.cpp | 40 +++++++++---------- 1 file changed, 19 insertions(+), 21 deletions(-) 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 index 701ec17b..0661f04d 100644 --- 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 @@ -1,26 +1,24 @@ - - int maxLength(vector& arr) { - vector dp = {0}; - int res = 0; +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'); - } + 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; + 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)); - } + 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; } -}; + + return res; +} From 1b14e3ac3a0d03003d4c2fdb17b0012fdf1a685a Mon Sep 17 00:00:00 2001 From: Shubh Krishna <135266175+Shubh-Krishna@users.noreply.github.com> Date: Sat, 27 Jan 2024 18:23:12 +0530 Subject: [PATCH 13/25] Create Shubh_Krishna_solution.cpp --- Day-23/q1: Minimum falling path sum/Shubh_Krishna_solution.cpp | 1 + 1 file changed, 1 insertion(+) create mode 100644 Day-23/q1: Minimum falling path sum/Shubh_Krishna_solution.cpp 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..8b137891 --- /dev/null +++ b/Day-23/q1: Minimum falling path sum/Shubh_Krishna_solution.cpp @@ -0,0 +1 @@ + From cc241ad673a8af9adac3565f73e2cd229312b221 Mon Sep 17 00:00:00 2001 From: Shubh Krishna <135266175+Shubh-Krishna@users.noreply.github.com> Date: Sat, 27 Jan 2024 18:23:31 +0530 Subject: [PATCH 14/25] Update Shubh_Krishna_solution.cpp --- .../Shubh_Krishna_solution.cpp | 39 ++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) 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 index 8b137891..af7ab2da 100644 --- a/Day-23/q1: Minimum falling path sum/Shubh_Krishna_solution.cpp +++ b/Day-23/q1: Minimum falling path sum/Shubh_Krishna_solution.cpp @@ -1 +1,38 @@ - + + class Solution { +public: + + 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 Date: Sat, 27 Jan 2024 18:24:20 +0530 Subject: [PATCH 15/25] Update Shubh_Krishna_solution.cpp --- .../Shubh_Krishna_solution.cpp | 30 ++++++++----------- 1 file changed, 13 insertions(+), 17 deletions(-) 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 index af7ab2da..4a090513 100644 --- a/Day-23/q1: Minimum falling path sum/Shubh_Krishna_solution.cpp +++ b/Day-23/q1: Minimum falling path sum/Shubh_Krishna_solution.cpp @@ -1,22 +1,18 @@ - - class Solution { -public: - - int minFallingPathSum(vector>& matrix) { - int n=matrix.size(); - int m=matrix[0].size(); - vector>dp(n,vector(m,0)); + 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) From c362131ea2470a11e30d44aa57fc791bdca9dc90 Mon Sep 17 00:00:00 2001 From: Shubh Krishna <135266175+Shubh-Krishna@users.noreply.github.com> Date: Sat, 27 Jan 2024 18:25:23 +0530 Subject: [PATCH 16/25] Update Shubh_Krishna_solution.cpp --- .../Shubh_Krishna_solution.cpp | 31 +++++++++---------- 1 file changed, 15 insertions(+), 16 deletions(-) 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 index 4a090513..371509ce 100644 --- a/Day-23/q1: Minimum falling path sum/Shubh_Krishna_solution.cpp +++ b/Day-23/q1: Minimum falling path sum/Shubh_Krishna_solution.cpp @@ -13,22 +13,21 @@ for(int j=0;j=0) - ld=matrix[i][j] + dp[i-1][j-1]; - if(j+1=0) + ld=matrix[i][j] + dp[i-1][j-1]; + if(j+1 Date: Sat, 27 Jan 2024 18:29:38 +0530 Subject: [PATCH 17/25] Create Shubh_Krishna_solution.cpp --- Day-21/q3: Unique Paths/Shubh_Krishna_solution.cpp | 1 + 1 file changed, 1 insertion(+) create mode 100644 Day-21/q3: Unique Paths/Shubh_Krishna_solution.cpp 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..8b137891 --- /dev/null +++ b/Day-21/q3: Unique Paths/Shubh_Krishna_solution.cpp @@ -0,0 +1 @@ + From 031b41144b1ad50a07dc27d9ba8a61a24f1b0cb6 Mon Sep 17 00:00:00 2001 From: Shubh Krishna <135266175+Shubh-Krishna@users.noreply.github.com> Date: Sat, 27 Jan 2024 18:29:54 +0530 Subject: [PATCH 18/25] Update Shubh_Krishna_solution.cpp --- .../Shubh_Krishna_solution.cpp | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/Day-21/q3: Unique Paths/Shubh_Krishna_solution.cpp b/Day-21/q3: Unique Paths/Shubh_Krishna_solution.cpp index 8b137891..395fbc7f 100644 --- a/Day-21/q3: Unique Paths/Shubh_Krishna_solution.cpp +++ b/Day-21/q3: Unique Paths/Shubh_Krishna_solution.cpp @@ -1 +1,23 @@ +class Solution { +public: + 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; + } +}; From 898c1a12ac4065a56cb1e2c480520a249cfb4fd5 Mon Sep 17 00:00:00 2001 From: Shubh Krishna <135266175+Shubh-Krishna@users.noreply.github.com> Date: Sat, 27 Jan 2024 18:30:08 +0530 Subject: [PATCH 19/25] Update Shubh_Krishna_solution.cpp --- Day-21/q3: Unique Paths/Shubh_Krishna_solution.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Day-21/q3: Unique Paths/Shubh_Krishna_solution.cpp b/Day-21/q3: Unique Paths/Shubh_Krishna_solution.cpp index 395fbc7f..8b77ce5b 100644 --- a/Day-21/q3: Unique Paths/Shubh_Krishna_solution.cpp +++ b/Day-21/q3: Unique Paths/Shubh_Krishna_solution.cpp @@ -1,5 +1,4 @@ -class Solution { -public: + int uniquePaths(int m, int n) { vector> dp(m, vector(n, -1)); return solve(m-1,n-1,dp); From 53926f47ec7dd5663cde67b47802f5890d3f7a33 Mon Sep 17 00:00:00 2001 From: Shubh Krishna <135266175+Shubh-Krishna@users.noreply.github.com> Date: Sat, 27 Jan 2024 18:31:08 +0530 Subject: [PATCH 20/25] Update Shubh_Krishna_solution.cpp --- .../Shubh_Krishna_solution.cpp | 36 +++++++++---------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/Day-21/q3: Unique Paths/Shubh_Krishna_solution.cpp b/Day-21/q3: Unique Paths/Shubh_Krishna_solution.cpp index 8b77ce5b..a28d8e55 100644 --- a/Day-21/q3: Unique Paths/Shubh_Krishna_solution.cpp +++ b/Day-21/q3: Unique Paths/Shubh_Krishna_solution.cpp @@ -1,22 +1,20 @@ +int uniquePaths(int m, int n) { + vector> dp(m, vector(n, -1)); + return solve(m-1,n-1,dp); +} - 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; } - - 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; + 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; +} From 4381d5b68c47d58cb8f6c0ad1d5c8baad1a4a26d Mon Sep 17 00:00:00 2001 From: Shubh Krishna <135266175+Shubh-Krishna@users.noreply.github.com> Date: Sat, 27 Jan 2024 18:35:51 +0530 Subject: [PATCH 21/25] Create Shubh_Krishna_solution.cpp --- Day-22/q3: Sort List/Shubh_Krishna_solution.cpp | 1 + 1 file changed, 1 insertion(+) create mode 100644 Day-22/q3: Sort List/Shubh_Krishna_solution.cpp 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..8b137891 --- /dev/null +++ b/Day-22/q3: Sort List/Shubh_Krishna_solution.cpp @@ -0,0 +1 @@ + From 46d0351d9b4c143289ccee42343a41104227e7b5 Mon Sep 17 00:00:00 2001 From: Shubh Krishna <135266175+Shubh-Krishna@users.noreply.github.com> Date: Sat, 27 Jan 2024 18:36:06 +0530 Subject: [PATCH 22/25] Update Shubh_Krishna_solution.cpp --- .../q3: Sort List/Shubh_Krishna_solution.cpp | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/Day-22/q3: Sort List/Shubh_Krishna_solution.cpp b/Day-22/q3: Sort List/Shubh_Krishna_solution.cpp index 8b137891..340d1b91 100644 --- a/Day-22/q3: Sort List/Shubh_Krishna_solution.cpp +++ b/Day-22/q3: Sort List/Shubh_Krishna_solution.cpp @@ -1 +1,53 @@ +/** + * 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: + /// Merge Function + 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; + } + + /// Divide and Conquer Approach (Merge Sort) + + 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); + } +}; From 619cfe3be651fd5ffe1f9edf513ee0264cb6d380 Mon Sep 17 00:00:00 2001 From: Shubh Krishna <135266175+Shubh-Krishna@users.noreply.github.com> Date: Sat, 27 Jan 2024 18:36:30 +0530 Subject: [PATCH 23/25] Update Shubh_Krishna_solution.cpp --- Day-22/q3: Sort List/Shubh_Krishna_solution.cpp | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/Day-22/q3: Sort List/Shubh_Krishna_solution.cpp b/Day-22/q3: Sort List/Shubh_Krishna_solution.cpp index 340d1b91..3e2873a8 100644 --- a/Day-22/q3: Sort List/Shubh_Krishna_solution.cpp +++ b/Day-22/q3: Sort List/Shubh_Krishna_solution.cpp @@ -1,13 +1,4 @@ -/** - * 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: /// Merge Function From 17857c44a8bbf33099bc408c6f6c922d4b4e2213 Mon Sep 17 00:00:00 2001 From: Shubh Krishna <135266175+Shubh-Krishna@users.noreply.github.com> Date: Sat, 27 Jan 2024 18:38:14 +0530 Subject: [PATCH 24/25] Update Shubh_Krishna_solution.cpp --- .../q3: Sort List/Shubh_Krishna_solution.cpp | 61 ++++++++----------- 1 file changed, 27 insertions(+), 34 deletions(-) diff --git a/Day-22/q3: Sort List/Shubh_Krishna_solution.cpp b/Day-22/q3: Sort List/Shubh_Krishna_solution.cpp index 3e2873a8..c558f69a 100644 --- a/Day-22/q3: Sort List/Shubh_Krishna_solution.cpp +++ b/Day-22/q3: Sort List/Shubh_Krishna_solution.cpp @@ -1,44 +1,37 @@ - -class Solution { -public: - /// Merge Function - - 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){ +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; - } - if(l2){ + }else{ curr->next=l2; l2=l2->next; } - return prev->next; + curr=curr->next; } - - /// Divide and Conquer Approach (Merge Sort) - - 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; + if(l1){ + curr->next=l1; + l1=l1->next; + } + if(l2){ + curr->next=l2; + l2=l2->next; } - temp->next=NULL; +return prev->next; +} - ListNode *point1=sortList(head), *point2=sortList(slow); - return mergeList(point1,point2); +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); +} }; From a3183259e667a1e70b43944bce98602b017b26cc Mon Sep 17 00:00:00 2001 From: Shubh Krishna <135266175+Shubh-Krishna@users.noreply.github.com> Date: Sat, 27 Jan 2024 18:38:32 +0530 Subject: [PATCH 25/25] Update Shubh_Krishna_solution.cpp --- Day-22/q3: Sort List/Shubh_Krishna_solution.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/Day-22/q3: Sort List/Shubh_Krishna_solution.cpp b/Day-22/q3: Sort List/Shubh_Krishna_solution.cpp index c558f69a..23c796c0 100644 --- a/Day-22/q3: Sort List/Shubh_Krishna_solution.cpp +++ b/Day-22/q3: Sort List/Shubh_Krishna_solution.cpp @@ -34,4 +34,3 @@ ListNode* sortList(ListNode* head){ ListNode *point1=sortList(head), *point2=sortList(slow); return mergeList(point1,point2); } -};