From 7956aeea77f2b2b151b3cf6d8b3ea0358f062ab2 Mon Sep 17 00:00:00 2001 From: jashanpreet singh Date: Tue, 11 Feb 2025 22:06:33 +0530 Subject: [PATCH 1/8] updated to main branch --- README.md | 20 +++++++++++++++++--- image.png | Bin 0 -> 33819 bytes 2 files changed, 17 insertions(+), 3 deletions(-) create mode 100644 image.png diff --git a/README.md b/README.md index 3ae92d8..cff404a 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,21 @@ -# Data Structures and Algorithms Repository +# 🚀 Ultimate DSA Repository for Coding Interviews 💻 + + -This repository contains various Data Structures and Algorithms (DSA) topics implemented in C++ . It contains topicwise problem link and solution code of various DSA topics . - +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** + +✅ 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) diff --git a/image.png b/image.png new file mode 100644 index 0000000000000000000000000000000000000000..0e2fb9b1de5e5b77044f57a58afbf32803872259 GIT binary patch literal 33819 zcmd?RRal!{6efzhwG?;v0>xe0;#Racl;ZC0?!}>Kp;&?7?xjeBy9JlvZXrM>{b%Nx zIXCC-T%C(NU-r(<%GR~lde^%WtDz>3i$#uwfPjFjsPI7(0RgEU0RfQ(0}WnNvtmdN ze;~SR%F7}ECMXZ#1r%!;RT%_?`ef`Ub5wX4(?vnw9RcCB$G;cix)qHN0z!D5;s+UR zACuFyK%+U!o(C9-5HBf}{0OS09;t)A48xqwbmFsYM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!%^$w@*Ln>!wLzFZxqbqN zis!)BHS<(6P8+BN2_d`vX2ON@Cfr#9KjiF9a^P5LgL_JX-;cyQC#~tOe)hRW{B?z@ zb=+NcOH#tNKNTY5Nrd_QzrXe@;vd&oC#S!&Zr1F*ANvszC*JQm4*fE=T{8VLAnn>o z@}R_yI9Msyln>eDSwis`Sh5FZ`+0b{rf&Nt^OdWw&9}=G-&ZrfHGY_;r!{SLuRA(Y zYEtUF&nO(Fo=N*xdSHve9CDz;+gkeWE;ff9DKEO5(}eZw8p5a&&q4Cv8?-C2=I@i9 z2#_+J;S&cAXlv3k_!K}trQ<*$7=Y@y(ploIjC3ruJ>O-!+!4G;8pc;6wYFI|y%J#I zOz5)yQrfg1)bBM1(1*17KlH&5jhD9z!yF<)){bf;MMcYtkX$}9RpL@xri#QonHCma z(SkN;Ma~4X50^v?{u5b#t(tp0(>FR$MppdWJswN;ElD^#gzi$y}arWK?0UI z{HB`ydm_x>J@2he6XNh!`2*(^Eqg$9NqY&fc@a8X2hES~RB+ZA0HI6dt`G_E&LaD$ z{hF*?pC<^2SvE6voTk5Ukt*+t5`LceF%CU1bJ`8&Db%2aLI=%zj9~&Bgu&|6+2nyt zOyG(5;E*pZ*NzkWR2M>=x? zrLHl+C-lwfnIDIIux0Q#{^M_BsR73J*DiBs7nStD;b@Eg2N~#YXMG&w|9{$jed7V~ Yr-?E0daB=>C%`{RQPBG$k&izA3w305Q~&?~ literal 0 HcmV?d00001 From b8b63b5b4e8d3c2dcd66534f37d46da45fae3c4a Mon Sep 17 00:00:00 2001 From: jashanpreet singh Date: Sun, 9 Mar 2025 22:23:38 +0530 Subject: [PATCH 2/8] matrix and dp --- DP/lec10_1.cpp | 49 +++++++++++++++++++++++ DP/lec10_10.cpp | 22 +++++++++++ DP/lec10_11.cpp | 23 +++++++++++ DP/lec10_12.cpp | 39 ++++++++++++++++++ DP/lec10_13.cpp | 65 ++++++++++++++++++++++++++++++ DP/lec10_14.cpp | 68 +++++++++++++++++++++++++++++++ DP/lec10_15.cpp | 67 +++++++++++++++++++++++++++++++ DP/lec10_16.cpp | 68 +++++++++++++++++++++++++++++++ DP/lec10_17.cpp | 15 +++++++ DP/lec10_18.cpp | 16 ++++++++ DP/lec10_19.cpp | 33 ++++++++++++++++ DP/lec10_2.cpp | 19 +++++++++ DP/lec10_20.cpp | 30 ++++++++++++++ DP/lec10_21.cpp | 21 ++++++++++ DP/lec10_22.cpp | 24 +++++++++++ DP/lec10_23.cpp | 28 +++++++++++++ DP/lec10_24.cpp | 19 +++++++++ DP/lec10_25.cpp | 21 ++++++++++ DP/lec10_26.cpp | 22 +++++++++++ DP/lec10_27.cpp | 24 +++++++++++ DP/lec10_3.cpp | 30 ++++++++++++++ DP/lec10_4.cpp | 58 +++++++++++++++++++++++++++ DP/lec10_5.cpp | 58 +++++++++++++++++++++++++++ DP/lec10_6.cpp | 64 ++++++++++++++++++++++++++++++ DP/lec10_7.cpp | 77 ++++++++++++++++++++++++++++++++++++ DP/lec10_8.cpp | 77 ++++++++++++++++++++++++++++++++++++ DP/lec10_9.cpp | 20 ++++++++++ Matrix/lec2_SpiralMatrix.cpp | 47 +++++++++++++++++++++- Matrix/lec2_Transpose.cpp | 25 +++++++++++- Matrix/lec2_WaveFormI.cpp | 48 ++++++++++++++++++++++ Matrix/lec2_revcol.cpp | 33 +++++++++++++--- README.md | 37 ++++++++++++----- 32 files changed, 1231 insertions(+), 16 deletions(-) create mode 100644 DP/lec10_1.cpp create mode 100644 DP/lec10_10.cpp create mode 100644 DP/lec10_11.cpp create mode 100644 DP/lec10_12.cpp create mode 100644 DP/lec10_13.cpp create mode 100644 DP/lec10_14.cpp create mode 100644 DP/lec10_15.cpp create mode 100644 DP/lec10_16.cpp create mode 100644 DP/lec10_17.cpp create mode 100644 DP/lec10_18.cpp create mode 100644 DP/lec10_19.cpp create mode 100644 DP/lec10_2.cpp create mode 100644 DP/lec10_20.cpp create mode 100644 DP/lec10_21.cpp create mode 100644 DP/lec10_22.cpp create mode 100644 DP/lec10_23.cpp create mode 100644 DP/lec10_24.cpp create mode 100644 DP/lec10_25.cpp create mode 100644 DP/lec10_26.cpp create mode 100644 DP/lec10_27.cpp create mode 100644 DP/lec10_3.cpp create mode 100644 DP/lec10_4.cpp create mode 100644 DP/lec10_5.cpp create mode 100644 DP/lec10_6.cpp create mode 100644 DP/lec10_7.cpp create mode 100644 DP/lec10_8.cpp create mode 100644 DP/lec10_9.cpp 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_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_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_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_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_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_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_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 Date: Mon, 10 Mar 2025 17:21:23 +0530 Subject: [PATCH 3/8] 2-D DP --- DP/lec10_28.cpp | 24 +++++++++++++ DP/lec10_29.cpp | 24 +++++++++++++ DP/lec10_30.cpp | 24 +++++++++++++ DP/lec10_31.cpp | 25 ++++++++++++++ DP/lec10_32.cpp | 26 ++++++++++++++ DP/lec10_33.cpp | 28 +++++++++++++++ DP/lec10_34.cpp | 44 ++++++++++++++++++++++++ DP/lec10_35.cpp | 36 ++++++++++++++++++++ DP/lec10_36.cpp | 70 ++++++++++++++++++++++++++++++++++++++ DP/lec10_37.cpp | 75 +++++++++++++++++++++++++++++++++++++++++ DP/lec10_38.cpp | 85 ++++++++++++++++++++++++++++++++++++++++++++++ DP/lec10_39.cpp | 90 +++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 8 +++++ 13 files changed, 559 insertions(+) create mode 100644 DP/lec10_28.cpp create mode 100644 DP/lec10_29.cpp create mode 100644 DP/lec10_30.cpp create mode 100644 DP/lec10_31.cpp create mode 100644 DP/lec10_32.cpp create mode 100644 DP/lec10_33.cpp create mode 100644 DP/lec10_34.cpp create mode 100644 DP/lec10_35.cpp create mode 100644 DP/lec10_36.cpp create mode 100644 DP/lec10_37.cpp create mode 100644 DP/lec10_38.cpp create mode 100644 DP/lec10_39.cpp 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_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/README.md b/README.md index 3bac3bc..1d76956 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ Yellow arrow indicates "platform name" , which on clicking ; navigates you to th 13. [Binary Tree](#binary-tree) 14. [Graph](#graph) 15. [Heap](#heap) +16. [Dynamic Programming](#dynamic-programming) ## 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/)*** @@ -367,6 +368,13 @@ All about objects and classes - 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/ninja-and-his-friends/1?utm_source=youtube&utm_medium=collab_striver_ytdescription&utm_campaign=ninja-and-his-friends)*** + + +### DP on subsequences + ## YouTube Channel Check out the accompanying YouTube channel for video explanations and tutorials: [Coder Army](https://youtube.com/@CoderArmy9?si=vc7X_Fis2kOgsE7i) From f3f4b1ec73f8980886de54c53ccef4264f87c83a Mon Sep 17 00:00:00 2001 From: jashanpreet singh Date: Sat, 15 Mar 2025 12:58:37 +0530 Subject: [PATCH 4/8] dp on subsequence --- DP/lec10_40.cpp | 62 +++++++++++++++++++ DP/lec10_41.cpp | 65 ++++++++++++++++++++ DP/lec10_42.cpp | 85 +++++++++++++++++++++++++ DP/lec10_43.cpp | 77 +++++++++++++++++++++++ DP/lec10_44.cpp | 44 +++++++++++++ DP/lec10_45.cpp | 40 ++++++++++++ DP/lec10_46.cpp | 44 +++++++++++++ DP/lec10_47.cpp | 66 ++++++++++++++++++++ DP/lec10_48.cpp | 72 ++++++++++++++++++++++ DP/lec10_49.cpp | 69 +++++++++++++++++++++ DP/lec10_50.cpp | 92 ++++++++++++++++++++++++++++ DP/lec10_54.cpp | 24 ++++++++ DP/lec10_55.cpp | 25 ++++++++ DP/lec10_56.cpp | 26 ++++++++ DP/lec10_57.cpp | 28 +++++++++ DP/lec10_58.cpp | 18 ++++++ DP/lec10_59.cpp | 28 +++++++++ DP/lec10_60.cpp | 30 +++++++++ DP/lec10_61.cpp | 49 +++++++++++++++ DP/lec10_62.cpp | 31 ++++++++++ DP/lec10_63.cpp | 45 ++++++++++++++ DP/lec10_64.cpp | 23 +++++++ DP/lec10_65.cpp | 24 ++++++++ DP/lec10_66.cpp | 42 +++++++++++++ DP/lec10_67.cpp | 46 ++++++++++++++ DP/lec10_68.cpp | 69 +++++++++++++++++++++ DP/lec10_69.cpp | 71 +++++++++++++++++++++ DP/lec10_70.cpp | 74 ++++++++++++++++++++++ DP/lec10_71.cpp | 160 ++++++++++++++++++++++++++++++++++++++++++++++++ DP/lec10_72.cpp | 66 ++++++++++++++++++++ DP/lec10_73.cpp | 66 ++++++++++++++++++++ DP/lec10_74.cpp | 86 ++++++++++++++++++++++++++ DP/lec10_75.cpp | 87 ++++++++++++++++++++++++++ README.md | 22 ++++++- 34 files changed, 1855 insertions(+), 1 deletion(-) create mode 100644 DP/lec10_40.cpp create mode 100644 DP/lec10_41.cpp create mode 100644 DP/lec10_42.cpp create mode 100644 DP/lec10_43.cpp create mode 100644 DP/lec10_44.cpp create mode 100644 DP/lec10_45.cpp create mode 100644 DP/lec10_46.cpp create mode 100644 DP/lec10_47.cpp create mode 100644 DP/lec10_48.cpp create mode 100644 DP/lec10_49.cpp create mode 100644 DP/lec10_50.cpp create mode 100644 DP/lec10_54.cpp create mode 100644 DP/lec10_55.cpp create mode 100644 DP/lec10_56.cpp create mode 100644 DP/lec10_57.cpp create mode 100644 DP/lec10_58.cpp create mode 100644 DP/lec10_59.cpp create mode 100644 DP/lec10_60.cpp create mode 100644 DP/lec10_61.cpp create mode 100644 DP/lec10_62.cpp create mode 100644 DP/lec10_63.cpp create mode 100644 DP/lec10_64.cpp create mode 100644 DP/lec10_65.cpp create mode 100644 DP/lec10_66.cpp create mode 100644 DP/lec10_67.cpp create mode 100644 DP/lec10_68.cpp create mode 100644 DP/lec10_69.cpp create mode 100644 DP/lec10_70.cpp create mode 100644 DP/lec10_71.cpp create mode 100644 DP/lec10_72.cpp create mode 100644 DP/lec10_73.cpp create mode 100644 DP/lec10_74.cpp create mode 100644 DP/lec10_75.cpp 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_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_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_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/README.md b/README.md index 1d76956..5eb0cb5 100644 --- a/README.md +++ b/README.md @@ -370,11 +370,31 @@ All about objects and classes - 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/ninja-and-his-friends/1?utm_source=youtube&utm_medium=collab_striver_ytdescription&utm_campaign=ninja-and-his-friends)*** +- 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) [Recursive](./DP/lec10_76.cpp) ; [DP](./DP/lec10_77.cpp) ; [Tabulation](./DP/lec10_78.cpp) ; [SpaceOptimised](./DP/lec10_79.cpp) :- ***[Gfg](https://www.geeksforgeeks.org/problems/longest-common-subsequence-4/1?utm_source=youtube&utm_medium=collab_striver_ytdescription&utm_campaign=longest-common-subsequence-4)*** ## YouTube Channel Check out the accompanying YouTube channel for video explanations and tutorials: [Coder Army](https://youtube.com/@CoderArmy9?si=vc7X_Fis2kOgsE7i) From 3ed8b0efa54badc23e45048fea627fc154dae5ec Mon Sep 17 00:00:00 2001 From: jashanpreet singh Date: Mon, 17 Mar 2025 00:03:33 +0530 Subject: [PATCH 5/8] dp on strings,stocks , --- .vscode/tasks.json | 28 ++++++++++ DP/lec10_76.cpp | 21 ++++++++ DP/lec10_77.cpp | 21 ++++++++ DP/lec10_78.cpp | 23 ++++++++ DP/lec10_79.cpp | 24 +++++++++ DP/lec10_80.cpp | 125 ++++++++++++++++++++++++++++++++++++++++++++ DP/lec10_81.cpp | 51 ++++++++++++++++++ DP/lec10_82.cpp | 53 +++++++++++++++++++ DP/lec10_83.cpp | 29 ++++++++++ DP/lec10_84.cpp | 33 ++++++++++++ DP/lec10_85.cpp | 26 +++++++++ DP/lec10_86.cpp | 18 +++++++ DP/lec10_87.cpp | 37 +++++++++++++ README.md | 27 +++++++++- sde_sheet/lec_1.cpp | 26 +++++++++ sde_sheet/lec_2.cpp | 17 ++++++ stack/lec6_24.cpp | 27 +++++++++- 17 files changed, 584 insertions(+), 2 deletions(-) create mode 100644 .vscode/tasks.json create mode 100644 DP/lec10_76.cpp create mode 100644 DP/lec10_77.cpp create mode 100644 DP/lec10_78.cpp create mode 100644 DP/lec10_79.cpp create mode 100644 DP/lec10_80.cpp create mode 100644 DP/lec10_81.cpp create mode 100644 DP/lec10_82.cpp create mode 100644 DP/lec10_83.cpp create mode 100644 DP/lec10_84.cpp create mode 100644 DP/lec10_85.cpp create mode 100644 DP/lec10_86.cpp create mode 100644 DP/lec10_87.cpp create mode 100644 sde_sheet/lec_1.cpp create mode 100644 sde_sheet/lec_2.cpp 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_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_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/README.md b/README.md index 5eb0cb5..5ed9ed5 100644 --- a/README.md +++ b/README.md @@ -394,7 +394,32 @@ All about objects and classes ### DP on strings -- Longest Common Subsequence (DP - 25) [Recursive](./DP/lec10_76.cpp) ; [DP](./DP/lec10_77.cpp) ; [Tabulation](./DP/lec10_78.cpp) ; [SpaceOptimised](./DP/lec10_79.cpp) :- ***[Gfg](https://www.geeksforgeeks.org/problems/longest-common-subsequence-4/1?utm_source=youtube&utm_medium=collab_striver_ytdescription&utm_campaign=longest-common-subsequence-4)*** +- 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/)*** ## YouTube Channel Check out the accompanying YouTube channel for video explanations and tutorials: [Coder Army](https://youtube.com/@CoderArmy9?si=vc7X_Fis2kOgsE7i) 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 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/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 From c8c1db52b5f6db605612c4be651423d0ee388f86 Mon Sep 17 00:00:00 2001 From: jashanpreet singh Date: Wed, 19 Mar 2025 23:15:40 +0530 Subject: [PATCH 6/8] sde sheet --- README.md | 13 ++++++++++ sde_sheet/lec_10.cpp | 60 ++++++++++++++++++++++++++++++++++++++++++++ sde_sheet/lec_11.cpp | 52 ++++++++++++++++++++++++++++++++++++++ sde_sheet/lec_4.cpp | 31 +++++++++++++++++++++++ sde_sheet/lec_5.cpp | 18 +++++++++++++ sde_sheet/lec_6.cpp | 22 ++++++++++++++++ sde_sheet/lec_7.cpp | 24 ++++++++++++++++++ sde_sheet/lec_8.cpp | 26 +++++++++++++++++++ sde_sheet/lec_9.cpp | 29 +++++++++++++++++++++ 9 files changed, 275 insertions(+) create mode 100644 sde_sheet/lec_10.cpp create mode 100644 sde_sheet/lec_11.cpp create mode 100644 sde_sheet/lec_4.cpp create mode 100644 sde_sheet/lec_5.cpp create mode 100644 sde_sheet/lec_6.cpp create mode 100644 sde_sheet/lec_7.cpp create mode 100644 sde_sheet/lec_8.cpp create mode 100644 sde_sheet/lec_9.cpp diff --git a/README.md b/README.md index 5ed9ed5..09ba739 100644 --- a/README.md +++ b/README.md @@ -420,6 +420,19 @@ All about objects and classes - 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/)*** ## YouTube Channel Check out the accompanying YouTube channel for video explanations and tutorials: [Coder Army](https://youtube.com/@CoderArmy9?si=vc7X_Fis2kOgsE7i) diff --git a/sde_sheet/lec_10.cpp b/sde_sheet/lec_10.cpp new file mode 100644 index 0000000..a9f5d7a --- /dev/null +++ b/sde_sheet/lec_10.cpp @@ -0,0 +1,60 @@ +class Solution { + public: + int findDuplicate(vector& 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_4.cpp b/sde_sheet/lec_4.cpp new file mode 100644 index 0000000..06243b1 --- /dev/null +++ b/sde_sheet/lec_4.cpp @@ -0,0 +1,31 @@ +class Solution { + public: + void nextPermutation(vector& 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 From 3210524c1eb6255cdd31ff68261a1bec8d1fdc05 Mon Sep 17 00:00:00 2001 From: jashanpreet singh Date: Sat, 22 Mar 2025 22:57:52 +0530 Subject: [PATCH 7/8] array and linked list --- README.md | 23 ++++++++++ sde_sheet/lec_12.cpp | 71 +++++++++++++++++++++++++++++ sde_sheet/lec_13.cpp | 104 +++++++++++++++++++++++++++++++++++++++++++ sde_sheet/lec_14.cpp | 64 ++++++++++++++++++++++++++ sde_sheet/lec_15.cpp | 97 ++++++++++++++++++++++++++++++++++++++++ sde_sheet/lec_22.cpp | 41 +++++++++++++++++ sde_sheet/lec_23.cpp | 41 +++++++++++++++++ sde_sheet/lec_24.cpp | 15 +++++++ sde_sheet/lec_25.cpp | 49 ++++++++++++++++++++ sde_sheet/lec_26.cpp | 53 ++++++++++++++++++++++ sde_sheet/lec_27.cpp | 66 +++++++++++++++++++++++++++ 11 files changed, 624 insertions(+) create mode 100644 sde_sheet/lec_12.cpp create mode 100644 sde_sheet/lec_13.cpp create mode 100644 sde_sheet/lec_14.cpp create mode 100644 sde_sheet/lec_15.cpp create mode 100644 sde_sheet/lec_22.cpp create mode 100644 sde_sheet/lec_23.cpp create mode 100644 sde_sheet/lec_24.cpp create mode 100644 sde_sheet/lec_25.cpp create mode 100644 sde_sheet/lec_26.cpp create mode 100644 sde_sheet/lec_27.cpp diff --git a/README.md b/README.md index 09ba739..f7ea33c 100644 --- a/README.md +++ b/README.md @@ -433,6 +433,29 @@ All about objects and classes ### 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/)*** +- ## YouTube Channel Check out the accompanying YouTube channel for video explanations and tutorials: [Coder Army](https://youtube.com/@CoderArmy9?si=vc7X_Fis2kOgsE7i) 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(jval <= 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..af78797 --- /dev/null +++ b/sde_sheet/lec_27.cpp @@ -0,0 +1,66 @@ +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; + } + }; \ No newline at end of file From 9f84cc65bf377adb3bf401650f1863339670216a Mon Sep 17 00:00:00 2001 From: jashanpreet singh Date: Sun, 23 Mar 2025 23:02:27 +0530 Subject: [PATCH 8/8] starting with greedy --- README.md | 10 ++++++++++ greedy/lec1_1.cpp | 0 sde_sheet/lec_27.cpp | 33 ++++++++++++++++++++++++++++++++- sde_sheet/lec_28.cpp | 21 +++++++++++++++++++++ sde_sheet/lec_29.cpp | 19 +++++++++++++++++++ 5 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 greedy/lec1_1.cpp create mode 100644 sde_sheet/lec_28.cpp create mode 100644 sde_sheet/lec_29.cpp diff --git a/README.md b/README.md index f7ea33c..9564d81 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,9 @@ Yellow arrow indicates "platform name" , which on clicking ; navigates you to th 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/)*** @@ -351,6 +354,8 @@ All about objects and classes - [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 @@ -456,6 +461,11 @@ All about objects and classes - 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) diff --git a/greedy/lec1_1.cpp b/greedy/lec1_1.cpp new file mode 100644 index 0000000..e69de29 diff --git a/sde_sheet/lec_27.cpp b/sde_sheet/lec_27.cpp index af78797..fc304cf 100644 --- a/sde_sheet/lec_27.cpp +++ b/sde_sheet/lec_27.cpp @@ -63,4 +63,35 @@ class Solution { return NULL; } - }; \ No newline at end of file + }; + + +// 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