diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..9c2092c --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,28 @@ +{ + "tasks": [ + { + "type": "cppbuild", + "label": "C/C++: gcc.exe build active file", + "command": "C:\\mingw\\mingw32\\bin\\gcc.exe", + "args": [ + "-fdiagnostics-color=always", + "-g", + "${file}", + "-o", + "${fileDirname}\\${fileBasenameNoExtension}.exe" + ], + "options": { + "cwd": "${fileDirname}" + }, + "problemMatcher": [ + "$gcc" + ], + "group": { + "kind": "build", + "isDefault": true + }, + "detail": "Task generated by Debugger." + } + ], + "version": "2.0.0" +} \ No newline at end of file diff --git a/DP/lec10_1.cpp b/DP/lec10_1.cpp new file mode 100644 index 0000000..0ccfbc5 --- /dev/null +++ b/DP/lec10_1.cpp @@ -0,0 +1,49 @@ +//{ Driver Code Starts +// Initial Template for C++ + +#include +using namespace std; + + +// } Driver Code Ends +// User function Template for C++ + +class Solution { + + public: + long long fibTD(int n, vector &dp) { + if (n <= 1) { + return n; + } + + if (dp[n] != -1) { + return dp[n]; + } + + return dp[n] = (fibTD(n - 1, dp) + fibTD(n - 2, dp)) % 1000000007; + } + long long int topDown(int n) { + // code here + vector fib(n + 1, -1); + return fibTD(n, fib); + + } + long long int bottomUp(int n) { + // code here + if (n <= 1) return n; + + long long prev2 = 0, prev = 1; + + for (int i = 2; i <= n; i++) { + long long cur_i = (prev2 + prev) % 1000000007; + prev2 = prev; + prev = cur_i; + } + + return prev; + } + +}; + +//{ Driver Code Starts. + diff --git a/DP/lec10_10.cpp b/DP/lec10_10.cpp new file mode 100644 index 0000000..045607c --- /dev/null +++ b/DP/lec10_10.cpp @@ -0,0 +1,22 @@ +class Solution { + public: + int f(int ind , vector&nums,vector&dp) + { + if(ind == 0 ) return nums[ind]; + if(ind < 0) return 0 ; + if(dp[ind]!= -1) return dp[ind]; + int pick = nums[ind] + f(ind - 2,nums,dp); + + int notPick = 0 + f(ind -1 , nums,dp); + + return dp[ind] = max(pick , notPick); + } + int rob(vector& nums) { + + int n = nums.size(); + + vector dp (n,-1); + return f(n-1 , nums, dp); + + } + }; \ No newline at end of file diff --git a/DP/lec10_11.cpp b/DP/lec10_11.cpp new file mode 100644 index 0000000..d20c97a --- /dev/null +++ b/DP/lec10_11.cpp @@ -0,0 +1,23 @@ +class Solution { + public: + int rob(vector& nums) { + + int n = nums.size(); + int prev = nums[0]; + int prev2 = 0 ; + + for(int i = 1 ; i1) take+=prev2; + + int notTake = 0 +prev; + + int curri = max(take , notTake); + prev2 = prev; + prev = curri; + } + + return prev; + } + }; \ No newline at end of file diff --git a/DP/lec10_12.cpp b/DP/lec10_12.cpp new file mode 100644 index 0000000..8d16434 --- /dev/null +++ b/DP/lec10_12.cpp @@ -0,0 +1,39 @@ +class Solution { + public: + int maxValue(vector& nums) { + + int n = nums.size(); + int prev = nums[0]; + int prev2 = 0 ; + + for(int i = 1 ; i1) take+=prev2; + + int notTake = 0 +prev; + + int curri = max(take , notTake); + prev2 = prev; + prev = curri; + } + + return prev; + } + int rob(vector& nums) { + + vector temp1 , temp2; + + int n = nums.size(); + + if(n == 1) return nums[0]; + + for(int i = 0 ; i +using namespace std; + + +// } Driver Code Ends + +class Solution { + public: + + int f(int day , int last , vector> &points){ + if(day == 0 ){ + int maxi = 0 ; + for(int task = 0 ; task<3;task++){ + + if(task!=last){ + maxi = max(maxi, points[0][task]); + } + } + return maxi; + } + int maxi = 0 ; + + for(int task = 0 ;task<3;task++){ + + if(task!=last){ + int point = points[day][task]+ f(day-1, task,points); + maxi = max(maxi,point); + } + } + return maxi; + } + int maximumPoints(vector>& arr) { + // Code here + int n = arr.size(); + return f(n-1, 3 , arr); + } +}; + + +//{ Driver Code Starts. +int main() { + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + vector> arr; + for (int i = 0; i < n; ++i) { + vector temp; + for (int j = 0; j < 3; ++j) { + int x; + cin >> x; + temp.push_back(x); + } + arr.push_back(temp); + } + + Solution obj; + cout << obj.maximumPoints(arr) << endl; + cout << "~" << endl; + } + return 0; +} +// } Driver Code Ends \ No newline at end of file diff --git a/DP/lec10_14.cpp b/DP/lec10_14.cpp new file mode 100644 index 0000000..de80b0d --- /dev/null +++ b/DP/lec10_14.cpp @@ -0,0 +1,68 @@ +//{ Driver Code Starts +#include +using namespace std; + + +// } Driver Code Ends + +class Solution { + public: + + int f(int day , int last , vector> &points ,vector>&dp){ + if(day == 0 ){ + int maxi = 0 ; + for(int task = 0 ; task<3;task++){ + + if(task!=last){ + maxi = max(maxi, points[0][task]); + } + } + return maxi; + } + if(dp[day][last]!=-1) return dp[day][last]; + int maxi = 0 ; + + for(int task = 0 ;task<3;task++){ + + if(task!=last){ + int point = points[day][task]+ f(day-1, task,points,dp); + maxi = max(maxi,point); + } + } + return dp[day][last] =maxi; + } + int maximumPoints(vector>& arr) { + // Code here + int n = arr.size(); + + vector> dp(n , vector(4,-1)); + return f(n-1, 3 , arr,dp); + } +}; + + +//{ Driver Code Starts. +int main() { + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + vector> arr; + for (int i = 0; i < n; ++i) { + vector temp; + for (int j = 0; j < 3; ++j) { + int x; + cin >> x; + temp.push_back(x); + } + arr.push_back(temp); + } + + Solution obj; + cout << obj.maximumPoints(arr) << endl; + cout << "~" << endl; + } + return 0; +} +// } Driver Code Ends \ No newline at end of file diff --git a/DP/lec10_15.cpp b/DP/lec10_15.cpp new file mode 100644 index 0000000..9cdb51c --- /dev/null +++ b/DP/lec10_15.cpp @@ -0,0 +1,67 @@ +//{ Driver Code Starts +#include +using namespace std; + + +// } Driver Code Ends + +class Solution { + public: + int maximumPoints(vector>& points) { + // Code here + int n = points.size(); + vector> dp(n , vector(4,0)); + + dp[0][0] = max(points[0][1],points[0][2]); + dp[0][1] = max(points[0][0] , points[0][2]); + dp[0][2] = max(points[0][0] , points[0 ][ 1]); + dp[0][3] = max (points[0][0] ,max(points[0][1], points[0][2])); + + for(int day = 1 ; day> t; + while (t--) { + int n; + cin >> n; + vector> arr; + for (int i = 0; i < n; ++i) { + vector temp; + for (int j = 0; j < 3; ++j) { + int x; + cin >> x; + temp.push_back(x); + } + arr.push_back(temp); + } + + Solution obj; + cout << obj.maximumPoints(arr) << endl; + cout << "~" << endl; + } + return 0; +} +// } Driver Code Ends \ No newline at end of file diff --git a/DP/lec10_16.cpp b/DP/lec10_16.cpp new file mode 100644 index 0000000..9eb7cbf --- /dev/null +++ b/DP/lec10_16.cpp @@ -0,0 +1,68 @@ +//{ Driver Code Starts +#include +using namespace std; + + +// } Driver Code Ends + +class Solution { + public: + int maximumPoints(vector>& points) { + // Code here + int n = points.size(); + vectorprev(4,0); + + prev[0] = max(points[0][1],points[0][2]); + prev[1] = max(points[0][0] , points[0][2]); + prev[2] = max(points[0][0] , points[0 ][ 1]); + prev[3] = max (points[0][0] ,max(points[0][1], points[0][2])); + + for(int day = 1 ; daytemp(4,0); + for(int last = 0 ; last <4;last++){ + temp[last]= 0 ; + + for(int task = 0 ; task<3; task++){ + if(task!=last){ + temp[last] = max(temp[last] , points[day][task]+ prev[task]); + } + } + + } + + prev = temp; + + } + + return prev[3]; + } + +}; + + +//{ Driver Code Starts. +int main() { + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + vector> arr; + for (int i = 0; i < n; ++i) { + vector temp; + for (int j = 0; j < 3; ++j) { + int x; + cin >> x; + temp.push_back(x); + } + arr.push_back(temp); + } + + Solution obj; + cout << obj.maximumPoints(arr) << endl; + cout << "~" << endl; + } + return 0; +} +// } Driver Code Ends \ No newline at end of file diff --git a/DP/lec10_17.cpp b/DP/lec10_17.cpp new file mode 100644 index 0000000..551e4f3 --- /dev/null +++ b/DP/lec10_17.cpp @@ -0,0 +1,15 @@ +class Solution { + public: + int f(int i ,int j){ + if(i == 0 && j == 0) return 1; + if(i<0 || j<0) return 0 ; + + int up = f(i-1, j); + int left = f(i , j-1); + + return up+left; + } + int uniquePaths(int m, int n) { + return f(m-1, n-1); + } + }; \ No newline at end of file diff --git a/DP/lec10_18.cpp b/DP/lec10_18.cpp new file mode 100644 index 0000000..756956f --- /dev/null +++ b/DP/lec10_18.cpp @@ -0,0 +1,16 @@ +class Solution { + public: + int f(int i ,int j,vector>&dp){ + if(i == 0 && j == 0) return 1; + if(i<0 || j<0) return 0 ; + if( dp[i][j]!= -1) return dp[i][j]; + int up = f(i-1, j, dp); + int left = f(i , j-1,dp); + + return dp[i][j] = up+left; + } + int uniquePaths(int m, int n) { + vector>dp( m , vector(n,-1)); + return f(m-1, n-1,dp); + } + }; \ No newline at end of file diff --git a/DP/lec10_19.cpp b/DP/lec10_19.cpp new file mode 100644 index 0000000..58b1077 --- /dev/null +++ b/DP/lec10_19.cpp @@ -0,0 +1,33 @@ +class Solution { + public: + int f(int i ,int j,vector>&dp){ + if(i == 0 && j == 0) return 1; + if(i<0 || j<0) return 0 ; + if( dp[i][j]!= -1) return dp[i][j]; + int up = f(i-1, j, dp); + int left = f(i , j-1,dp); + + return dp[i][j] = up+left; + } + int uniquePaths(int m, int n) { + int dp[m][n] ; + + for(int i = 0 ; i0) up = dp[i-1][j]; + if(j>0 ) left = dp[i][j-1]; + dp[i][j] = up + left; + } + } + + } + + return dp[m-1][n-1]; + } + }; \ No newline at end of file diff --git a/DP/lec10_2.cpp b/DP/lec10_2.cpp new file mode 100644 index 0000000..257209d --- /dev/null +++ b/DP/lec10_2.cpp @@ -0,0 +1,19 @@ +class Solution { + public: + int climbStairs(int n) { + if(n<=1) + { + return 1; + } + int ways1 = 1 , ways2 = 1 ;//intialise + int totalways = 0; + for(int i = 2; i<=n;i++) + { + totalways = ways1 + ways2; + ways1 = ways2; + ways2 = totalways; + } + + return totalways; + } + }; \ No newline at end of file diff --git a/DP/lec10_20.cpp b/DP/lec10_20.cpp new file mode 100644 index 0000000..1df14c1 --- /dev/null +++ b/DP/lec10_20.cpp @@ -0,0 +1,30 @@ +class Solution { + public: + + int uniquePaths(int m, int n) { + + + vector prev(n , 0); + + for(int i = 0 ; i curr(n,0); + for(int j = 0 ; j0) up = prev[j]; + if(j>0 ) left = curr[j-1]; + curr[j] = up + left; + } + } + + prev = curr; + + } + + return prev[n-1]; + } + }; \ No newline at end of file diff --git a/DP/lec10_21.cpp b/DP/lec10_21.cpp new file mode 100644 index 0000000..8cf1c72 --- /dev/null +++ b/DP/lec10_21.cpp @@ -0,0 +1,21 @@ +class Solution { + public: + int f (int i , int j , vector> &mat, vector>&dp){ + // if(i>=0 && j>=0 && mat[i][j] == -1) return 0 ; + if (i >= 0 && j >= 0 && mat[i][j] == 1) return 0; + if( i == 0 && j == 0 ) return 1; + + if(i< 0 || j<0) return 0 ; + if(dp[i][j]!=-1) return dp[i][j]; + int up = f(i - 1, j , mat ,dp); + int left = f(i , j-1 , mat,dp); + return dp[i][j]= up+ left; + } + int uniquePathsWithObstacles(vector>& obstacleGrid) { + int n = obstacleGrid.size(); + int m = obstacleGrid[0].size(); + vector>dp( n , vector(m , -1)); + + return f(n-1 , m -1 , obstacleGrid , dp); + } + }; \ No newline at end of file diff --git a/DP/lec10_22.cpp b/DP/lec10_22.cpp new file mode 100644 index 0000000..e8f6e85 --- /dev/null +++ b/DP/lec10_22.cpp @@ -0,0 +1,24 @@ +class Solution { + public: + int uniquePathsWithObstacles(vector>& mat) { + int n =mat.size(); + int m = mat[0].size(); + + int dp[n][m]; + + for(int i = 0 ; i0) up = dp[i-1][j]; + if(j>0) left = dp[i][j-1]; + dp[i][j] = (up + left); + } + } + } + + return dp[n-1][m-1]; + } + }; \ No newline at end of file diff --git a/DP/lec10_23.cpp b/DP/lec10_23.cpp new file mode 100644 index 0000000..ff8b47e --- /dev/null +++ b/DP/lec10_23.cpp @@ -0,0 +1,28 @@ +class Solution { + public: + int uniquePathsWithObstacles(vector>& mat) { + int n = mat.size(); + int m = mat[0].size(); + + vector prev(m, 0); // Should be of size m (columns) + + for (int i = 0; i < n; i++) { // Iterate row-wise + vector curr(m, 0); + for (int j = 0; j < m; j++) { // Iterate column-wise + if (mat[i][j] == 1) { + curr[j] = 0; // Obstacle, no path + } else if (i == 0 && j == 0) { + curr[j] = 1; // Start cell + } else { + int up = (i > 0) ? prev[j] : 0; + int left = (j > 0) ? curr[j - 1] : 0; + curr[j] = up + left; + } + } + prev = curr; // Move current row to previous row + } + + return prev[m - 1]; // Return last cell of last row + } + }; + \ No newline at end of file diff --git a/DP/lec10_24.cpp b/DP/lec10_24.cpp new file mode 100644 index 0000000..233b72d --- /dev/null +++ b/DP/lec10_24.cpp @@ -0,0 +1,19 @@ +class Solution { + public: + int f(int i, int j, vector>& grid) { + if (i == 0 && j == 0) return grid[i][j]; // Base case: starting cell + if (i < 0 || j < 0) return INT_MAX; // Out of bounds, return large value + + int up = f(i - 1, j, grid); + int left = f(i, j - 1, grid); + + return grid[i][j] + min(up, left); + } + + int minPathSum(vector>& grid) { + int n = grid.size(); + int m = grid[0].size(); + return f(n - 1, m - 1, grid); + } + }; + \ No newline at end of file diff --git a/DP/lec10_25.cpp b/DP/lec10_25.cpp new file mode 100644 index 0000000..061a461 --- /dev/null +++ b/DP/lec10_25.cpp @@ -0,0 +1,21 @@ +class Solution { + public: + int f(int i, int j, vector>& grid,vector>& dp) { + if (i == 0 && j == 0) return grid[i][j]; // Base case: starting cell + if (i < 0 || j < 0) return INT_MAX; // Out of bounds, return large value + if(dp[i][j]!=-1) return dp[i][j]; + int up = f(i - 1, j, grid,dp); + int left = f(i, j - 1, grid,dp); + + return dp[i][j] = grid[i][j] + min(up, left); + } + + int minPathSum(vector>& grid) { + + int n = grid.size(); + int m = grid[0].size(); + vector> dp(n ,vector(m ,-1)); + return f(n - 1, m - 1, grid,dp); + } + }; + \ No newline at end of file diff --git a/DP/lec10_26.cpp b/DP/lec10_26.cpp new file mode 100644 index 0000000..9e5e05b --- /dev/null +++ b/DP/lec10_26.cpp @@ -0,0 +1,22 @@ +class Solution { + public: + int minPathSum(vector>& grid) { + int n = grid.size(); + int m = grid[0].size(); + vector> dp(n, vector(m, 0)); + + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + if (i == 0 && j == 0) { + dp[i][j] = grid[i][j]; // Base case: start position + } else { + int up = (i > 0) ? dp[i - 1][j] : INT_MAX; + int left = (j > 0) ? dp[i][j - 1] : INT_MAX; + dp[i][j] = grid[i][j] + min(up, left); + } + } + } + return dp[n - 1][m - 1]; + } + }; + \ No newline at end of file diff --git a/DP/lec10_27.cpp b/DP/lec10_27.cpp new file mode 100644 index 0000000..7691b38 --- /dev/null +++ b/DP/lec10_27.cpp @@ -0,0 +1,24 @@ +class Solution { + public: + int minPathSum(vector>& grid) { + int n = grid.size(); + int m = grid[0].size(); + + vectorprev( m , 0); + for (int i = 0; i < n; i++) { + vector curr(m ,0); + for (int j = 0; j < m; j++) { + if (i == 0 && j == 0) { + curr[j] = grid[i][j]; // Base case: start position + } else { + int up = (i > 0) ? prev[j] : INT_MAX; + int left = (j > 0) ? curr[j - 1] : INT_MAX; + curr[j] = grid[i][j] + min(up, left); + } + } + prev = curr; + } + return prev[m - 1]; + } + }; + \ No newline at end of file diff --git a/DP/lec10_28.cpp b/DP/lec10_28.cpp new file mode 100644 index 0000000..4a6c155 --- /dev/null +++ b/DP/lec10_28.cpp @@ -0,0 +1,24 @@ +class Solution { + public: + + int f( int i , int j ,vector>& triangle,int n) + { + if(i== n-1){ + return triangle[n-1][j]; + + } + + int d = triangle[i][j] + f(i+1,j, triangle , n); + int dg = triangle[i][j] + f(i+1 , j+1 , triangle , n); + + return min(d , dg); + } + int minimumTotal(vector>& triangle) { + + + int n = triangle.size(); + vector> dp(n , vector(n , -1)); + return f(0 , 0 , triangle , n); + + } + }; \ No newline at end of file diff --git a/DP/lec10_29.cpp b/DP/lec10_29.cpp new file mode 100644 index 0000000..c216e17 --- /dev/null +++ b/DP/lec10_29.cpp @@ -0,0 +1,24 @@ +class Solution { + public: + + int f( int i , int j ,vector>& triangle,int n,vector>& dp) + { + if(i== n-1){ + return triangle[n-1][j]; + + } + if(dp[i][j] !=-1) return dp[i][j]; + int d = triangle[i][j] + f(i+1,j, triangle , n,dp); + int dg = triangle[i][j] + f(i+1 , j+1 , triangle , n,dp); + + return dp[i][j] = min(d , dg); + } + int minimumTotal(vector>& triangle) { + + + int n = triangle.size(); + vector> dp(n , vector(n , -1)); + return f(0 , 0 , triangle , n,dp); + + } + }; \ No newline at end of file diff --git a/DP/lec10_3.cpp b/DP/lec10_3.cpp new file mode 100644 index 0000000..6ad5d34 --- /dev/null +++ b/DP/lec10_3.cpp @@ -0,0 +1,30 @@ +// failed at 10th test case + +class Solution { + public: + + int f (int ind , vector&heights) + { + if(ind == 0 ) return 0 ; + + int left = f(ind - 1 , heights)+ abs(heights[ind] - heights[ind-1]); + + int right = INT_MAX; + + if(ind>1) + { + right = f( ind - 2, heights)+ abs(heights[ind] - heights[ind-2]); + } + + return min(left , right); + } + int minCost(vector& height) { + // Code here + int n = height.size(); + return f(n-1 , height); + } +}; + + + + diff --git a/DP/lec10_30.cpp b/DP/lec10_30.cpp new file mode 100644 index 0000000..5fa79ce --- /dev/null +++ b/DP/lec10_30.cpp @@ -0,0 +1,24 @@ +class Solution { + public: + int minimumTotal(vector>& triangle) { + + int n = triangle.size(); + + vector>dp( n , vector(n,0)); + + for(int j = 0 ; j=0; i--) + { + for(int j = i; j>=0 ; j--) + { + int d = triangle[i][j] + dp[i+1][j]; + int dg = triangle[i][j] + dp[i+1][j+1]; + + dp[i][j] = min(d , dg); + } + } + + return dp[0][0]; + } + }; \ No newline at end of file diff --git a/DP/lec10_31.cpp b/DP/lec10_31.cpp new file mode 100644 index 0000000..9117132 --- /dev/null +++ b/DP/lec10_31.cpp @@ -0,0 +1,25 @@ +class Solution { + public: + int minimumTotal(vector>& triangle) { + + int n = triangle.size(); + + vectorfront(n , 0) , curr(n ,0); + + for(int j = 0 ; j=0; i--) + { + for(int j = i; j>=0 ; j--) + { + int d = triangle[i][j] + front[j]; + int dg = triangle[i][j] + front[j+1]; + + curr[j] = min(d , dg); + } + front = curr; + } + + return front[0]; + } + }; \ No newline at end of file diff --git a/DP/lec10_32.cpp b/DP/lec10_32.cpp new file mode 100644 index 0000000..8deb74c --- /dev/null +++ b/DP/lec10_32.cpp @@ -0,0 +1,26 @@ +class Solution { + public: + int f(int i, int j, vector>& matrix) { + if (j < 0 || j >= matrix[0].size()) return 1e8; // Handling out-of-bound cases + if (i == 0) return matrix[0][j]; // Base case: first row values are taken as is + + int up = matrix[i][j] + f(i - 1, j, matrix); + int leftDiag = matrix[i][j] + f(i - 1, j - 1, matrix); + int rightDiag = matrix[i][j] + f(i - 1, j + 1, matrix); + + return min({up, leftDiag, rightDiag}); // Finding the minimum path sum + } + + int minFallingPathSum(vector>& matrix) { + int n = matrix.size(); + int m = matrix[0].size(); + int mini = 1e8; // Initialize to a large value + + for (int j = 0; j < m; j++) { + mini = min(mini, f(n - 1, j, matrix)); // Start from the last row + } + + return mini; + } + }; + \ No newline at end of file diff --git a/DP/lec10_33.cpp b/DP/lec10_33.cpp new file mode 100644 index 0000000..afd68e7 --- /dev/null +++ b/DP/lec10_33.cpp @@ -0,0 +1,28 @@ +class Solution { + public: + int f(int i, int j, vector>& matrix, vector>&dp) { + if (j < 0 || j >= matrix[0].size()) return 1e8; // Handling out-of-bound cases + if (i == 0) return matrix[0][j]; // Base case: first row values are taken as is + + if(dp[i][j]!=-1) return dp[i][j]; + int up = matrix[i][j] + f(i - 1, j, matrix,dp); + int leftDiag = matrix[i][j] + f(i - 1, j - 1, matrix,dp); + int rightDiag = matrix[i][j] + f(i - 1, j + 1, matrix,dp); + + return dp[i][j]= min({up, leftDiag, rightDiag}); // Finding the minimum path sum + } + + int minFallingPathSum(vector>& matrix) { + int n = matrix.size(); + int m = matrix[0].size(); + int mini = 1e8; // Initialize to a large value + + vector>dp(n , vector (m , -1)); + for (int j = 0; j < m; j++) { + mini = min(mini, f(n - 1, j, matrix,dp)); // Start from the last row + } + + return mini; + } + }; + \ No newline at end of file diff --git a/DP/lec10_34.cpp b/DP/lec10_34.cpp new file mode 100644 index 0000000..c782480 --- /dev/null +++ b/DP/lec10_34.cpp @@ -0,0 +1,44 @@ +class Solution { + public: + int f(int i, int j, vector>& matrix, vector>&dp) { + if (j < 0 || j >= matrix[0].size()) return 1e8; // Handling out-of-bound cases + if (i == 0) return matrix[0][j]; // Base case: first row values are taken as is + + if(dp[i][j]!=-1) return dp[i][j]; + int up = matrix[i][j] + f(i - 1, j, matrix,dp); + int leftDiag = matrix[i][j] + f(i - 1, j - 1, matrix,dp); + int rightDiag = matrix[i][j] + f(i - 1, j + 1, matrix,dp); + + return dp[i][j]= min({up, leftDiag, rightDiag}); // Finding the minimum path sum + } + + int minFallingPathSum(vector>& matrix) { + int n = matrix.size(); + int m = matrix[0].size(); + int mini = 1e8; // Initialize to a large value + + vector>dp(n , vector (m , -1)); + + + for(int j = 0 ; j=0) ld+= dp[i-1][j-1]; + else ld +=1e8; + int rd = matrix[i][j]; + if(j+1>& matrix) { + int n = matrix.size(); + int m = matrix[0].size(); + int mini = 1e8; // Initialize to a large value + + vector>dp(n , vector (m , -1)); + + vectorprev( m , 0) , cur(m ,0); + + for(int j = 0 ; j=0) ld+= prev[j-1]; + else ld +=1e8; + int rd = matrix[i][j]; + if(j+1 +using namespace std; + + +// } Driver Code Ends + +class Solution { + public: + + int f(int i, int j1, int j2, int r, int c, vector>& grid) { + if (j1 < 0 || j2 < 0 || j1 >= c || j2 >= c) { + return -1e9; // Large negative value for invalid states + } + + if (i == r - 1) { // Base case: Last row + if (j1 == j2) return grid[i][j1]; + else return grid[i][j1] + grid[i][j2]; + } + + int maxi = -1e9; // Maximum value from all paths + for (int dj1 = -1; dj1 <= 1; dj1++) { + for (int dj2 = -1; dj2 <= 1; dj2++) { + int value = 0; + if (j1 == j2) + value = grid[i][j1]; // Both are at the same cell + else + value = grid[i][j1] + grid[i][j2]; // Different cells + + value += f(i + 1, j1 + dj1, j2 + dj2, r, c, grid); + maxi = max(maxi, value); + } + } + return maxi; + } + int solve(int r, int c, vector>& grid) { + // code here + + return f(0, 0, c - 1, r, c, grid); + } +}; + + +//{ Driver Code Starts. + +int main() { + int t; + cin >> t; + while (t--) { + int n, m; + cin >> n >> m; + vector> grid; + for (int i = 0; i < n; i++) { + vector temp; + for (int j = 0; j < m; j++) { + int x; + cin >> x; + temp.push_back(x); + } + grid.push_back(temp); + } + + Solution obj; + cout << obj.solve(n, m, grid) << "\n"; + cout << "~" + << "\n"; + } + return 0; +} +// } Driver Code Ends \ No newline at end of file diff --git a/DP/lec10_37.cpp b/DP/lec10_37.cpp new file mode 100644 index 0000000..07e86fe --- /dev/null +++ b/DP/lec10_37.cpp @@ -0,0 +1,75 @@ +//{ Driver Code Starts +#include +using namespace std; + + +// } Driver Code Ends + +class Solution { + public: + + int f(int i, int j1, int j2, int r, int c, vector>& grid, vector>> &dp) { + if (j1 < 0 || j2 < 0 || j1 >= c || j2 >= c) { + return -1e9; // Large negative value for invalid states + } + + if (i == r - 1) { // Base case: Last row + if (j1 == j2) return grid[i][j1]; + else return grid[i][j1] + grid[i][j2]; + } + + if( dp[i][j1][j2] !=-1) return dp[i][j1][j2]; + int maxi = -1e9; // Maximum value from all paths + for (int dj1 = -1; dj1 <= 1; dj1++) { + for (int dj2 = -1; dj2 <= 1; dj2++) { + int value = 0; + if (j1 == j2) + value = grid[i][j1]; // Both are at the same cell + else + value = grid[i][j1] + grid[i][j2]; // Different cells + + value += f(i + 1, j1 + dj1, j2 + dj2, r, c, grid , dp); + maxi = max(maxi, value); + } + } + return dp[i][j1][j2] =maxi; + } + int solve(int r, int c, vector>& grid) { + // code here + + // int dp[r][c][c]; + + vector>> dp( r ,vector>( c , vector(c , -1))); + + return f(0, 0, c - 1, r, c, grid ,dp); + } +}; + + +//{ Driver Code Starts. + +int main() { + int t; + cin >> t; + while (t--) { + int n, m; + cin >> n >> m; + vector> grid; + for (int i = 0; i < n; i++) { + vector temp; + for (int j = 0; j < m; j++) { + int x; + cin >> x; + temp.push_back(x); + } + grid.push_back(temp); + } + + Solution obj; + cout << obj.solve(n, m, grid) << "\n"; + cout << "~" + << "\n"; + } + return 0; +} +// } Driver Code Ends \ No newline at end of file diff --git a/DP/lec10_38.cpp b/DP/lec10_38.cpp new file mode 100644 index 0000000..1f4e7db --- /dev/null +++ b/DP/lec10_38.cpp @@ -0,0 +1,85 @@ +//{ Driver Code Starts +#include +using namespace std; + + +// } Driver Code Ends + +class Solution { + public: + int solve(int n, int m, vector>& grid) { + // code here + + vector>> dp( n , vector > ( m , vector ( m ,0))); + + for(int j1 = 0 ; j1=0; i--){ + for(int j1 = 0;j1=0 && j1+dj1= 0 && j2+dj2 > t; + while (t--) { + int n, m; + cin >> n >> m; + vector> grid; + for (int i = 0; i < n; i++) { + vector temp; + for (int j = 0; j < m; j++) { + int x; + cin >> x; + temp.push_back(x); + } + grid.push_back(temp); + } + + Solution obj; + cout << obj.solve(n, m, grid) << "\n"; + cout << "~" + << "\n"; + } + return 0; +} +// } Driver Code Ends \ No newline at end of file diff --git a/DP/lec10_39.cpp b/DP/lec10_39.cpp new file mode 100644 index 0000000..e6be29a --- /dev/null +++ b/DP/lec10_39.cpp @@ -0,0 +1,90 @@ +//{ Driver Code Starts +#include +using namespace std; + + +// } Driver Code Ends + +class Solution { + public: + int solve(int n, int m, vector>& grid) { + // code here + + vector>> dp( n , vector > ( m , vector ( m ,0))); + + vector> front( m , vector(m)) , curr( m , vector(m)); + // int front[m][m]; + // int curr[m][m]; + for(int j1 = 0 ; j1=0; i--){ + for(int j1 = 0;j1=0 && j1+dj1= 0 && j2+dj2 > t; + while (t--) { + int n, m; + cin >> n >> m; + vector> grid; + for (int i = 0; i < n; i++) { + vector temp; + for (int j = 0; j < m; j++) { + int x; + cin >> x; + temp.push_back(x); + } + grid.push_back(temp); + } + + Solution obj; + cout << obj.solve(n, m, grid) << "\n"; + cout << "~" + << "\n"; + } + return 0; +} +// } Driver Code Ends \ No newline at end of file diff --git a/DP/lec10_4.cpp b/DP/lec10_4.cpp new file mode 100644 index 0000000..08f4939 --- /dev/null +++ b/DP/lec10_4.cpp @@ -0,0 +1,58 @@ +//{ Driver Code Starts +#include +using namespace std; + + +// } Driver Code Ends + +class Solution { + public: + + int f (int ind , vector&heights, vector&dp) + { + if(ind == 0 ) return 0 ; + + if(dp[ind]!=-1) return dp[ind]; + int left = f(ind - 1 , heights,dp)+ abs(heights[ind] - heights[ind-1]); + + int right = INT_MAX; + + if(ind>1) + { + right = f( ind - 2, heights,dp)+ abs(heights[ind] - heights[ind-2]); + } + + return dp[ind] = min(left , right); + } + int minCost(vector& height) { + // Code here + int n = height.size(); + vector dp(n+1 , -1); + return f(n-1 , height, dp); + } +}; + + +//{ Driver Code Starts. + +int main() { + string str; + getline(cin, str); + int t = stoi(str); + while (t--) { + getline(cin, str); + stringstream ss(str); + vector arr; + int num; + while (ss >> num) { + arr.push_back(num); + } + Solution ob; + cout << ob.minCost(arr) << endl; + cout << "~" << endl; + } + + return 0; +} + +// } Driver Code Ends \ No newline at end of file diff --git a/DP/lec10_40.cpp b/DP/lec10_40.cpp new file mode 100644 index 0000000..6dab89e --- /dev/null +++ b/DP/lec10_40.cpp @@ -0,0 +1,62 @@ +//{ Driver Code Starts + +#include +using namespace std; + + +// } Driver Code Ends + +// User function template for C++ + +class Solution { + public: + + bool f(int ind , int target , vector &arr){ + if(target == 0 ) return true; + + if( ind == 0 ) return ( arr[0] == target); + + bool notTake = f(ind - 1 , target , arr); + bool take = false; + if(arr[ind] <=target) take = f( ind-1 , target-arr[ind],arr); + return take | notTake; + } + bool isSubsetSum(vector& arr, int target) { + // code here + int n = arr.size(); + return f(n-1 , target , arr); + } +}; + + +//{ Driver Code Starts. + +int main() { + + int t; + cin >> t; + cin.ignore(); + while (t--) { + vector arr; + string input; + getline(cin, input); + stringstream ss(input); + int number; + while (ss >> number) { + arr.push_back(number); + } + int sum; + cin >> sum; + cin.ignore(); + + Solution ob; + if (ob.isSubsetSum(arr, sum)) + cout << "true" << endl; + else + cout << "false" << endl; + cout << "~" << endl; + } + return 0; +} + +// } Driver Code Ends \ No newline at end of file diff --git a/DP/lec10_41.cpp b/DP/lec10_41.cpp new file mode 100644 index 0000000..e675d3a --- /dev/null +++ b/DP/lec10_41.cpp @@ -0,0 +1,65 @@ +//{ Driver Code Starts + +#include +using namespace std; + + +// } Driver Code Ends + +// User function template for C++ + +class Solution { + public: + + bool f(int ind , int target , vector &arr,vector>&dp){ + if(target == 0 ) return true; + + if( ind == 0 ) return ( arr[0] == target); + + if(dp[ind][target]!= -1) return dp[ind][target]; + bool notTake = f(ind - 1 , target , arr,dp); + bool take = false; + if(arr[ind] <=target) take = f( ind-1 , target-arr[ind],arr,dp); + return dp[ind][target]=take | notTake; + } + bool isSubsetSum(vector& arr, int target) { + // code here + int n = arr.size(); + + vector>dp( n+1 , vector(target+1 , -1)); + return f(n-1 , target , arr,dp); + } +}; + + +//{ Driver Code Starts. + +int main() { + + int t; + cin >> t; + cin.ignore(); + while (t--) { + vector arr; + string input; + getline(cin, input); + stringstream ss(input); + int number; + while (ss >> number) { + arr.push_back(number); + } + int sum; + cin >> sum; + cin.ignore(); + + Solution ob; + if (ob.isSubsetSum(arr, sum)) + cout << "true" << endl; + else + cout << "false" << endl; + cout << "~" << endl; + } + return 0; +} + +// } Driver Code Ends \ No newline at end of file diff --git a/DP/lec10_42.cpp b/DP/lec10_42.cpp new file mode 100644 index 0000000..dbfc71f --- /dev/null +++ b/DP/lec10_42.cpp @@ -0,0 +1,85 @@ +//{ Driver Code Starts + +#include +using namespace std; + + +// } Driver Code Ends + +// User function template for C++ + +class Solution { + public: + + bool f(int ind , int target , vector &arr,vector>&dp){ + if(target == 0 ) return true; + + if( ind == 0 ) return ( arr[0] == target); + + if(dp[ind][target]!= -1) return dp[ind][target]; + bool notTake = f(ind - 1 , target , arr,dp); + bool take = false; + if(arr[ind] <=target) take = f( ind-1 , target-arr[ind],arr,dp); + return dp[ind][target]=take | notTake; + } + bool isSubsetSum(vector& arr, int target) { + // code here + int n = arr.size(); + + // 2D DP Table + vector> dp(n, vector(target + 1, false)); + + // Base Case: Sum 0 is always possible + for (int i = 0; i < n; i++) dp[i][0] = true; + + // Base Case: If first element is within target range + if (arr[0] <= target) dp[0][arr[0]] = true; + + // Bottom-Up DP + for (int ind = 1; ind < n; ind++) { + for (int i = 1; i <= target; i++) { + bool notTake = dp[ind - 1][i]; // Exclude current element + bool take = false; + if (arr[ind] <= i) + take = dp[ind - 1][i - arr[ind]]; // Include current element + + dp[ind][i] = take || notTake; // Store result + } + } + + return dp[n - 1][target]; // Final answer + } +}; + + +//{ Driver Code Starts. + +int main() { + + int t; + cin >> t; + cin.ignore(); + while (t--) { + vector arr; + string input; + getline(cin, input); + stringstream ss(input); + int number; + while (ss >> number) { + arr.push_back(number); + } + int sum; + cin >> sum; + cin.ignore(); + + Solution ob; + if (ob.isSubsetSum(arr, sum)) + cout << "true" << endl; + else + cout << "false" << endl; + cout << "~" << endl; + } + return 0; +} + +// } Driver Code Ends \ No newline at end of file diff --git a/DP/lec10_43.cpp b/DP/lec10_43.cpp new file mode 100644 index 0000000..aa250c3 --- /dev/null +++ b/DP/lec10_43.cpp @@ -0,0 +1,77 @@ +//{ Driver Code Starts + +#include +using namespace std; + + +// } Driver Code Ends + +// User function template for C++ + +class Solution { + public: + + + bool isSubsetSum(vector& arr, int target) { + + + int n = arr.size(); + + // Optimized DP arrays (previous & current row) + vector prev(target + 1, false), cur(target + 1, false); + + // Base case: Sum 0 is always possible + prev[0] = cur[0] = true; + + // If first element is within range, mark it + if (arr[0] <= target) prev[arr[0]] = true; + + // Bottom-Up DP + for (int ind = 1; ind < n; ind++) { + for (int i = 1; i <= target; i++) { + bool notTake = prev[i]; // Exclude current element + bool take = false; + if (arr[ind] <= i) + take = prev[i - arr[ind]]; // Include current element + + cur[i] = take || notTake; // Store result + } + prev = cur; // Move current row to previous + } + + return prev[target]; // Final answer + } +}; + + +//{ Driver Code Starts. + +int main() { + + int t; + cin >> t; + cin.ignore(); + while (t--) { + vector arr; + string input; + getline(cin, input); + stringstream ss(input); + int number; + while (ss >> number) { + arr.push_back(number); + } + int sum; + cin >> sum; + cin.ignore(); + + Solution ob; + if (ob.isSubsetSum(arr, sum)) + cout << "true" << endl; + else + cout << "false" << endl; + cout << "~" << endl; + } + return 0; +} + +// } Driver Code Ends \ No newline at end of file diff --git a/DP/lec10_44.cpp b/DP/lec10_44.cpp new file mode 100644 index 0000000..010f385 --- /dev/null +++ b/DP/lec10_44.cpp @@ -0,0 +1,44 @@ +class Solution { + public: + // Helper function: Checks if a subset with the given target sum exists + bool isSubsetSum(vector& arr, int target) { + int n = arr.size(); + + // Optimized DP arrays (previous & current row) + vector prev(target + 1, false), cur(target + 1, false); + + // Base case: Sum 0 is always possible + prev[0] = cur[0] = true; + + // If first element is within range, mark it + if (arr[0] <= target) prev[arr[0]] = true; + + // Bottom-Up DP + for (int ind = 1; ind < n; ind++) { + for (int i = 1; i <= target; i++) { + bool notTake = prev[i]; // Exclude current element + bool take = false; + if (arr[ind] <= i) + take = prev[i - arr[ind]]; // Include current element + + cur[i] = take || notTake; // Store result + } + prev = cur; // Move current row to previous + } + + return prev[target]; // Final answer + } + + // Main function: Checks if the array can be partitioned into two equal subsets + bool canPartition(vector& arr) { + int n = arr.size(); + int totSum = accumulate(arr.begin(), arr.end(), 0); + + // If total sum is odd, partitioning is not possible + if (totSum % 2 != 0) return false; + + int target = totSum / 2; + return isSubsetSum(arr, target); + } + }; + \ No newline at end of file diff --git a/DP/lec10_45.cpp b/DP/lec10_45.cpp new file mode 100644 index 0000000..6bc7d1b --- /dev/null +++ b/DP/lec10_45.cpp @@ -0,0 +1,40 @@ +class Solution { + public: + int minimumDifference(vector& arr) { + int n = arr.size(); + + int totSum = accumulate(arr.begin(), arr.end(), 0); // Efficient sum calculation + int k = totSum; + + vector> dp(n, vector(k + 1, false)); + + // Base case: sum 0 is always possible + for (int i = 0; i < n; i++) dp[i][0] = true; + + // Only mark dp[0][arr[0]] if it's within bounds + if (arr[0] <= k) dp[0][arr[0]] = true; + + // DP filling + for (int ind = 1; ind < n; ind++) { + for (int target = 1; target <= k; target++) { + bool notTake = dp[ind - 1][target]; + bool take = false; + + if (arr[ind] <= target) + take = dp[ind - 1][target - arr[ind]]; + + dp[ind][target] = take || notTake; + } + } + + // Finding the minimum partition difference + int mini = INT_MAX; + for (int s1 = 0; s1 <= totSum / 2; s1++) { + if (dp[n - 1][s1]) { + mini = min(mini, abs((totSum - s1) - s1)); + } + } + return mini; + } + }; + \ No newline at end of file diff --git a/DP/lec10_46.cpp b/DP/lec10_46.cpp new file mode 100644 index 0000000..fd7c1b7 --- /dev/null +++ b/DP/lec10_46.cpp @@ -0,0 +1,44 @@ +class Solution { + public: + int minimumDifference(vector& nums) { + int n = nums.size(), sum = 0; + sum = accumulate(nums.begin(), nums.end(),0); // To find the total sum of the array + + int N = n/2; // Divide it into two equals parts as length is even + vector> left(N+1), right(N+1); // left array and right array + + //All possible sum in left and right part (Generating and storing using BIT-Masking) + for(int mask = 0; mask<(1< +using namespace std; + + +// } Driver Code Ends + +class Solution { + public: + int f(int ind , int sum , vector&num,vector> &dp){ + + if(sum == 0) return 1; + if(ind == 0) return num[0] == sum; + if(dp[ind][sum]!=-1) return dp[ind][sum]; + int notTake =f(ind-1 , sum,num,dp); + int take = 0 ; + if(num[ind]<=sum) take = f(ind-1 , sum - num[ind],num,dp); + + + return dp[ind][sum] = notTake + take; + } + int perfectSum(vector& num, int tar) { + // code here + int n = num.size(); + vector> dp( n , vector(tar+1 ,-1)); + + + return f(n-1 , tar , num , dp); + } +}; + + + + + +//{ Driver Code Starts. + +int main() { + int t; + cin >> t; + cin.ignore(); // Ignore newline character after t + + while (t--) { + vector arr; + int target; + string inputLine; + + getline(cin, inputLine); // Read the array input as a line + stringstream ss(inputLine); + int value; + while (ss >> value) { + arr.push_back(value); + } + + cin >> target; + cin.ignore(); // Ignore newline character after target input + + Solution solution; + cout << solution.perfectSum(arr, target); + cout << "\n~\n"; + } + + return 0; +} + +// } Driver Code Ends \ No newline at end of file diff --git a/DP/lec10_48.cpp b/DP/lec10_48.cpp new file mode 100644 index 0000000..72ba472 --- /dev/null +++ b/DP/lec10_48.cpp @@ -0,0 +1,72 @@ +//{ Driver Code Starts +#include +using namespace std; + + +// } Driver Code Ends + +class Solution { +public: + int perfectSum(vector& num, int tar) { + int n = num.size(); + vector> dp(n, vector(tar + 1, 0)); + + // Base case: empty subset always forms a sum of 0. + dp[0][0] = 1; + + // If the first element is within the target range, count it + if (num[0] <= tar) dp[0][num[0]] += 1; + + // Special case for num[0] == 0 (double count the empty subset) + if (num[0] == 0) dp[0][0] = 2; + + for (int ind = 1; ind < n; ind++) { + for (int sum = 0; sum <= tar; sum++) { + int notTake = dp[ind - 1][sum]; + int take = 0; + if (num[ind] <= sum) take = dp[ind - 1][sum - num[ind]]; + + dp[ind][sum] = notTake + take; + } + } + + return dp[n - 1][tar]; + } +}; + + + + + + +//{ Driver Code Starts. + +int main() { + int t; + cin >> t; + cin.ignore(); // Ignore newline character after t + + while (t--) { + vector arr; + int target; + string inputLine; + + getline(cin, inputLine); // Read the array input as a line + stringstream ss(inputLine); + int value; + while (ss >> value) { + arr.push_back(value); + } + + cin >> target; + cin.ignore(); // Ignore newline character after target input + + Solution solution; + cout << solution.perfectSum(arr, target); + cout << "\n~\n"; + } + + return 0; +} + +// } Driver Code Ends \ No newline at end of file diff --git a/DP/lec10_49.cpp b/DP/lec10_49.cpp new file mode 100644 index 0000000..fea94de --- /dev/null +++ b/DP/lec10_49.cpp @@ -0,0 +1,69 @@ +//{ Driver Code Starts +#include +using namespace std; + + +// } Driver Code Ends + +class Solution { +public: + int perfectSum(vector& num, int tar) { + int n = num.size(); + vector prev(tar+1,0) , cur(tar+1); + + prev[0] = cur[0] = 1; + + if(num[0] <=tar ) prev[num[0]] = 1; + + for (int ind = 1; ind < n; ind++) { + for (int sum = 0; sum <= tar; sum++) { + int notTake = prev[sum]; + int take = 0; + if (num[ind] <= sum) take = prev[sum - num[ind]]; + + cur[sum] = notTake + take; + } + + prev = cur; + } + + return prev[tar]; + } +}; + + + + + + +//{ Driver Code Starts. + +int main() { + int t; + cin >> t; + cin.ignore(); // Ignore newline character after t + + while (t--) { + vector arr; + int target; + string inputLine; + + getline(cin, inputLine); // Read the array input as a line + stringstream ss(inputLine); + int value; + while (ss >> value) { + arr.push_back(value); + } + + cin >> target; + cin.ignore(); // Ignore newline character after target input + + Solution solution; + cout << solution.perfectSum(arr, target); + cout << "\n~\n"; + } + + return 0; +} + +// } Driver Code Ends \ No newline at end of file diff --git a/DP/lec10_5.cpp b/DP/lec10_5.cpp new file mode 100644 index 0000000..6389e4b --- /dev/null +++ b/DP/lec10_5.cpp @@ -0,0 +1,58 @@ +//{ Driver Code Starts +#include +using namespace std; + + +// } Driver Code Ends + +class Solution { + public: + int minCost(vector& height) { + // Code here + + int n = height.size(); + + vectordp(n,0); + + for(int i = 1 ; i1) + { + ss = dp[i-2] + abs(height[i]- height[i-2]); + } + + dp[i] = min(fs , ss); + } + + return dp[n-1]; + } +}; + + +//{ Driver Code Starts. + +int main() { + string str; + getline(cin, str); + int t = stoi(str); + while (t--) { + getline(cin, str); + stringstream ss(str); + vector arr; + int num; + while (ss >> num) { + arr.push_back(num); + } + Solution ob; + cout << ob.minCost(arr) << endl; + cout << "~" << endl; + } + + return 0; +} + +// } Driver Code Ends \ No newline at end of file diff --git a/DP/lec10_50.cpp b/DP/lec10_50.cpp new file mode 100644 index 0000000..1a679c4 --- /dev/null +++ b/DP/lec10_50.cpp @@ -0,0 +1,92 @@ +//{ Driver Code Starts +// Initial function template for C++ + +#include +using namespace std; + + +// } Driver Code Ends + +class Solution { + public: +int mod = (int) ( 1e9 + 7); +int f(int ind , int sum , vector &num , vector> &dp){ + + if(ind == 0){ + + if(sum == 0 && num[0] == 0) return 2; + if( sum == 0 || sum == num[0]) return 1; + + return 0; + } + + + if( dp[ind][sum] !=-1) return dp[ind][sum]; + + int notTake = f(ind - 1 , sum , num , dp); + + int take = 0 ; + if(num[ind]<=sum) take = f(ind - 1 , sum - num[ind] , num , dp); + + return dp[ind][sum] = (notTake + take)%mod; +} + +int findWays ( vector &num , int tar) +{ + int n = num.size(); + + vector> dp( n , vector (tar +1 , -1)); + + return f( n-1 , tar , num , dp); +} + int countPartitions(vector& arr, int d) { + // Code here + + int n = arr.size(); + + int totSum = 0 ; + + for( auto &it : arr) totSum +=it; + + if(totSum -d <0 || (totSum -d) %2) return false; + + return findWays(arr , (totSum -d)/2); + } +}; + + +//{ Driver Code Starts. + +int main() { + int test_case; + cin >> test_case; + cin.ignore(); + while (test_case--) { + + int d; + vector arr, brr, crr; + string input; + getline(cin, input); + stringstream ss(input); + int number; + while (ss >> number) { + arr.push_back(number); + } + getline(cin, input); + ss.clear(); + ss.str(input); + while (ss >> number) { + crr.push_back(number); + } + d = crr[0]; + int n = arr.size(); + Solution ob; + int ans = ob.countPartitions(arr, d); + cout << ans << endl; + + cout << "~" + << "\n"; + } + return 0; +} +// } Driver Code Ends \ No newline at end of file diff --git a/DP/lec10_54.cpp b/DP/lec10_54.cpp new file mode 100644 index 0000000..ec87598 --- /dev/null +++ b/DP/lec10_54.cpp @@ -0,0 +1,24 @@ +#include + +int f( int ind , int W , vector & wt , vector &val){ + if(ind == 0){ + if(wt[0] <= W) return val[0]; + return 0; + } + + int notTake = 0 + f(ind - 1 , W ,wt , val); + + int take = INT_MIN; + + if(wt[ind]<=W){ + take = val[ind] + f(ind-1 , W- wt[ind],wt , val); + } + + return max( take , notTake); +} +int knapsack(vector weight, vector value, int n, int maxWeight) +{ + // Write your code here + + return f(n-1 , maxWeight, weight, value); +} \ No newline at end of file diff --git a/DP/lec10_55.cpp b/DP/lec10_55.cpp new file mode 100644 index 0000000..b40d84d --- /dev/null +++ b/DP/lec10_55.cpp @@ -0,0 +1,25 @@ +#include + +int f( int ind , int W , vector & wt , vector &val, vector> &dp){ + if(ind == 0){ + if(wt[0] <= W) return val[0]; + return 0; + } + + if(dp[ind][W]!=-1) return dp[ind][W]; + int notTake = 0 + f(ind - 1 , W ,wt , val ,dp); + + int take = INT_MIN; + + if(wt[ind]<=W){ + take = val[ind] + f(ind-1 , W- wt[ind],wt , val , dp); + } + + return dp[ind][W] = max( take , notTake); +} +int knapsack(vector weight, vector value, int n, int maxWeight) +{ + // Write your code here + vector> dp( n , vector(maxWeight+1 , -1)); + return f(n-1 , maxWeight, weight, value , dp); +} \ No newline at end of file diff --git a/DP/lec10_56.cpp b/DP/lec10_56.cpp new file mode 100644 index 0000000..588a6b1 --- /dev/null +++ b/DP/lec10_56.cpp @@ -0,0 +1,26 @@ +#include + +int knapsack(vector wt, vector val, int n, int maxWeight) +{ + // Write your code here + vector> dp( n , vector(maxWeight+1 , 0)); + + for(int W = wt[0];W<=maxWeight ; W++) dp[0][W] = val[0]; + for(int ind = 1 ; ind + +int knapsack(vector wt, vector val, int n, int maxWeight) +{ + // Write your code here + + vector prev(maxWeight+1 ,0 ); + + for(int W = wt[0] ; W<=maxWeight ; W++) prev[W]= val[0]; + + for(int ind = 1 ; ind=0; W--){ + + int notTake = 0 + prev[ W]; + + int take = INT_MIN; + + if(wt[ind]<=W){ + take = val[ind] + prev[ W-wt[ind]]; + } + + prev[W] = max(take ,notTake); + + } + } + return prev[maxWeight]; +} \ No newline at end of file diff --git a/DP/lec10_58.cpp b/DP/lec10_58.cpp new file mode 100644 index 0000000..0333d49 --- /dev/null +++ b/DP/lec10_58.cpp @@ -0,0 +1,18 @@ +class Solution { + public: + int findContentChildren(vector& g, vector& s) { + sort(g.begin(), g.end()); // Sort greed factors + sort(s.begin(), s.end()); // Sort cookie sizes + + int i = 0, j = 0, count = 0; + while (i < g.size() && j < s.size()) { + if (s[j] >= g[i]) { // If the cookie can satisfy the child + count++; + i++; // Move to next child + } + j++; // Move to next cookie + } + return count; + } + }; + \ No newline at end of file diff --git a/DP/lec10_59.cpp b/DP/lec10_59.cpp new file mode 100644 index 0000000..8eb257a --- /dev/null +++ b/DP/lec10_59.cpp @@ -0,0 +1,28 @@ +class Solution { + + int f(int ind , int T , vector &nums){ + + if(ind == 0 ){ + + if(T % nums[0] == 0) return T / nums[0]; + + return 1e9; + } + + int notTake = 0 + f(ind - 1 , T , nums); + int take = INT_MAX; + + if(nums[ind]<=T) take = 1 + f(ind , T - nums[ind] , nums); + + return min(take , notTake); + } + public: + int coinChange(vector& num, int target) { + int n = num.size(); + int ans = f(n-1 , target , num); + + if( ans >= 1e9) return -1; + + return ans; + } + }; \ No newline at end of file diff --git a/DP/lec10_6.cpp b/DP/lec10_6.cpp new file mode 100644 index 0000000..5533927 --- /dev/null +++ b/DP/lec10_6.cpp @@ -0,0 +1,64 @@ +//{ Driver Code Starts +#include +using namespace std; + + +// } Driver Code Ends + +class Solution { + public: + int minCost(vector& height) { + // Code here + + int n = height.size(); + + + + int prev = 0 ; + int prev2 = 0 ; + int curr = 0 ; + for(int i = 1 ; i1) + { + ss = prev2 + abs(height[i]- height[i-2]); + } + + curr= min(fs , ss); + + prev2 = prev; + prev = curr; + } + + return prev; + } +}; + + +//{ Driver Code Starts. + +int main() { + string str; + getline(cin, str); + int t = stoi(str); + while (t--) { + getline(cin, str); + stringstream ss(str); + vector arr; + int num; + while (ss >> num) { + arr.push_back(num); + } + Solution ob; + cout << ob.minCost(arr) << endl; + cout << "~" << endl; + } + + return 0; +} + +// } Driver Code Ends \ No newline at end of file diff --git a/DP/lec10_60.cpp b/DP/lec10_60.cpp new file mode 100644 index 0000000..1d5335a --- /dev/null +++ b/DP/lec10_60.cpp @@ -0,0 +1,30 @@ +class Solution { + + int f(int ind , int T , vector &nums , vector> &dp){ + + if(ind == 0 ){ + + if(T % nums[0] == 0) return T / nums[0]; + + return 1e9; + } + if(dp[ind][T]!=-1) return dp[ind][T]; + int notTake = 0 + f(ind - 1 , T , nums,dp); + int take = INT_MAX; + + if(nums[ind]<=T) take = 1 + f(ind , T - nums[ind] , nums,dp); + + return dp[ind][T] =min(take , notTake); + } + public: + int coinChange(vector& num, int target) { + int n = num.size(); + + vector> dp( n , vector (target+1 , -1)); + int ans = f(n-1 , target , num,dp); + + if( ans >= 1e9) return -1; + + return ans; + } + }; \ No newline at end of file diff --git a/DP/lec10_61.cpp b/DP/lec10_61.cpp new file mode 100644 index 0000000..cce7c50 --- /dev/null +++ b/DP/lec10_61.cpp @@ -0,0 +1,49 @@ +class Solution { + + int f(int ind , int T , vector &nums , vector> &dp){ + + if(ind == 0 ){ + + if(T % nums[0] == 0) return T / nums[0]; + + return 1e9; + } + if(dp[ind][T]!=-1) return dp[ind][T]; + int notTake = 0 + f(ind - 1 , T , nums,dp); + int take = INT_MAX; + + if(nums[ind]<=T) take = 1 + f(ind , T - nums[ind] , nums,dp); + + return dp[ind][T] =min(take , notTake); + } + public: + int coinChange(vector& nums, int target) { + int n = nums.size(); + + vector> dp( n , vector (target+1 , -1)); + + + for(int T = 0 ; T<=target ; T++){ + if(T % nums[0] == 0) dp[0][T] = T/nums[0]; + else dp[0][T] = 1e9; + } + + for(int ind = 1 ; ind= 1e9) return -1; + + return ans; + } + }; \ No newline at end of file diff --git a/DP/lec10_62.cpp b/DP/lec10_62.cpp new file mode 100644 index 0000000..4bb82c8 --- /dev/null +++ b/DP/lec10_62.cpp @@ -0,0 +1,31 @@ +class Solution { + public: + int coinChange(vector& nums, int target) { + int n = nums.size(); + vector prev(target + 1, 1e9), cur(target + 1, 1e9); + + // Base case: When target is 0, no coins are needed + prev[0] = cur[0] = 0; + + // Fill base case for first coin + for (int T = 0; T <= target; T++) { + if (T % nums[0] == 0) prev[T] = T / nums[0]; + } + + for (int ind = 1; ind < n; ind++) { + for (int T = 0; T <= target; T++) { + int notTake = prev[T]; + int take = 1e9; + + if (nums[ind] <= T) take = 1 + cur[T - nums[ind]]; // **Use cur, not prev** + + cur[T] = min(take, notTake); + } + prev.swap(cur); // **Efficient way to copy instead of prev = cur** + } + + int ans = prev[target]; + return (ans >= 1e9) ? -1 : ans; + } + }; + \ No newline at end of file diff --git a/DP/lec10_63.cpp b/DP/lec10_63.cpp new file mode 100644 index 0000000..da0fe63 --- /dev/null +++ b/DP/lec10_63.cpp @@ -0,0 +1,45 @@ +class Solution { + public: + + int findWays( vector& num , int tar) + { + int n = num.size(); + + vector prev( tar + 1 , 0) , cur(tar +1 , 0); + + if(num[0] == 0) prev[0] = 2; + + else prev[0]= 1 ; + + if(num[0] !=0 && num[0] <= tar ) prev[num[0]] = 1 ; + + for(int ind = 1 ; ind &arr){ + int totSum = 0 ; + for(auto &it : arr) totSum +=it; + + if(totSum -d <0 || (totSum - d)%2) return false ; + + return findWays(arr , (totSum-d)/2); + } + int findTargetSumWays(vector& nums, int target) { + + int n = nums.size(); + return countPartitions(n , target , nums); + } + }; \ No newline at end of file diff --git a/DP/lec10_64.cpp b/DP/lec10_64.cpp new file mode 100644 index 0000000..0fe59d1 --- /dev/null +++ b/DP/lec10_64.cpp @@ -0,0 +1,23 @@ +class Solution { + public: + + int f(int ind , int T , vector&a){ + + if( ind ==0 ){ + + return (T % a[0] == 0 ); + } + + int notTake = f(ind -1 , T , a); + + int take = 0 ; + if(a[ind]<=T) take = f(ind , T - a[ind] ,a); + + return take + notTake ; + } + int change(int amount, vector& coins) { + + int n = coins.size(); + return f(n-1 , amount , coins); + } + }; \ No newline at end of file diff --git a/DP/lec10_65.cpp b/DP/lec10_65.cpp new file mode 100644 index 0000000..4a1a75c --- /dev/null +++ b/DP/lec10_65.cpp @@ -0,0 +1,24 @@ +class Solution { + public: + + int f(int ind , int T , vector&a , vector>&dp){ + + if( ind ==0 ){ + + return (T % a[0] == 0 ); + } + if(dp[ind][T]!=-1) return dp[ind][T]; + int notTake = f(ind -1 , T , a,dp); + + int take = 0 ; + if(a[ind]<=T) take = f(ind , T - a[ind] ,a,dp); + + return dp[ind][T] = take + notTake ; + } + int change(int amount, vector& coins) { + + int n = coins.size(); + vector> dp(n, vector(amount+1, -1)); + return f(n-1 , amount , coins,dp); + } + }; \ No newline at end of file diff --git a/DP/lec10_66.cpp b/DP/lec10_66.cpp new file mode 100644 index 0000000..4aeb3b0 --- /dev/null +++ b/DP/lec10_66.cpp @@ -0,0 +1,42 @@ +class Solution { + public: + + int f(int ind , int T , vector&a , vector>&dp){ + + if( ind ==0 ){ + + return (T % a[0] == 0 ); + } + if(dp[ind][T]!=-1) return dp[ind][T]; + int notTake = f(ind -1 , T , a,dp); + + int take = 0 ; + if(a[ind]<=T) take = f(ind , T - a[ind] ,a,dp); + + return dp[ind][T] = take + notTake ; + } + int change(int amount, vector& a) { + + int n = a.size(); + vector> dp(n, vector(amount+1, 0)); + + for(int T = 0 ; T <= amount;T++){ + dp[0][T] = ( T% a[0] == 0); + } + + for(int ind = 1 ; ind < n; ind++){ + + for(int T = 0 ; T<=amount ; T++){ + + int notTake = dp[ind -1][T]; + + int take = 0 ; + if(a[ind]<=T) take = dp[ind][T - a[ind] ]; + + dp[ind][T] = take + notTake ; + + } + } + return dp[n-1][ amount ]; + } + }; \ No newline at end of file diff --git a/DP/lec10_67.cpp b/DP/lec10_67.cpp new file mode 100644 index 0000000..440cc2e --- /dev/null +++ b/DP/lec10_67.cpp @@ -0,0 +1,46 @@ +class Solution { + public: + + int f(int ind , int T , vector&a , vector>&dp){ + + if( ind ==0 ){ + + return (T % a[0] == 0 ); + } + if(dp[ind][T]!=-1) return dp[ind][T]; + int notTake = f(ind -1 , T , a,dp); + + int take = 0 ; + if(a[ind]<=T) take = f(ind , T - a[ind] ,a,dp); + + return dp[ind][T] = take + notTake ; + } + int change(int amount, vector& a) { + + int n = a.size(); + vector> dp(n, vector(amount+1, 0)); + + vectorprev(amount+1 , 0) , cur(amount +1 , 0); + + for(int T = 0 ; T <= amount;T++){ + prev[T] = ( T% a[0] == 0); + } + + for(int ind = 1 ; ind < n; ind++){ + + for(int T = 0 ; T<=amount ; T++){ + + int notTake = prev[T]; + + int take = 0 ; + if(a[ind]<=T) take = cur[T - a[ind] ]; + + cur[T] = take + notTake ; + + } + + prev = cur; + } + return prev[ amount ]; + } + }; \ No newline at end of file diff --git a/DP/lec10_68.cpp b/DP/lec10_68.cpp new file mode 100644 index 0000000..fb120c4 --- /dev/null +++ b/DP/lec10_68.cpp @@ -0,0 +1,69 @@ +//{ Driver Code Starts +// Initial Template for C++ + +#include +using namespace std; + + +// } Driver Code Ends + +// User function Template for C++ + +class Solution { + public: + +int f(int ind , int W , vector &val , vector & wt){ + + if(ind == 0){ + + return ((int )(W / wt[0]))* val[0]; + } + + int notTake = 0 + f(ind-1 , W , val , wt); + + int take = 0 ; + if(wt[ind]<= W){ + take = val[ind] + f(ind , W - wt[ind] , val , wt); + } + return max(take , notTake); +} + int knapSack(vector& val, vector& wt, int w) { + // code here + int n = val.size(); + return f(n-1 , w , val , wt); + } +}; + + +//{ Driver Code Starts. + +int main() { + int t; + cin >> t; + while (t--) { + int W; + cin >> W; + cin.ignore(); + string str; + getline(cin, str); + stringstream ss(str); + vector val; + int num; + while (ss >> num) { + val.push_back(num); + } + string str2; + getline(cin, str2); + stringstream ss2(str2); + vector wt; + int num2; + while (ss2 >> num2) { + wt.push_back(num2); + } + Solution ob; + cout << ob.knapSack(val, wt, W) << endl; + cout << "~" << endl; + } + return 0; +} +// } Driver Code Ends \ No newline at end of file diff --git a/DP/lec10_69.cpp b/DP/lec10_69.cpp new file mode 100644 index 0000000..3277b56 --- /dev/null +++ b/DP/lec10_69.cpp @@ -0,0 +1,71 @@ +//{ Driver Code Starts +// Initial Template for C++ + +#include +using namespace std; + + +// } Driver Code Ends + +// User function Template for C++ + +class Solution { + public: + +int f(int ind , int W , vector &val , vector & wt, vector> &dp){ + + if(ind == 0){ + + return ((int )(W / wt[0]))* val[0]; + } + if(dp[ind][W]!=-1) return dp[ind][W]; + int notTake = 0 + f(ind-1 , W , val , wt ,dp); + + int take = 0 ; + if(wt[ind]<= W){ + take = val[ind] + f(ind , W - wt[ind] , val , wt,dp); + } + return dp[ind][W] = max(take , notTake); +} + int knapSack(vector& val, vector& wt, int w) { + // code here + int n = val.size(); + + vector> dp( n , vector(w+1 , -1)); + return f(n-1 , w , val , wt,dp); + } +}; + + +//{ Driver Code Starts. + +int main() { + int t; + cin >> t; + while (t--) { + int W; + cin >> W; + cin.ignore(); + string str; + getline(cin, str); + stringstream ss(str); + vector val; + int num; + while (ss >> num) { + val.push_back(num); + } + string str2; + getline(cin, str2); + stringstream ss2(str2); + vector wt; + int num2; + while (ss2 >> num2) { + wt.push_back(num2); + } + Solution ob; + cout << ob.knapSack(val, wt, W) << endl; + cout << "~" << endl; + } + return 0; +} +// } Driver Code Ends \ No newline at end of file diff --git a/DP/lec10_7.cpp b/DP/lec10_7.cpp new file mode 100644 index 0000000..a5dd405 --- /dev/null +++ b/DP/lec10_7.cpp @@ -0,0 +1,77 @@ +//{ Driver Code Starts +#include +using namespace std; + + +// } Driver Code Ends + +class Solution { + public: + + int solveUtil(int ind ,vector& arr, vector&dp,int k) + { + if(ind == 0) return 0 ; + + if(dp[ind] !=-1) return dp[ind]; + + int mmSteps =INT_MAX; + + for(int j = 1 ; j<=k;j++) + { + if(ind - j >=0){ + + int jump = solveUtil(ind-j, arr, dp ,k) +abs(arr[ind]-arr[ind-j]); + + mmSteps = min(jump , mmSteps); + } + } + + return dp[ind] = mmSteps; + } + int solve(int n , vector & arr, int k ) + { + vectordp(n , -1); + + return solveUtil(n-1,arr,dp,k); + } + int minimizeCost(int k, vector& arr) { + // Code here + + int n = arr.size(); + + vectordp(n , -1); + + return solve(n, arr, k); + } +}; + + +//{ Driver Code Starts. + +int main() { + string ts; + getline(cin, ts); + int t = stoi(ts); + while (t--) { + string ks; + getline(cin, ks); + int k = stoi(ks); + vector arr; + string input; + getline(cin, input); + stringstream ss(input); + int number; + while (ss >> number) { + arr.push_back(number); + } + Solution obj; + int res = obj.minimizeCost(k, arr); + cout << res << endl; + cout << "~" << endl; + // string tl; + // getline(cin, tl); + } + return 0; +} + +// } Driver Code Ends \ No newline at end of file diff --git a/DP/lec10_70.cpp b/DP/lec10_70.cpp new file mode 100644 index 0000000..bdb48a6 --- /dev/null +++ b/DP/lec10_70.cpp @@ -0,0 +1,74 @@ +//{ Driver Code Starts +// Initial Template for C++ + +#include +using namespace std; + + +// } Driver Code Ends + +// User function Template for C++ + +class Solution { + public: + + + int knapSack(vector& val, vector& wt, int w) { + // code here + int n = val.size(); + + vector> dp( n , vector(w+1 , 0)); + + for(int W = 0 ; W<=w ; W++){ + dp[0][W] = ((int )(W / wt[0]))* val[0]; + } + + for(int ind = 1 ; ind > t; + while (t--) { + int W; + cin >> W; + cin.ignore(); + string str; + getline(cin, str); + stringstream ss(str); + vector val; + int num; + while (ss >> num) { + val.push_back(num); + } + string str2; + getline(cin, str2); + stringstream ss2(str2); + vector wt; + int num2; + while (ss2 >> num2) { + wt.push_back(num2); + } + Solution ob; + cout << ob.knapSack(val, wt, W) << endl; + cout << "~" << endl; + } + return 0; +} +// } Driver Code Ends \ No newline at end of file diff --git a/DP/lec10_71.cpp b/DP/lec10_71.cpp new file mode 100644 index 0000000..bf6cab4 --- /dev/null +++ b/DP/lec10_71.cpp @@ -0,0 +1,160 @@ +//{ Driver Code Starts +// Initial Template for C++ + +#include +using namespace std; + + +// } Driver Code Ends + +// User function Template for C++ + +class Solution { + public: + + + int knapSack(vector& val, vector& wt, int w) { + // code here + int n = val.size(); + + vector> dp( n , vector(w+1 , 0)); + vector prev(w+1 ,0) , cur(w+1,0); + for(int W = 0 ; W<=w ; W++){ + prev[W] = ((int )(W / wt[0]))* val[0]; + } + + for(int ind = 1 ; ind > t; + while (t--) { + int W; + cin >> W; + cin.ignore(); + string str; + getline(cin, str); + stringstream ss(str); + vector val; + int num; + while (ss >> num) { + val.push_back(num); + } + string str2; + getline(cin, str2); + stringstream ss2(str2); + vector wt; + int num2; + while (ss2 >> num2) { + wt.push_back(num2); + } + Solution ob; + cout << ob.knapSack(val, wt, W) << endl; + cout << "~" << endl; + } + return 0; +} +// } Driver Code Ends + + + + + +// single array optimized space +//{ Driver Code Starts +// Initial Template for C++ + +#include +using namespace std; + + +// } Driver Code Ends + +// User function Template for C++ + +class Solution { + public: + + + int knapSack(vector& val, vector& wt, int w) { + // code here + int n = val.size(); + + vector> dp( n , vector(w+1 , 0)); + vector prev(w+1 ,0) ; + for(int W = 0 ; W<=w ; W++){ + prev[W] = ((int )(W / wt[0]))* val[0]; + } + + for(int ind = 1 ; ind > t; + while (t--) { + int W; + cin >> W; + cin.ignore(); + string str; + getline(cin, str); + stringstream ss(str); + vector val; + int num; + while (ss >> num) { + val.push_back(num); + } + string str2; + getline(cin, str2); + stringstream ss2(str2); + vector wt; + int num2; + while (ss2 >> num2) { + wt.push_back(num2); + } + Solution ob; + cout << ob.knapSack(val, wt, W) << endl; + cout << "~" << endl; + } + return 0; +} +// } Driver Code Ends \ No newline at end of file diff --git a/DP/lec10_72.cpp b/DP/lec10_72.cpp new file mode 100644 index 0000000..42ca7fe --- /dev/null +++ b/DP/lec10_72.cpp @@ -0,0 +1,66 @@ +//{ Driver Code Starts +// Initial Template for C++ + +#include +using namespace std; + + +// } Driver Code Ends + +// User function Template for C++ + +class Solution { + public: + +int f(int ind , int N , vector &price){ + + if(ind == 0){ + return N * price[0]; + } + + int notTake = 0 + f(ind-1 , N , price); + int take = INT_MIN; + + int rodLength = ind +1 ; + if( rodLength <=N){ + + take = price[ind] + f(ind , N- rodLength ,price); + } + + return max(take , notTake); +} + int cutRod(vector &price) { + // code here + + int n = price.size(); + + + return f(n-1 , n , price); + } +}; + + +//{ Driver Code Starts. + +int main() { + int t; + scanf("%d ", &t); + while (t--) { + + vector a; + string input; + getline(cin, input); + stringstream ss(input); + int number; + while (ss >> number) { + a.push_back(number); + } + + Solution ob; + + cout << ob.cutRod(a) << endl; + cout << "~" << endl; + } + return 0; +} +// } Driver Code Ends \ No newline at end of file diff --git a/DP/lec10_73.cpp b/DP/lec10_73.cpp new file mode 100644 index 0000000..cb18f3f --- /dev/null +++ b/DP/lec10_73.cpp @@ -0,0 +1,66 @@ +//{ Driver Code Starts +// Initial Template for C++ + +#include +using namespace std; + + +// } Driver Code Ends + +// User function Template for C++ + +class Solution { + public: + +int f(int ind , int N , vector &price , vector>&dp){ + + if(ind == 0){ + return N * price[0]; + } + if(dp[ind][N]!=-1) return dp[ind][N]; + int notTake = 0 + f(ind-1 , N , price ,dp); + int take = INT_MIN; + + int rodLength = ind +1 ; + if( rodLength <=N){ + + take = price[ind] + f(ind , N- rodLength ,price,dp); + } + + return dp[ind][N] = max(take , notTake); +} + int cutRod(vector &price) { + // code here + + int n = price.size(); + + vector>dp (n , vector( n+1 , -1)); + return f(n-1 , n , price,dp); + } +}; + + +//{ Driver Code Starts. + +int main() { + int t; + scanf("%d ", &t); + while (t--) { + + vector a; + string input; + getline(cin, input); + stringstream ss(input); + int number; + while (ss >> number) { + a.push_back(number); + } + + Solution ob; + + cout << ob.cutRod(a) << endl; + cout << "~" << endl; + } + return 0; +} +// } Driver Code Ends \ No newline at end of file diff --git a/DP/lec10_74.cpp b/DP/lec10_74.cpp new file mode 100644 index 0000000..94bf98c --- /dev/null +++ b/DP/lec10_74.cpp @@ -0,0 +1,86 @@ +//{ Driver Code Starts +// Initial Template for C++ + +#include +using namespace std; + + +// } Driver Code Ends + +// User function Template for C++ + +class Solution { + public: + +int f(int ind , int N , vector &price , vector>&dp){ + + if(ind == 0){ + return N * price[0]; + } + if(dp[ind][N]!=-1) return dp[ind][N]; + int notTake = 0 + f(ind-1 , N , price ,dp); + int take = INT_MIN; + + int rodLength = ind +1 ; + if( rodLength <=N){ + + take = price[ind] + f(ind , N- rodLength ,price,dp); + } + + return dp[ind][N] = max(take , notTake); +} + int cutRod(vector &price) { + // code here + + int n = price.size(); + + vector>dp (n , vector( n+1 , 0)); + + for(int N = 0 ; N<=n; N++){ + dp[0][N] = N * price[0]; + } + + for(int ind = 1 ; ind a; + string input; + getline(cin, input); + stringstream ss(input); + int number; + while (ss >> number) { + a.push_back(number); + } + + Solution ob; + + cout << ob.cutRod(a) << endl; + cout << "~" << endl; + } + return 0; +} +// } Driver Code Ends \ No newline at end of file diff --git a/DP/lec10_75.cpp b/DP/lec10_75.cpp new file mode 100644 index 0000000..53106b1 --- /dev/null +++ b/DP/lec10_75.cpp @@ -0,0 +1,87 @@ +//{ Driver Code Starts +// Initial Template for C++ + +#include +using namespace std; + + +// } Driver Code Ends + +// User function Template for C++ + +class Solution { + public: + +int f(int ind , int N , vector &price , vector>&dp){ + + if(ind == 0){ + return N * price[0]; + } + if(dp[ind][N]!=-1) return dp[ind][N]; + int notTake = 0 + f(ind-1 , N , price ,dp); + int take = INT_MIN; + + int rodLength = ind +1 ; + if( rodLength <=N){ + + take = price[ind] + f(ind , N- rodLength ,price,dp); + } + + return dp[ind][N] = max(take , notTake); +} + int cutRod(vector &price) { + // code here + + int n = price.size(); + + vector>dp (n , vector( n+1 , 0)); + + vector prev(n+1 , 0); + for(int N = 0 ; N<=n; N++){ + prev[N] = N * price[0]; + } + + for(int ind = 1 ; ind a; + string input; + getline(cin, input); + stringstream ss(input); + int number; + while (ss >> number) { + a.push_back(number); + } + + Solution ob; + + cout << ob.cutRod(a) << endl; + cout << "~" << endl; + } + return 0; +} +// } Driver Code Ends \ No newline at end of file diff --git a/DP/lec10_76.cpp b/DP/lec10_76.cpp new file mode 100644 index 0000000..fa22ac9 --- /dev/null +++ b/DP/lec10_76.cpp @@ -0,0 +1,21 @@ +class Solution { + public: + + int f(int i , int j , string &s , string &t,vector>&dp){ + + if(i == 0 || j== 0) return 0; + + if(dp[i][j]!=-1) return dp[i][j]; + if(s[i-1] == t[j-1]) return dp[i][j]= 1 + f(i-1 , j-1 , s , t,dp); + + return dp[i][j]=max(f(i-1,j , s , t,dp),f(i , j-1 , s , t,dp)); + } + int longestCommonSubsequence(string s, string t) { + + int n = s.size(); + int m = t.size(); + + vector>dp( n+1 , vector( m+1 ,-1)); + return f(n , m , s , t,dp); + } + }; \ No newline at end of file diff --git a/DP/lec10_77.cpp b/DP/lec10_77.cpp new file mode 100644 index 0000000..0e15fb2 --- /dev/null +++ b/DP/lec10_77.cpp @@ -0,0 +1,21 @@ +class Solution { + public: + + int f(int i , int j , string &s , string &t,vector>&dp){ + + if(i < 0 || j< 0) return 0; + + if(dp[i][j]!=-1) return dp[i][j]; + if(s[i] == t[j]) return dp[i][j]= 1 + f(i-1 , j-1 , s , t,dp); + + return dp[i][j]=max(f(i-1,j , s , t,dp),f(i , j-1 , s , t,dp)); + } + int longestCommonSubsequence(string s, string t) { + + int n = s.size(); + int m = t.size(); + + vector>dp( n , vector( m ,-1)); + return f(n-1 , m - 1 , s , t,dp); + } + }; \ No newline at end of file diff --git a/DP/lec10_78.cpp b/DP/lec10_78.cpp new file mode 100644 index 0000000..1b3f3ea --- /dev/null +++ b/DP/lec10_78.cpp @@ -0,0 +1,23 @@ +class Solution { + public: + + int longestCommonSubsequence(string s, string t) { + + int n = s.size(); + int m = t.size(); + + vector>dp( n+1 , vector( m+1 ,-1)); + + for(int j = 0 ; j<=m;j++) dp[0][j] = 0; + for(int i = 0 ; i<=n;i++) dp[i][0] = 0 ; + for(int i = 1 ;i<=n;i++){ + + for(int j = 1 ;j<=m;j++){ + if(s[i-1] == t[j-1]) dp[i][j]= 1 + dp[i-1 ][ j-1 ]; + else + dp[i][j]=max(dp[i-1][j],dp[i ][ j-1]); + } + } + return dp[n][m]; + } + }; \ No newline at end of file diff --git a/DP/lec10_79.cpp b/DP/lec10_79.cpp new file mode 100644 index 0000000..3d817a9 --- /dev/null +++ b/DP/lec10_79.cpp @@ -0,0 +1,24 @@ +class Solution { + public: + + int longestCommonSubsequence(string s, string t) { + + int n = s.size(); + int m = t.size(); + + vector>dp( n+1 , vector( m+1 ,-1)); + vector prev(m+1 , 0) , cur(m+1,0); + for(int j = 0 ; j<=m;j++) prev[j] = 0; + + for(int i = 1 ;i<=n;i++){ + + for(int j = 1 ;j<=m;j++){ + if(s[i-1] == t[j-1]) cur[j]= 1 + prev[ j-1 ]; + else + cur[j]=max(prev[j],cur[ j-1]); + } + prev = cur; + } + return prev[m]; + } + }; \ No newline at end of file diff --git a/DP/lec10_8.cpp b/DP/lec10_8.cpp new file mode 100644 index 0000000..6fe9777 --- /dev/null +++ b/DP/lec10_8.cpp @@ -0,0 +1,77 @@ +//{ Driver Code Starts +#include +using namespace std; + + +// } Driver Code Ends + +class Solution { + public: + + int solveUtil(int n ,vector& arr, vector&dp,int k) + { + + dp[0]=0; + + for(int i = 1;i=0) { + int jump = dp[i-j] + abs(arr[i] - arr[i-j]); + mmSteps = min(jump , mmSteps); + } + } + dp[i] = mmSteps; + } + return dp[n-1]; + } + int solve(int n , vector & arr, int k ) + { + vectordp(n , -1); + + return solveUtil(n,arr,dp,k); + } + int minimizeCost(int k, vector& arr) { + // Code here + + int n = arr.size(); + + vectordp(n , -1); + + return solve(n, arr, k); + } +}; + + +//{ Driver Code Starts. + +int main() { + string ts; + getline(cin, ts); + int t = stoi(ts); + while (t--) { + string ks; + getline(cin, ks); + int k = stoi(ks); + vector arr; + string input; + getline(cin, input); + stringstream ss(input); + int number; + while (ss >> number) { + arr.push_back(number); + } + Solution obj; + int res = obj.minimizeCost(k, arr); + cout << res << endl; + cout << "~" << endl; + // string tl; + // getline(cin, tl); + } + return 0; +} + +// } Driver Code Ends \ No newline at end of file diff --git a/DP/lec10_80.cpp b/DP/lec10_80.cpp new file mode 100644 index 0000000..2096e70 --- /dev/null +++ b/DP/lec10_80.cpp @@ -0,0 +1,125 @@ +//{ Driver Code Starts +#include +using namespace std; + + +// } Driver Code Ends + +class Solution { + + + public: + vector all_longest_common_subsequences(string s, string t) { + // Code here + int n = s.size(), m = t.size(); + + vector> dp(n+1, vector(m+1, 0)); // DP table initialized to 0 + + // Fill the DP table + for (int i = 1; i <= n; i++) { + for (int j = 1; j <= m; j++) { + if (s[i-1] == t[j-1]) + dp[i][j] = 1 + dp[i-1][j-1]; + else + dp[i][j] = max(dp[i-1][j], dp[i][j-1]); + } + } + int len = dp[n][m]; // Answer is in the last cell + + vector ans(len , '$') + + + int index = len -1; + int i = n , j = m; + while(i>0 && j>0){ + + if(s[i-1] == t[j-1]){ + ans[index] = s[i-1]; + index--; + i-- , j--; + } + else if(dp[i-1][j] >dp[i][j-1]){ + i--; + } + else j--; + } + + + return ans; + } +}; + +//{ Driver Code Starts +#include +using namespace std; + + +// } Driver Code Ends + +class Solution { +public: + vector all_longest_common_subsequences(string s1, string s2) { + int n1 = s1.size(), n2 = s2.size(); + + // Step 1: Create and Fill DP Table in Reverse Order + vector> dp(n1 + 1, vector(n2 + 1, 0)); + + for (int i = n1 - 1; i >= 0; i--) { + for (int j = n2 - 1; j >= 0; j--) { + if (s1[i] == s2[j]) + dp[i][j] = 1 + dp[i + 1][j + 1]; + else + dp[i][j] = max(dp[i + 1][j], dp[i][j + 1]); + } + } + + // Step 2: Backtracking with Memoization + unordered_set ans; // Using unordered_set for efficiency + unordered_set visited; // To avoid duplicate recursive calls + + function f = [&](int i, int j, string lcs) { + if (i == n1 || j == n2) { + ans.insert(lcs); // Store the LCS found + return; + } + + string key = to_string(i) + "," + to_string(j) + "," + lcs; + if (visited.count(key)) return; // Skip if already visited + visited.insert(key); + + if (s1[i] == s2[j]) { + f(i + 1, j + 1, lcs + s1[i]); + } else { + if (dp[i][j] == dp[i][j + 1]) f(i, j + 1, lcs); + if (dp[i][j] == dp[i + 1][j]) f(i + 1, j, lcs); + } + }; + + f(0, 0, ""); + + vector result(ans.begin(), ans.end()); + sort(result.begin(), result.end()); // Sort once at the end + return result; + } +}; + + + +//{ Driver Code Starts. +int main() { + int T; + cin >> T; + while (T--) { + string s, t; + cin >> s >> t; + Solution ob; + vector ans = ob.all_longest_common_subsequences(s, t); + for (auto i : ans) + cout << i << " "; + cout << "\n"; + cout << "~" << endl; + } + return 0; +} + +// } Driver Code Ends \ No newline at end of file diff --git a/DP/lec10_81.cpp b/DP/lec10_81.cpp new file mode 100644 index 0000000..98399d9 --- /dev/null +++ b/DP/lec10_81.cpp @@ -0,0 +1,51 @@ +//{ Driver Code Starts +#include +using namespace std; + + +// } Driver Code Ends + +class Solution { + public: + int longestCommonSubstr(string& s, string& t) { + // your code here + + int n = s.size(), m = t.size(); + + vector> dp(n+1, vector(m+1, 0)); // DP table initialized to 0 + + // Fill the DP table + int ans = 0 ; + for (int i = 1; i <= n; i++) { + for (int j = 1; j <= m; j++) { + if (s[i-1] == t[j-1]) { + dp[i][j] = 1 + dp[i-1][j-1]; + ans = max ( ans ,dp[i][j]);} + else { + dp[i][j] = 0; } + } + } + + return ans; + } +}; + + +//{ Driver Code Starts. + +int main() { + int t; + cin >> t; + while (t--) { + string s1, s2; + cin >> s1 >> s2; + Solution ob; + + cout << ob.longestCommonSubstr(s1, s2) << endl; + + cout << "~" + << "\n"; + } +} + +// } Driver Code Ends \ No newline at end of file diff --git a/DP/lec10_82.cpp b/DP/lec10_82.cpp new file mode 100644 index 0000000..88fb1be --- /dev/null +++ b/DP/lec10_82.cpp @@ -0,0 +1,53 @@ +//{ Driver Code Starts +#include +using namespace std; + + +// } Driver Code Ends + +class Solution { + public: + int longestCommonSubstr(string& s, string& t) { + // your code here + + int n = s.size(), m = t.size(); + + vector> dp(n+1, vector(m+1, 0)); // DP table initialized to 0 + + vector prev(m+1 ,0) , cur(m+1 ,0); + // Fill the DP table + int ans = 0 ; + for (int i = 1; i <= n; i++) { + for (int j = 1; j <= m; j++) { + if (s[i-1] == t[j-1]) { + cur[j] = 1 + prev[j-1]; + ans = max ( ans ,cur[j]);} + else { + cur[j] = 0; } + } + prev = cur; + } + + return ans; + } +}; + + +//{ Driver Code Starts. + +int main() { + int t; + cin >> t; + while (t--) { + string s1, s2; + cin >> s1 >> s2; + Solution ob; + + cout << ob.longestCommonSubstr(s1, s2) << endl; + + cout << "~" + << "\n"; + } +} + +// } Driver Code Ends \ No newline at end of file diff --git a/DP/lec10_83.cpp b/DP/lec10_83.cpp new file mode 100644 index 0000000..cd849a7 --- /dev/null +++ b/DP/lec10_83.cpp @@ -0,0 +1,29 @@ +class Solution { + public: + int longestCommonSubsequence(string s, string t) { + + int n = s.size(); + int m = t.size(); + + vector>dp( n+1 , vector( m+1 ,-1)); + + for(int j = 0 ; j<=m;j++) dp[0][j] = 0; + for(int i = 0 ; i<=n;i++) dp[i][0] = 0 ; + for(int i = 1 ;i<=n;i++){ + + for(int j = 1 ;j<=m;j++){ + if(s[i-1] == t[j-1]) dp[i][j]= 1 + dp[i-1 ][ j-1 ]; + else + dp[i][j]=max(dp[i-1][j],dp[i ][ j-1]); + } + } + return dp[n][m]; + } + int longestPalindromeSubseq(string s) { + + string t = s; + reverse(t.begin() , t.end()); + + return longestCommonSubsequence(s ,t); + } + }; \ No newline at end of file diff --git a/DP/lec10_84.cpp b/DP/lec10_84.cpp new file mode 100644 index 0000000..708105b --- /dev/null +++ b/DP/lec10_84.cpp @@ -0,0 +1,33 @@ +class Solution { + public: + int longestCommonSubsequence(string s, string t) { + + int n = s.size(); + int m = t.size(); + + vector>dp( n+1 , vector( m+1 ,-1)); + + for(int j = 0 ; j<=m;j++) dp[0][j] = 0; + for(int i = 0 ; i<=n;i++) dp[i][0] = 0 ; + for(int i = 1 ;i<=n;i++){ + + for(int j = 1 ;j<=m;j++){ + if(s[i-1] == t[j-1]) dp[i][j]= 1 + dp[i-1 ][ j-1 ]; + else + dp[i][j]=max(dp[i-1][j],dp[i ][ j-1]); + } + } + return dp[n][m]; + } + int longestPalindromeSubseq(string s) { + + string t = s; + reverse(t.begin() , t.end()); + + return longestCommonSubsequence(s ,t); + } + int minInsertions(string s) { + + return s.size() - longestPalindromeSubseq(s); + } + }; \ No newline at end of file diff --git a/DP/lec10_85.cpp b/DP/lec10_85.cpp new file mode 100644 index 0000000..c605253 --- /dev/null +++ b/DP/lec10_85.cpp @@ -0,0 +1,26 @@ +class Solution { + public: + int longestCommonSubsequence(string s, string t) { + + int n = s.size(); + int m = t.size(); + + vector>dp( n+1 , vector( m+1 ,-1)); + + for(int j = 0 ; j<=m;j++) dp[0][j] = 0; + for(int i = 0 ; i<=n;i++) dp[i][0] = 0 ; + for(int i = 1 ;i<=n;i++){ + + for(int j = 1 ;j<=m;j++){ + if(s[i-1] == t[j-1]) dp[i][j]= 1 + dp[i-1 ][ j-1 ]; + else + dp[i][j]=max(dp[i-1][j],dp[i ][ j-1]); + } + } + return dp[n][m]; + } + int minDistance(string word1, string word2) { + + return word1.size() + word2.size() - 2* longestCommonSubsequence(word1 , word2); + } + }; \ No newline at end of file diff --git a/DP/lec10_86.cpp b/DP/lec10_86.cpp new file mode 100644 index 0000000..4a3f188 --- /dev/null +++ b/DP/lec10_86.cpp @@ -0,0 +1,18 @@ +class Solution { + public: + int maxProfit(vector& prices) { + int mini = prices[0]; + int maxProfit = 0 ; + + int n = prices.size(); + + for(int i = 1 ; i& histo) { + stack st; + int maxA = 0; + int n = histo.size(); + + for(int i = 0; i <= n; i++) { + while (!st.empty() && (i == n || histo[st.top()] >= histo[i])) { + int height = histo[st.top()]; + st.pop(); + int width = st.empty() ? i : i - st.top() - 1; + maxA = max(maxA, width * height); + } + st.push(i); + } + return maxA; + } + + int maximalRectangle(vector>& mat) { + int n = mat.size(); + if (n == 0) return 0; // Edge case: empty matrix + int m = mat[0].size(); + int maxArea = 0; + vector height(m, 0); + + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + height[j] = (mat[i][j] == '1') ? height[j] + 1 : 0; // ✅ Fixed comparison + } + int area = largestRectangleArea(height); + maxArea = max(maxArea, area); + } + return maxArea; + } + }; + \ No newline at end of file diff --git a/DP/lec10_9.cpp b/DP/lec10_9.cpp new file mode 100644 index 0000000..88df83a --- /dev/null +++ b/DP/lec10_9.cpp @@ -0,0 +1,20 @@ +class Solution { + public: + int f(int ind , vector&nums) + { + if(ind == 0 ) return nums[ind]; + if(ind < 0) return 0 ; + + int pick = nums[ind] + f(ind - 2,nums); + + int notPick = 0 + f(ind -1 , nums); + + return max(pick , notPick); + } + int rob(vector& nums) { + + int n = nums.size(); + return f(n-1 , nums); + + } + }; \ No newline at end of file diff --git a/Matrix/lec2_SpiralMatrix.cpp b/Matrix/lec2_SpiralMatrix.cpp index ca0f2ac..decd7b9 100644 --- a/Matrix/lec2_SpiralMatrix.cpp +++ b/Matrix/lec2_SpiralMatrix.cpp @@ -1 +1,46 @@ -https://leetcode.com/problems/spiral-matrix/description/ \ No newline at end of file +https://leetcode.com/problems/spiral-matrix/description/ + +class Solution { + public: + vector spiralOrder(vector>& matrix) { + int top = 0; + int right = matrix[0].size() - 1; + int bottom = matrix.size() - 1; + int left = 0; + + vector ans; + + while (top <= bottom && left <= right) { + // Traverse from left to right + for (int j = left; j <= right; j++) { + ans.push_back(matrix[top][j]); + } + top++; + + // Traverse from top to bottom + for (int i = top; i <= bottom; i++) { + ans.push_back(matrix[i][right]); + } + right--; + + // Traverse from right to left (if there are remaining rows) + if (top <= bottom) { + for (int j = right; j >= left; j--) { + ans.push_back(matrix[bottom][j]); + } + bottom--; + } + + // Traverse from bottom to top (if there are remaining columns) + if (left <= right) { + for (int i = bottom; i >= top; i--) { + ans.push_back(matrix[i][left]); + } + left++; + } + } + + return ans; + } + }; + \ No newline at end of file diff --git a/Matrix/lec2_Transpose.cpp b/Matrix/lec2_Transpose.cpp index a666e96..27a8a7e 100644 --- a/Matrix/lec2_Transpose.cpp +++ b/Matrix/lec2_Transpose.cpp @@ -1 +1,24 @@ -https://www.geeksforgeeks.org/problems/transpose-of-matrix-1587115621/1?utm_source=geeksforgeeks&utm_medium=ml_article_practice_tab&utm_campaign=article_practice_tab \ No newline at end of file +https://www.geeksforgeeks.org/problems/transpose-of-matrix-1587115621/1?utm_source=geeksforgeeks&utm_medium=ml_article_practice_tab&utm_campaign=article_practice_tab + +//{ Driver Code Starts +#include +using namespace std; + + +// } Driver Code Ends +class Solution { + public: + // Function to find transpose of a matrix. + void transpose(vector>& mat, int n) { + // code here + + for(int i = 0 ; i +#include +using namespace std; + +int main() +{ + int n = 4; + vector> m(n, vector(n, 0)); + + int count=1; + cout<<"Given Matrix"<=0;i--) + { + cout< +using namespace std; + +int main() +{ + int n = 3; + vector >m ( n , vector(n,0)); + + int col = m.size(); + int row = m[0].size(); + + for(int i = 0 ; i + + +This repository contains **Data Structures and Algorithms (DSA) problems** solved in **C++**, sourced from **LeetCode, GeeksforGeeks, and InterviewBit**. +Ideal for **competitive programming**, **coding interviews**, and **DSA mastery**. + +- It tracks all the code and problem statement from the free DSA course at youtube by **Coder Army** via **Rohit Negi** +- Advanced topics Like DP are covered from **Take u forward** by **Striver** + +### Key Features: +- ✅ 100+ Handpicked Questions +- ✅ Topic-wise Categorization +- ✅ Solutions with Explanations +- ✅ Updated regulary. +- ✅ More than one approach to each problems + +![alt text](image.png) +Example for easy navigation : purple arrow pointing to "Size of Binary Tree" which when clicked , opens the solution of the problem. +Yellow arrow indicates "platform name" , which on clicking ; navigates you to the problem statement tab. ## Table of Contents 0. [Basic DSA /Math](#basic-dsa-/math-problems) 1. [Basic Array Problems](#basic-array-problems) @@ -19,6 +35,10 @@ This repository contains various Data Structures and Algorithms (DSA) topics imp 13. [Binary Tree](#binary-tree) 14. [Graph](#graph) 15. [Heap](#heap) +16. [Dynamic Programming](#dynamic-programming) +17. [Greedy](#greedy) + +## Bonus Section [SDE SHEET](#sde-sheet) ## Basic DSA /Math Problems $1$ [ArmStrongNO](./basic_prblmsDSA/armstrong.cpp) ; $2$ [BishopMoves](./basic_prblmsDSA/bishopmoves.cpp):- ***[IB](https://www.interviewbit.com/problems/total-moves-for-bishop/)*** ; $3$ [bitDiff](./basic_prblmsDSA/bitDiff.cpp):-***[gfg](https://www.geeksforgeeks.org/problems/count-total-set-bits-1587115620/1)*** $4$ [ComplimentNo](./basic_prblmsDSA/complimentOfNo.cpp):-***[LC](https://leetcode.com/problems/complement-of-base-10-integer/description/)*** ; $5$ [DistributeCircle](./basic_prblmsDSA/distributeInCircle.cpp):-***[IB](https://www.interviewbit.com/problems/distribute-in-circle/)*** ; $6$ [NimGame](./basic_prblmsDSA/NimGame.cpp):-***[LC](https://leetcode.com/problems/nim-game/)*** ; $7$ [AddDigits](./basic_prblmsDSA/p1_AddDigits.cpp):-***[LC](https://leetcode.com/problems/add-digits/description/)*** ; $8$ [LeapYear](./basic_prblmsDSA/p2_LeapYear.cpp):-***[gfg](https://www.geeksforgeeks.org/problems/leap-year0943/1?utm_source=geeksforgeeks&utm_medium=ml_article_practice_tab&utm_campaign=article_practice_tab)*** ; $9$ [RevInt](./basic_prblmsDSA/p3_ReverseInteger.cpp):-***[gfg](https://leetcode.com/problems/reverse-integer/description/)*** ; $10$ [PowerOf2](./basic_prblmsDSA/p4_PowerOfTwo.cpp):- ***[LC](https://leetcode.com/problems/power-of-two/description/)*** ; $11$ [PalandromeNo](./basic_prblmsDSA/palandrone.cpp):-***[LC](https://leetcode.com/problems/palindrome-number/description/)*** ; $12$ [CheckRectangle](./basic_prblmsDSA/rectangleOrnot.cpp):- ***[IB](https://www.interviewbit.com/problems/is-rectangle/)*** ; $13$ [SetBits](./basic_prblmsDSA/setBits.cpp):-***[gfg](https://www.geeksforgeeks.org/problems/set-bits0143/1)*** ; $14$ [NbyNChessBoard](./basic_prblmsDSA/sqrNbyNchessBoard.cpp):- ***[gfg](https://www.geeksforgeeks.org/problems/squares-in-nn-chessboard1801/1?page=1&difficulty%5B%5D=-1&category%5B%5D=Numbers&sortBy=submissions)*** ; $15$ [Sqrt](./basic_prblmsDSA/sqrt.cpp):- ***[LC](https://leetcode.com/problems/sqrtx/description/ )*** ; $16$ [TotalSetBits](./basic_prblmsDSA/totalSetBits.cpp):- ***[gfg](https://www.geeksforgeeks.org/problems/count-total-set-bits-1587115620/1)*** ; $17$ [TralingZeroesFact](./basic_prblmsDSA/trailingzero.cpp):- ***[gfg](https://www.geeksforgeeks.org/problems/trailing-zeroes-in-factorial5134/1)*** ; $18$ [uglyNo](./basic_prblmsDSA/uglyNo.cpp):- ***[LC](https://leetcode.com/problems/ugly-number/)*** @@ -113,7 +133,7 @@ Reverse each row of matrix - 2D Array Interview Problems - [Transpose](./Matrix/lec2_Transpose.cpp) :- ***[gfg](https://www.geeksforgeeks.org/problems/transpose-of-matrix-1587115621/1?utm_source=geeksforgeeks&utm_medium=ml_article_practice_tab&utm_campaign=article_practice_tab)*** - - [SpiralMatrix](./Matrix/lec2_SpiralMatrix.cpp) :- ***[Leetcode](./Matrix/lec2_SpiralMatrix.cpp)*** + - [SpiralMatrix](./Matrix/lec2_SpiralMatrix.cpp) :- ***[Leetcode](https://leetcode.com/problems/spiral-matrix/description/)*** - [WaveFormI](./Matrix/lec2_WaveFormI.cpp) - [RevCol](./Matrix/lec2_revcol.cpp) - [SpiralMatrixII](./Matrix/lec2_SpiralMatrixII.cpp) :- ***[LeetCode](https://leetcode.com/problems/spiral-matrix-ii/description/)*** @@ -332,7 +352,120 @@ All about objects and classes - [Max Heap](./Heap/lec9_2.cpp) - [BuildMaxHeap](./Heap/lec9_3.cpp) - [Priority Queue](./Heap/lec9_4.cpp) - + + +## Greedy +- Fractional Knapsack Problem [Approach](./Greedy/lec1_1.cpp) :- ***[gfg](https://www.geeksforgeeks.org/fractional-knapsack-problem/1?utm_source=geeksforgeeks&utm_medium=ml_article_practice_tab&utm_campaign=article_practice_tab)*** +## Dynamic Programming + +### Introduction to DP + - fibonacci [solution](./DP/lec10_1.cpp) :- ***[gfg](https://www.geeksforgeeks.org/problems/introduction-to-dp/1?utm_source=youtube&utm_medium=collab_striver_ytdescription&utm_campaign=introduction-to-dp)*** + +### 1-D DP +- climbing stairs [solution](./DP/lec10_2.cpp) :- ***[leetcode](https://leetcode.com/problems/climbing-stairs/description/)*** +- Frog Jump(DP-3) [RecursiveSoln](./DP/lec10_3.cpp) ; [DPsolution](./DP/lec10_4.cpp) ; [Tablesolution](./DP/lec10_5.cpp) ; [SpaceOptimisedsolution](./DP/lec10_6.cpp) ; :- ***[Gfg](https://www.geeksforgeeks.org/problems/geek-jump/1?utm_source=youtube&utm_medium=collab_striver_ytdescription&utm_campaign=geek-jump)*** +- Frog Jump with k distances(DP-4) [DPsolution](./DP/lec10_7.cpp) ; [Tabulation](./DP/lec10_8.cpp):- ***[Gfg](https://www.geeksforgeeks.org/problems/minimal-cost/1?utm_source=youtube&utm_medium=collab_striver_ytdescription&utm_campaign=minimal-cost)*** +- Maximum sum of non-adjacent elements (DP 5) [Recursive](./DP/lec10_9.cpp); [DP](./DP/lec10_10.cpp) ; [Tabulation](./DP/lec10_11.cpp) :- ***[LC](https://leetcode.com/problems/house-robber/)*** +- House Robber (DP 6) [Solution](./DP/lec10_12.cpp) :- ***[LC](https://leetcode.com/problems/house-robber-ii/submissions/1566948933/)*** + +### 2-D DP / Grid +- Ninja's Training (DP 7) [Recursive](./DP/lec10_13.cpp); [DP](./DP/lec10_14.cpp) ; [Tabulation](./DP/lec10_15.cpp); [SpaceOptimsed](./dp/lec10_16.cpp):- ***[Gfg](https://www.geeksforgeeks.org/problems/geeks-training/1?utm_source=youtube&utm_medium=collab_striver_ytdescription&utm_campaign=geeks-training)*** +- Grid Unique Paths : DP on Grids (DP8) [Recursive](./DP/lec10_17.cpp); [DP](./DP/lec10_18.cpp) ; [Tabulation](./DP/lec10_19.cpp); [SpaceOptimsed](./dp/lec10_20.cpp):- ***[LC](https://leetcode.com/problems/unique-paths/)*** : alternative solution : [yt](https://www.youtube.com/watch?v=t_f0nwwdg5o&t=0s) +- Grid Unique Paths 2 (DP 9) [DP](./DP/lec10_21.cpp) ; [Tabulation](./DP/lec10_22.cpp); [SpaceOptimised](./DP/lec10_23.cpp) :- ***[LC](https://leetcode.com/problems/unique-paths-ii/)*** +- Minimum path sum in Grid (DP 10) [Recursive](./DP/lec10_24.cpp); [DP](./DP/lec10_25.cpp) ; [Tabulation](./DP/lec10_26.cpp); [SpaceOptimsed](./dp/lec10_27.cpp):- ***[LC](https://leetcode.com/problems/minimum-path-sum/)*** +- Minimum path sum in Triangular Grid (DP 11) [Recursive](./DP/lec10_28.cpp); [DP](./DP/lec10_29.cpp) ; [Tabulation](./DP/lec10_30.cpp); [SpaceOptimsed](./dp/lec10_31.cpp):- ***[LC](https://leetcode.com/problems/triangle/)*** +- Minimum/Maximum Falling Path Sum (DP-12) [Recursive](./DP/lec10_32.cpp); [DP](./DP/lec10_33.cpp) ; [Tabulation](./DP/lec10_34.cpp); [SpaceOptimsed](./dp/lec10_35.cpp):- ***[LC](https://leetcode.com/problems/minimum-falling-path-sum/)*** +- 3-d DP : Ninja and his friends (DP-13) [Recursive](./DP/lec10_36.cpp); [DP](./DP/lec10_37.cpp) ; [Tabulation](./DP/lec10_38.cpp); [SpaceOptimsed](./dp/lec10_39.cpp):- ***[Gfg](https://www.geeksforgeeks.org/problems/chocolates-pickup/1?utm_source=youtube&utm_medium=collab_striver_ytdescription&utm_campaign=chocolates-pickup)*** + + +### DP on subsequences +- Subset sum equal to target (DP- 14) [Recursive](./DP/lec10_40.cpp); [DP](./DP/lec10_41.cpp) ; [Tabulation](./DP/lec10_42.cpp); [SpaceOptimsed](./dp/lec10_43.cpp):- ***[Gfg](https://www.geeksforgeeks.org/problems/subset-sum-problem-1611555638/1)*** + +- Partition Equal Subset Sum (DP- 15) [Tabulation](./DP/lec10_44.cpp) :- ***[LC](https://leetcode.com/problems/partition-equal-subset-sum/)*** + +- Partition Set Into 2 Subsets With Min Absolute Sum Diff (DP- 16) +[Tabulation](./DP/lec10_45.cpp) ; [Alternative](./DP/lec10_46.cpp):- ***[LC](https://leetcode.com/problems/partition-array-into-two-arrays-to-minimize-sum-difference/description/)*** + +- Count Subsets with Sum K (DP - 17) [DP](./DP/lec10_47.cpp) ; [Tabulation](./DP/lec10_48.cpp) ; [spaceOptimised](./DP/lec10_49.cpp) :- ***[Gfg](https://www.geeksforgeeks.org/problems/perfect-sum-problem5633/1?utm_source=youtube&utm_medium=collab_striver_ytdescription&utm_campaign=perfect-sum-problem)*** +- Count Partitions with Given Difference (DP - 18) [Recursion](./DP/lec10_50.cpp) ; [DP](./DP/lec10_51.cpp) ; [Tabulation](./DP/lec10_52.cpp) ; [SpaceOptimised](./DP/lec10_53.cpp) :- ***[LC](https://leetcode.com/problems/partition-equal-subset-sum/)*** + +- 0/1 Knapsack (DP - 19) [Recursive](./DP/lec10_54.cpp) ; [DP](./DP/lec10_55.cpp) ; [Tabulation](./DP/lec10_56.cpp) ; [SpaceOptimised](./DP/lec10_57.cpp) :- ***[CN](https://www.naukri.com/code360/problems/0-1-knapsack_920542?source=youtube&campaign=striver_dp_videos&utm_source=youtube&utm_medium=affiliate&utm_campaign=striver_dp_videos&leftPanelTabValue=PROBLEM)*** +- Assign Cookies [Solution](./DP/lec10_58.cpp) :- ***[LC](https://leetcode.com/problems/assign-cookies/description/)*** +- Minimum Coins (DP - 20) [Recursive](./DP/lec10_59.cpp) ; [DP](./DP/lec10_60.cpp) ; [Tabulation](./DP/lec10_61.cpp) ; [SpaceOptimised](./DP/lec10_62.cpp) :- ***[LC](https://leetcode.com/problems/coin-change/)*** +- Target Sum (DP - 21) [Tabulation](./DP/lec10_63.cpp) :- ***[LC](https://leetcode.com/problems/target-sum/)*** +- Coin Change 2 (DP - 22) [Recursive](./DP/lec10_64.cpp) ; [DP](./DP/lec10_65.cpp) ; [Tabulation](./DP/lec10_66.cpp) ; [SpaceOptimised](./DP/lec10_67.cpp) :- ***[LC](https://leetcode.com/problems/coin-change-2/)*** +- Unbounded Knapsack (DP - 23) [Recursive](./DP/lec10_68.cpp) ; [DP](./DP/lec10_69.cpp) ; [Tabulation](./DP/lec10_70.cpp) ; [SpaceOptimised](./DP/lec10_71.cpp) :- ***[Gfg](https://www.geeksforgeeks.org/problems/knapsack-with-duplicate-items4201/1?utm_source=youtube&utm_medium=collab_striver_ytdescription&utm_campaign=knapsack-with-duplicate-items)*** +- Rod Cutting (DP - 24) [Recursive](./DP/lec10_72.cpp) ; [DP](./DP/lec10_73.cpp) ; [Tabulation](./DP/lec10_74.cpp) ; [SpaceOptimised](./DP/lec10_75.cpp) :- ***[Gfg](https://www.geeksforgeeks.org/problems/cutted-segments/1?utm_source=youtube&utm_medium=collab_striver_ytdescription&utm_campaign=cutted-segments)*** + + +### DP on strings +- Longest Common Subsequence (DP - 25) [WithShiftedIndex](./DP/lec10_76.cpp) ; [DP](./DP/lec10_77.cpp) ; [Tabulation](./DP/lec10_78.cpp) ; [SpaceOptimised](./DP/lec10_79.cpp) :- ***[LC](https://leetcode.com/problems/longest-common-subsequence/)*** +- Print Longest Common Subsequence | (DP - 26) [Tabulation](./DP/lec10_80.cpp) :- ***[Gfg](https://www.geeksforgeeks.org/problems/print-longest-common-subsequence/1?utm_source=youtube&utm_medium=collab_striver_ytdescription&utm_campaign=print-longest-common-subsequence)*** +- Longest Common Substring | (DP - 27) [Tabulation](./DP/lec10_81.cpp) ; [spaceoptimised](./DP/lec10_82.cpp):- ***[Gfg](https://www.geeksforgeeks.org/problems/longest-common-substring/1?utm_source=youtube&utm_medium=collab_striver_ytdescription&utm_campaign=longest-common-substring)*** +- Longest Palindromic Subsequence | (DP-28) [Tabulation](./DP/lec10_83.cpp) :- ***[LC](https://leetcode.com/problems/longest-palindromic-subsequence/)*** +- Minimum insertions to make string palindrome | DP-29 [Tabulation](./DP/lec10_84.cpp) :- ***[LC](https://leetcode.com/problems/minimum-insertion-steps-to-make-a-string-palindrome/submissions/1574579741/)*** +- Minimum Insertions/Deletions to Convert String | (DP- 30) [Tabulation](./DP/lec10_85.cpp) :- ***[LC](https://leetcode.com/problems/delete-operation-for-two-strings/)*** + +### DP on Stocks +- Best Time to Buy and Sell Stock |(DP-35) [DP](./DP/lec10_86.cpp) :- ***[LC](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/)*** + +### DP on Squrares + +- Maximum Rectangle Area with all 1's|(DP-55) + - prereq : [Largest Rectangle in Histogram](./stack/lec6_24.cpp) :- ***[LC](https://leetcode.com/problems/maximal-rectangle/)*** + - [DP](./DP/lec10_87.cpp) :- ***[LC](https://leetcode.com/problems/maximal-square/)*** +- Count Square Submatrices with All Ones|(DP-56) + + + + + +## SDE Sheet +### Arrays +- pascal tree [solution](./sde_sheet/lec_1.cpp) :- ***[LC](https://leetcode.com/problems/pascals-triangle-ii/description/)*** +- pascal tree ii [solution](./sde_sheet/lec_2.cpp) :- ***[LC](https://leetcode.com/problems/pascals-triangle-ii/description/)*** +- Set Matrix Zeros [solution](./sde_sheet/lec_3.cpp) :- ***[LC](https://leetcode.com/problems/set-matrix-zeroes/description/)*** +- next permutaion [solution](./sde_sheet/lec_4.cpp) :- ***[LC](https://leetcode.com/problems/next-permutation/description/)*** +- Kadane's Algorithm [solution](./sde_sheet/lec_7.cpp) :- ***[LC](https://leetcode.com/problems/maximum-subarray/description/)*** +- Sort an array of 0's, 1's and 2's [solution](./sde_sheet/lec_8.cpp) :- ***[LC](https://leetcode.com/problems/sort-colors/description/)*** +### Arrays 2 +- transpose a matrix [solution](./sde_sheet/lec_5.cpp) :- ***[LC](https://leetcode.com/problems/transpose-matrix/description/)*** +- rotate a matrix [solution](./sde_sheet/lec_6.cpp) :- ***[LC](https://leetcode.com/problems/rotate-image/description/)*** +- Merge two sorted arrays without extra space [solution](./sde_sheet/lec_9.cpp) :- ***[LC](https://leetcode.com/problems/merge-sorted-array/description/)*** +- Find the duplicate in an array of N+1 integers [solution](./sde_sheet/lec_10.cpp) :- ***[LC](https://leetcode.com/problems/find-the-duplicate-number/description/)*** + + +### Array 3 +- Search in a 2 D matrix [solution](./Matrix/lec4_Search2DMatrix.cpp) :- ***[LC](https://leetcode.com/problems/search-a-2d-matrix/description/)*** +- Pow(x, n) [solution](./sde_sheet/lec_11.cpp) :- ***[LC](https://leetcode.com/problems/powx-n/description/)*** +- Majority Element (>n/2 times) [solution](./sde_sheet/lec_12.cpp) :- ***[LC](https://leetcode.com/problems/majority-element/description/)*** +- Majority Element (n/3 times) [solution](./sde_sheet/lec_13.cpp) :- ***[LC](https://leetcode.com/problems/majority-element-ii/description/)*** +### Array 4 +- 2 sum [solution](./sde_sheet/lec_14.cpp) :- ***[LC](https://leetcode.com/problems/two-sum/description/)*** +- 3 sum [solution](./sde_sheet/lec_15.cpp) :- ***[LC](https://leetcode.com/problems/3sum/description/)*** +- 4 sum [solution](./sde_sheet/lec_16.cpp) :- ***[LC](https://leetcode.com/problems/4sum/description/)*** +- Longest Consecutive Sequence [solution](./sde_sheet/lec_17.cpp) :- ***[LC](https://leetcode.com/problems/longest-consecutive-sequence/description/)*** +- Largest Subarray with K sum [solution](./sde_sheet/lec_18.cpp) :- ***[LC](https://leetcode.com/problems/maximum-size-subarray-sum-equals-k/description/)*** +- Largest Subarray with K sum [solution](./sde_sheet/lec_19.cpp) :- ***[LC](https://leetcode.com/problems/longest-subarray-with-absolute-diff-less-than-or-equal-to-limit/description/)*** +- Count number of subarrays with given xor K [solution](./sde_sheet/lec_20.cpp) :- ***[LC](https://leetcode.com/problems/subarray-sum-equals-k/description/)*** +- Longest Substring without repeating characters [solution](./sde_sheet/lec_21.cpp) :- ***[LC](https://leetcode.com/problems/longest-substring-without-repeating-characters/description/)*** + + +### Linked list 1 +- Merge two sorted Linked List (use method used in mergeSort) [solution](./sde_sheet/lec_22.cpp) :- ***[LC](https://leetcode.com/problems/merge-two-sorted-lists/description/)*** +- add two numbers [solution](./sde_sheet/lec_23.cpp) :- ***[LC](https://leetcode.com/problems/add-two-numbers/description/)*** +- Delete a given Node when a node is given.(0(1) solution) [solution](./sde_sheet/lec_24.cpp) :- ***[LC](https://leetcode.com/problems/delete-node-in-a-linked-list/description/)*** + +### Linked list 2 +- Find intersection point of Y LinkedList [solution](./sde_sheet/lec_25.cpp) :- ***[LC](https://leetcode.com/problems/intersection-of-two-linked-lists/description/)*** +- Detect a cycle in a linked list [solution](./sde_sheet/lec_26.cpp) :- ***[LC](https://leetcode.com/problems/linked-list-cycle/description/)*** +- Find the starting point of the Loop of LinkedList [solution](./sde_sheet/lec_27.cpp) :- ***[LC](https://leetcode.com/problems/linked-list-cycle-ii/description/)*** +- + +### Linked list and array +- Max consecutive ones [solution](./sde_sheet/lec_28.cpp) :- ***[LC](https://leetcode.com/problems/max-consecutive-ones/description/)*** +- Remove Duplicate from Sorted array [solution](./sde_sheet/lec_29.cpp) :- ***[LC](https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/)*** + ## YouTube Channel Check out the accompanying YouTube channel for video explanations and tutorials: [Coder Army](https://youtube.com/@CoderArmy9?si=vc7X_Fis2kOgsE7i) @@ -340,7 +473,6 @@ All about objects and classes ## Contributing Feel free to contribute by adding new problems, improving existing solutions, or suggesting enhancements. Follow the [contribution guidelines](./CONTRIBUTING.md) for more details. - ## License This project is licensed under the [MIT License](./LICENSE). diff --git a/greedy/lec1_1.cpp b/greedy/lec1_1.cpp new file mode 100644 index 0000000..e69de29 diff --git a/image.png b/image.png new file mode 100644 index 0000000..0e2fb9b Binary files /dev/null and b/image.png differ diff --git a/sde_sheet/lec_1.cpp b/sde_sheet/lec_1.cpp new file mode 100644 index 0000000..fb67356 --- /dev/null +++ b/sde_sheet/lec_1.cpp @@ -0,0 +1,26 @@ +class Solution { + public: + vector getRow(int n) { + vector ansE(n + 1, 1); // Initialize with 1's, size n+1 + long long ans = 1; + + // Printing the rest of the part and filling ansE + for (int i = 1; i <= n / 2; i++) { + ans = ans * (n - i + 1) / i; + ansE[i] = ans; // Set the ith value + ansE[n - i] = ans; // Set the symmetric value + } + + return ansE; + } + vector> generate(int numRows) { + + vector>ansRow; + + for(int i = 0 ;i& nums) { + int n = nums.size(); + + sort(nums.begin() , nums.end()); + + for(int i = 0 ; i& arr) { + int n = arr.size(); + + vectorfreq(n+1 ,0); + + for(int i = 0 ;i& nums) { + int slow = nums[0]; + int fast = nums[0]; + + do{ + slow = nums[slow]; + fast = nums[nums[fast]]; + }while(slow!= fast); + + fast = nums[0]; + while(slow!=fast){ + slow = nums[slow]; + fast = nums[fast]; + } + + return slow; + } + }; \ No newline at end of file diff --git a/sde_sheet/lec_11.cpp b/sde_sheet/lec_11.cpp new file mode 100644 index 0000000..89f5d7a --- /dev/null +++ b/sde_sheet/lec_11.cpp @@ -0,0 +1,52 @@ +class Solution { + public: + double myPow(double x, int n) { + double ans = 1.0; + bool isNegative = (n < 0); // Check if exponent is negative + long long N = abs((long long)n); // Convert to positive to avoid overflow + + for (long long i = 0; i < N; i++) { + ans = ans * x; + } + + if (isNegative) { + return 1.0 / ans; + } + return ans; + } + }; + +// alternative solution +class Solution { + public: + double myPow(double x, int n) { + double ans = 1.0; + double oriNum = n; + + if( x == 0 || x == 1){ + return x; + } + + if(n<0){ + x = 1/x; + n = -(n +1); + ans = ans*x; + } + + while(n>0){ + + if(n%2==1){ + ans = ans*x; + n = n-1; + } + + else{ + n = n/2; + x = x*x; + + } + } + + return ans; + } + }; \ No newline at end of file diff --git a/sde_sheet/lec_12.cpp b/sde_sheet/lec_12.cpp new file mode 100644 index 0000000..4cad6f1 --- /dev/null +++ b/sde_sheet/lec_12.cpp @@ -0,0 +1,71 @@ +class Solution { + public: + int majorityElement(vector& nums) { + + int n = nums.size(); + + for(int i = 0 ;in/2){ + return nums[i]; + } + } + return -1; + } + }; + +// alternative solution +class Solution { + public: + int majorityElement(vector& nums) { + + int n = nums.size(); + + mapmpp; + + for(int i = 0 ;i (n/2)){ + return it.first; + } + } + return -1; + } + }; + +// alternative solution +class Solution { + public: + int majorityElement(vector& nums) { + + int n = nums.size(); + + int cnt = 0 ; + int el ; + + for(int i = 0 ;i(n/2) ) return el; + return -1; + } + }; \ No newline at end of file diff --git a/sde_sheet/lec_13.cpp b/sde_sheet/lec_13.cpp new file mode 100644 index 0000000..d682ffa --- /dev/null +++ b/sde_sheet/lec_13.cpp @@ -0,0 +1,104 @@ +class Solution { + public: + vector majorityElement(vector& v) { + + int n = v.size(); + + vector ls; + + mapmpp; + + int mini = int (n/3) +1; + + for(int i = 0 ;i majorityElement(vector& v) { + int n = v.size(); //size of the array + vector ls; // list of answers + + for (int i = 0; i < n; i++) { + //selected element is v[i]: + // Checking if v[i] is not already + // a part of the answer: + if (ls.size() == 0 || ls[0] != v[i]) { + int cnt = 0; + for (int j = 0; j < n; j++) { + // counting the frequency of v[i] + if (v[j] == v[i]) { + cnt++; + } + } + + // check if frquency is greater than n/3: + if (cnt > (n / 3)) + ls.push_back(v[i]); + } + + if (ls.size() == 2) break; + } + + return ls; + } + }; + +// alternative solution +class Solution { + public: + vector majorityElement(vector& v) { + + int n = v.size(); + + int cnt1 = 0 , cnt2 = 0 ; + int el1 = INT_MIN; + int el2 = INT_MIN; + + for(int i = 0 ; ils; + + cnt1 = 0 , cnt2 = 0 ; + + for(int i = 0 ;i=mini) ls.push_back(el1); + if(cnt2 >= mini) ls.push_back(el2); + + return ls; + } + }; \ No newline at end of file diff --git a/sde_sheet/lec_14.cpp b/sde_sheet/lec_14.cpp new file mode 100644 index 0000000..8004138 --- /dev/null +++ b/sde_sheet/lec_14.cpp @@ -0,0 +1,64 @@ +class Solution { + public: + vector twoSum(vector& nums, int target) { + int n = nums.size(); + + vectorans(2,-1); + for(int i = 0 ; i< n ; i++) + { + + + for(int j = i+1 ; j +using namespace std; + +vector twoSum(int n, vector &arr, int target) { + unordered_map mpp; + for (int i = 0; i < n; i++) { + int num = arr[i]; + int moreNeeded = target - num; + if (mpp.find(moreNeeded) != mpp.end()) { + return {mpp[moreNeeded], i}; + } + mpp[num] = i; + } + return { -1, -1}; +} + +// two pointer +class Solution { + public: + vector twoSum(vector& arr, int target) { + int n = arr.size(); + sort(arr.begin() , arr.end()); + int left = 0 , right = n-1; + while(left > threeSum(vector& v) { + int n = v.size(); + + set>st; + + for(int i = 0 ;i temp = {v[i] ,v[j] ,v[k] }; + sort(temp.begin() , temp.end()); + st.insert(temp); + } + } + } + } + + vector> ans(st.begin() , st.end()); + return ans; + } + }; + +// +class Solution { + public: + vector> threeSum(vector& arr) { + int n = arr.size(); + + set> st; + + for (int i = 0; i < n; i++) { + set hashset; + for (int j = i + 1; j < n; j++) { + //Calculate the 3rd element: + int third = -(arr[i] + arr[j]); + + //Find the element in the set: + if (hashset.find(third) != hashset.end()) { + vector temp = {arr[i], arr[j], third}; + sort(temp.begin(), temp.end()); + st.insert(temp); + } + hashset.insert(arr[j]); + } + } + + //store the set in the answer: + vector> ans(st.begin(), st.end()); + return ans; + } + }; + +// pointer approach +class Solution { + public: + vector> threeSum(vector& arr) { + int n = arr.size(); + + vector>ans; + sort(arr.begin() , arr.end()); + + for(int i = 0 ; i0){ + k--; + } + else{ + + vectortemp = {arr[i] ,arr[j],arr[k]}; + ans.push_back(temp); + j++; + k--; + + while(j getRow(int n) { + vector ansE(n + 1, 1); // Initialize with 1's, size n+1 + long long ans = 1; + + // Printing the rest of the part and filling ansE + for (int i = 1; i <= n / 2; i++) { + ans = ans * (n - i + 1) / i; + ansE[i] = ans; // Set the ith value + ansE[n - i] = ans; // Set the symmetric value + } + + return ansE; + } + }; + \ No newline at end of file diff --git a/sde_sheet/lec_22.cpp b/sde_sheet/lec_22.cpp new file mode 100644 index 0000000..d20dc1a --- /dev/null +++ b/sde_sheet/lec_22.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* mergeTwoLists(ListNode* list1, ListNode* list2) { + + ListNode * dummyNode = new ListNode(-1); + ListNode * temp = dummyNode; + + while(list1 != nullptr && list2!= nullptr){ + + if(list1->val <= list2->val){ + temp->next = list1; + list1 = list1->next; + }else{ + temp->next = list2; + list2 = list2->next; + } + + temp = temp->next; + } + + if(list1!= nullptr){ + temp->next = list1; + }else{ + temp->next = list2; + } + + return dummyNode->next; + } + }; \ No newline at end of file diff --git a/sde_sheet/lec_23.cpp b/sde_sheet/lec_23.cpp new file mode 100644 index 0000000..2139492 --- /dev/null +++ b/sde_sheet/lec_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* addTwoNumbers(ListNode* l1, ListNode* l2) { + ListNode *dummy = new ListNode(); + + ListNode *temp = dummy; + int carry = 0 ; + + while((l1!=NULL || l2!=NULL)|| carry){ + + int sum = 0; + if(l1!=NULL){ + sum+=l1->val; + l1 = l1->next; + } + if(l2!=NULL){ + sum+=l2->val; + l2 = l2->next; + } + + sum+=carry; + carry = sum/10; + + ListNode *node = new ListNode(sum%10); + temp ->next = node; + temp = temp->next; + } + + return dummy->next; + } + }; \ No newline at end of file diff --git a/sde_sheet/lec_24.cpp b/sde_sheet/lec_24.cpp new file mode 100644 index 0000000..be252ba --- /dev/null +++ b/sde_sheet/lec_24.cpp @@ -0,0 +1,15 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode(int x) : val(x), next(NULL) {} + * }; + */ +class Solution { + public: + void deleteNode(ListNode* t) { + t->val = t->next->val; + t->next = t->next->next; + } + }; \ No newline at end of file diff --git a/sde_sheet/lec_25.cpp b/sde_sheet/lec_25.cpp new file mode 100644 index 0000000..bd1f2a4 --- /dev/null +++ b/sde_sheet/lec_25.cpp @@ -0,0 +1,49 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode(int x) : val(x), next(NULL) {} + * }; + */ +class Solution { + public: + ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { + while(headB != NULL){ + ListNode * temp = headA ; + while(temp!= NULL){ + + if(temp == headB) return headB; + + temp = temp->next; + } + + headB = headB->next; + } + + return NULL; + } + }; + + +// alternative +class Solution { + public: + ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { + + unordered_setst; + + while(headA != NULL){ + st.insert(headA); + headA = headA->next; + } + while(headB!=NULL){ + if(st.find(headB)!= st.end()) return headB; + headB = headB->next; + } + + return NULL; + } + }; + +// alternative \ No newline at end of file diff --git a/sde_sheet/lec_26.cpp b/sde_sheet/lec_26.cpp new file mode 100644 index 0000000..c88ee5f --- /dev/null +++ b/sde_sheet/lec_26.cpp @@ -0,0 +1,53 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode(int x) : val(x), next(NULL) {} + * }; + */ +class Solution { + public: + bool hasCycle(ListNode *head) { + ListNode *temp = head; + unordered_mapnodeMap; + + while(temp!=NULL){ + if(nodeMap.find(temp)!=nodeMap.end()){ + return true; + } + + nodeMap[temp] =1; + + temp=temp->next; + } + + return false; + + } + }; + +// alternative +class Solution { + public: + bool hasCycle(ListNode *head) { + ListNode *slow = head; + ListNode *fast = head; + if(head== NULL) return false; + + while(fast != NULL && fast->next != NULL){ + + slow = slow->next; + + fast=fast->next->next; + + if(slow == fast){ + return true; + } + } + + + return false; + + } + }; \ No newline at end of file diff --git a/sde_sheet/lec_27.cpp b/sde_sheet/lec_27.cpp new file mode 100644 index 0000000..fc304cf --- /dev/null +++ b/sde_sheet/lec_27.cpp @@ -0,0 +1,97 @@ +class Solution { + public: + bool hasCycle(ListNode *head) { + ListNode *slow = head; + ListNode *fast = head; + if(head== NULL) return false; + + while(fast != NULL && fast->next != NULL){ + + slow = slow->next; + + fast=fast->next->next; + + if(slow == fast){ + return true; + } + } + + + return false; + + } + }; + +// atlernative + +/** + * 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) { + + ListNode * slow = head; + ListNode * fast = head; + + while(fast !=NULL && fast->next !=NULL){ + + slow = slow->next; + + fast = fast->next->next; + + + if(slow == fast){ + + slow = head; + + + while( slow!=fast){ + slow = slow->next; + fast = fast->next; + } + + return slow; + } + } + + + return NULL; + } + }; + + +// alternative +int removeDuplicates(int arr[], int n) { + set < int > set; + for (int i = 0; i < n; i++) { + set.insert(arr[i]); + } + int k = set.size(); + int j = 0; + for (int x: set) { + arr[j++] = x; + } + return k; + } +// alternative +class Solution { + public: + int removeDuplicates(vector& arr) { + int n = arr.size(); + + int i = 0; + for (int j = 1; j < n; j++) { + if (arr[i] != arr[j]) { + i++; + arr[i] = arr[j]; + } + } + return i + 1; + } + }; \ No newline at end of file diff --git a/sde_sheet/lec_28.cpp b/sde_sheet/lec_28.cpp new file mode 100644 index 0000000..6570b13 --- /dev/null +++ b/sde_sheet/lec_28.cpp @@ -0,0 +1,21 @@ +class Solution { + public: + int findMaxConsecutiveOnes(vector& arr) { + int maxi = 0; + + int n = arr.size(); + + int count = 0 ; + for(int i = 0 ;i& arr) { + int n = arr.size(); + + int count = 0; + for(int i = 0 ; i& A) { + + int n = A.size(); + + //find the break point + int ind = - 1 ; + for(int i = n -2; i>=0 ;i--){ + + if(A[i]ind;i--){ + if(A[i]> A[ind]){ + swap(A[i],A[ind]); + break; + } + } + + reverse(A.begin() + ind +1 , A.end()); + } + }; \ No newline at end of file diff --git a/sde_sheet/lec_5.cpp b/sde_sheet/lec_5.cpp new file mode 100644 index 0000000..f6b9ad0 --- /dev/null +++ b/sde_sheet/lec_5.cpp @@ -0,0 +1,18 @@ +class Solution { + public: + vector> transpose(vector>& matrix) { + int n = matrix.size(); + int m = matrix[0].size(); + vector> temp(m, vector(n)); + + for(int i = 0 ; i>& matrix) { + int n = matrix.size(); + // int m = matrix[0].size(); + + + for(int i = 0 ; i& arr) { + + int maxi = INT_MIN; + int sum = 0 ; + int n = arr.size(); + + for(int i =0 ; imaxi){ + maxi = sum; + } + + if(sum<0){ + sum = 0 ; + } + } + + return maxi; + } + }; \ No newline at end of file diff --git a/sde_sheet/lec_8.cpp b/sde_sheet/lec_8.cpp new file mode 100644 index 0000000..125bb99 --- /dev/null +++ b/sde_sheet/lec_8.cpp @@ -0,0 +1,26 @@ +class Solution { + public: + void sortColors(vector& arr) { + + int n = arr.size(); + + int low = 0 , mid = 0 , high = n-1; + + while(mid<= high){ + + if(arr[mid] == 0){ + swap(arr[low] , arr[mid]); + low++; + mid++; + } + else if(arr[mid] == 1){ + mid++; + } + else { + + swap(arr[mid] , arr[high]); + high--; + } + } + } + }; \ No newline at end of file diff --git a/sde_sheet/lec_9.cpp b/sde_sheet/lec_9.cpp new file mode 100644 index 0000000..0826b2a --- /dev/null +++ b/sde_sheet/lec_9.cpp @@ -0,0 +1,29 @@ +class Solution { + public: + void merge(vector& arr1, int m, vector& arr2, int n) { + + int left = m - 1; // Last valid element in arr1 + int right = 0; // First element in arr2 + + // Swap misplaced elements between arr1 and arr2 + while (left >= 0 && right < n) { + if (arr1[left] > arr2[right]) { + swap(arr1[left], arr2[right]); + left--; + right++; + } else { + break; + } + } + + // Sort both arrays separately + sort(arr1.begin(), arr1.begin() + m); + sort(arr2.begin(), arr2.end()); + + // Copy sorted arr2 back into arr1's extra space + for (int i = 0; i < n; i++) { + arr1[m + i] = arr2[i]; + } + } + }; + \ No newline at end of file diff --git a/stack/lec6_24.cpp b/stack/lec6_24.cpp index c803702..54946d1 100644 --- a/stack/lec6_24.cpp +++ b/stack/lec6_24.cpp @@ -49,4 +49,29 @@ class Solution { return ans; } -}; \ No newline at end of file +}; + + + +//alternative +class Solution { + public: + int largestRectangleArea(vector& histo) { + stack st; + int maxA = 0; + int n =histo.size(); + for(int i=0;i<=n;i++) { + while(!st.empty() && (i==n || histo[st.top()] >= histo[i])) { + int height = histo[st.top()]; + st.pop(); + int width; + if(st.empty()) width = i; + else width = i- st.top() - 1; + maxA = max(maxA, width * height); + } + st.push(i); + } + return maxA; + } + }; + \ No newline at end of file