├── .gitignore ├── Count_ways_to_N'th_Stair.cpp ├── Maximum_Sum_Dividing_Recurssive.cpp ├── Count_Number_of_hops.cpp ├── No_Of_Ways_To_Tile.cpp ├── Kadane.cpp ├── Diameter_Of_Binary_Tree.cpp ├── Friends_Pairing_Problem.cpp ├── unique_paths_1.cpp ├── Reach_a_given_score.cpp ├── Tiling_Problem.cpp ├── Max_Path_Sum_in_Triangle.cpp ├── Maximum_Path_Sum_in_Binary_tree.cpp ├── Count_Distinct_Subsequences.cpp ├── jump_game_2..cpp ├── Matrix_Chain_Multiplication.cpp ├── Maximum_length_of_pairs.cpp ├── Longest_Consequetive_Subsequence.cpp ├── Longest_Decreasing_Subsequence.cpp ├── flip_game_1.cpp ├── Longest_Increasing_Subsequence.cpp ├── Number_of_ways_to_decode_a_string.cpp ├── maximum_product_subarray.cpp ├── number_of_valid_parenthesis.cpp ├── nim_game.cpp ├── number_of_permutations_such_that_no_three_forms_increasing_sequence.cpp ├── Longest_Repeating_Subsequence.cpp ├── Get_Minimum_Squares.cpp ├── spoj ├── AIBOHP - Aibohphobia.cpp ├── ANARC09A - Seinfeld.cpp ├── ABA12C - Buying Apples!.cpp └── ACODE - Alphacode.cpp ├── Count_All_Pallindromic_Subsequence.cpp ├── Jump_Game_5.cpp ├── Optimal_Strategy_To_Play_A_Game.cpp ├── Russian_Doll_Envelope.cpp ├── Best_Time_To_Buy_And_Sell_Stock_.cpp ├── Maximum_Sum_Increasing_Subsequence.cpp ├── Maximum_Product_Increasing_Subsequence.cpp ├── Circular_Kadene.cpp ├── video_stiching.cpp ├── Jump_Game_3.cpp ├── Longest_Common_Substring.cpp ├── Shortest_Common_Supersequence.cpp ├── Maximum_Sum_Decreasing_Subsequence.cpp ├── Minimum_Initial_Points_To_Reach_Destinaion.cpp ├── minm_number_of_taps_to_open_water.cpp ├── Minm_Deletion_To_Make_Pallindrome.cpp ├── Maximize_the_cutting.cpp ├── SubsetSum.cpp ├── Uncrossed_Lines.cpp ├── Knapsack_0_1.cpp ├── Max_path_sum.cpp ├── Coin_Changing_2.cpp ├── Longest_Common_Subsequence.cpp ├── maximum_sum_submatrix.cpp ├── Count_Pallindromic_SubString_Of_String_gfg.cpp ├── Minm_Insertion_Deletion_Convert.cpp ├── Longest_Pallindromic_Substring.cpp ├── Longest_Pallindromic_Subsequence.cpp ├── Edit_Distance.cpp ├── Equal_Sum_Partition.cpp ├── nth_catlan_number.cpp ├── count_submatrices_with_all_ones.cpp ├── Count_Subsets_of_given_Sum.cpp ├── Path_In_A_Matrix.cpp ├── Unbounded_Knapsack.cpp ├── Coin_Changing_1.cpp ├── Rod_Cutting.cpp ├── GoldMine_Problem.cpp ├── flip_game_2.cpp ├── Minimum_Subset_Sum_Difference.cpp ├── Procuct_of_array_except_self.cpp ├── num_products.cpp ├── Distinct_Occurences.cpp ├── BitonicSequence.cpp ├── Print_Longest_Common_Subsequence.cpp ├── Minimum_Jumps_To_Reach_End.cpp ├── Maximum_Size_Submatrix_Square.cpp ├── 3_segment.cpp ├── Count_All_Increasing_Subsequences.cpp ├── maximal_square.cpp ├── Minimum_Ascii_Sum_deletion_To_Make_two_Strings_equal.cpp ├── unique_paths_2.cpp ├── count_subsets_of_given_difference.cpp ├── zerosAndOnes.cpp ├── Minm_Cost_To_Fill_Bag.cpp ├── Box_Stacking_Problem.cpp ├── Maximum_Dot_Product_Subsequence.cpp ├── Last_Stone_3.cpp ├── longest_alternating_subsequence.cpp └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | a.out -------------------------------------------------------------------------------- /Count_ways_to_N'th_Stair.cpp: -------------------------------------------------------------------------------- 1 | long long countWays(int m){ 2 | long long dp[m+1]={0}; 3 | dp[0]=1; 4 | for(int i=1;i<=m;i++) dp[i]+=dp[i-1]; 5 | for(int i=2;i<=m;i++) dp[i]+=dp[i-2]; 6 | return dp[m]; 7 | } 8 | -------------------------------------------------------------------------------- /Maximum_Sum_Dividing_Recurssive.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int t; 6 | cin>>t; 7 | while(t--){ 8 | int n; 9 | cin>>n; 10 | int dp[n+1]; 11 | dp[0]=0,dp[1]=1; 12 | for(int i=2;i<=n;i++){ 13 | dp[i]=max(dp[i/2]+dp[i/3]+dp[i/4],i); 14 | } 15 | cout< 2 | using namespace std; 3 | void solve(int n){ 4 | int dp[n]; 5 | dp[1]=1,dp[2]=2,dp[3]=4; 6 | for(int i=4;i<=n;i++){ 7 | dp[i]=dp[i-1]+dp[i-2]+dp[i-3]; 8 | } 9 | cout<>t; 15 | while(t--){ 16 | int n; 17 | cin>>n; 18 | solve(n); 19 | } 20 | return 0; 21 | } -------------------------------------------------------------------------------- /No_Of_Ways_To_Tile.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int t; 6 | cin>>t; 7 | while(t--){ 8 | int n; 9 | cin>>n; 10 | long long int dp[n+1]; 11 | dp[0]=0; 12 | for(int i=1;i<=n;i++){ 13 | if(i<=3) dp[i]=1; 14 | else if (i==4) dp[i]=2; 15 | else dp[i] = dp[i-1]+dp[i-4]; 16 | } 17 | cout< 2 | using namespace std; 3 | 4 | int Kadane(int a[],int size){ 5 | int max_cur=a[0],max_far=a[0]; 6 | for(int i=1;ileft,res); 5 | int right=helper(node->right,res); 6 | int tempAns=1+max(left,right); 7 | int ans=max(tempAns,1+left+right); 8 | res=max(res,ans); 9 | return tempAns; 10 | } 11 | int diameter(Node* node) { 12 | int res=INT_MIN; 13 | helper(node,res); 14 | return res; 15 | } 16 | -------------------------------------------------------------------------------- /Friends_Pairing_Problem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define m 1000000007 3 | using namespace std; 4 | int main() 5 | { 6 | int t; 7 | cin>>t; 8 | while(t--){ 9 | int n; 10 | cin>>n; 11 | long long int dp[n+1]; 12 | for(int i=0;i<=n;i++){ 13 | if(i<=2) dp[i]=i; 14 | else{ 15 | dp[i]=((dp[i-1]%m)+((i-1)%m*(dp[i-2]%m))%m)%m; 16 | } 17 | } 18 | cout< 2 | using namespace std; 3 | 4 | // problem: https://leetcode.com/problems/unique-paths/ 5 | 6 | class Solution { 7 | public: 8 | int uniquePaths(int m, int n) { 9 | vector>dp(m,vector(n,1)); 10 | for(int i=1;i 2 | using namespace std; 3 | void solve(int sum){ 4 | int dp[sum+1]={0}; 5 | dp[0]=1; 6 | for(int i=3;i<=sum;i++) dp[i]+=dp[i-3]; 7 | for(int i=5;i<=sum;i++) dp[i]+=dp[i-5]; 8 | for(int i=10;i<=sum;i++) dp[i]+=dp[i-10]; 9 | 10 | cout<>t; 16 | while(t--){ 17 | int sum; 18 | cin>>sum; 19 | solve(sum); 20 | } 21 | return 0; 22 | } 23 | 24 | //similar to coin changing -------------------------------------------------------------------------------- /Tiling_Problem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int CountWays(int n, int m) 5 | { 6 | int dp[n + 1]; 7 | dp[0] = 0; 8 | for (int i = 1; i <= n; i++) { 9 | if (i > m) 10 | dp[i] = dp[i - 1] + dp[i - m]; 11 | else if (i < m) 12 | dp[i] = 1; 13 | else 14 | dp[i] = 2; 15 | } 16 | return dp[n]; 17 | } 18 | int main() 19 | { 20 | int n = 7, m = 4; 21 | cout << "Number of ways = " 22 | << CountWays(n, m); 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Max_Path_Sum_in_Triangle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define N 4 3 | using namespace std; 4 | 5 | int Max_Sum_Tri(int t[][N],int m,int n){ 6 | for(int i=m-2;i>=0;i--){ 7 | for(int j=0;j<=i;j++){ 8 | t[i][j] = max(t[i+1][j],t[i+1][j+1])+t[i][j]; 9 | } 10 | } 11 | return t[0][0]; 12 | } 13 | 14 | int main(){ 15 | int t[N][N] = { {3,0,0,0}, 16 | {7,4,0,0}, 17 | {2,4,6,0}, 18 | {8,5,9,3} }; 19 | cout<left, re); 6 | int r = maxToRoot(root->right, re); 7 | if (l < 0) l = 0; 8 | if (r < 0) r = 0; 9 | re=max(re,l+r+root->val); 10 | return root->val += max(l, r); 11 | } 12 | public: 13 | int maxPathSum(TreeNode *root) { 14 | int re = -2147483648; 15 | maxToRoot(root, re); 16 | return re; 17 | } 18 | }; -------------------------------------------------------------------------------- /Count_Distinct_Subsequences.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX_CHAR 256 3 | using namespace std; 4 | int CDS(string s,int size){ 5 | vectorlast_char(MAX_CHAR,-1); 6 | int dp[size+1]; 7 | dp[0]=1; 8 | for(int i=1;i<=size;i++){ 9 | dp[i] = 2*dp[i-1]; 10 | if(last_char[s[i-1]]!=-1){ 11 | dp[i]=dp[i]-dp[last_char[s[i-1]]]; 12 | } 13 | last_char[s[i-1]]=(i-1); 14 | } 15 | return dp[size]; 16 | } 17 | int main(){ 18 | string s="gfg"; 19 | int size=s.length(); 20 | cout< 2 | using namespace std; 3 | 4 | // problem : https://leetcode.com/problems/jump-game-ii/ 5 | // dp+greedy(range-based) 6 | 7 | class Solution { 8 | public: 9 | int jump(vector& nums) { 10 | int pos=0,des=0,jump=0; 11 | for(int i=0;i 2 | using namespace std; 3 | 4 | int dp[5][5]; 5 | int MCM(int a[],int i,int j){ 6 | if(i>=j) return 0; 7 | if(dp[i][j] != -1) return dp[i][j]; 8 | int ans = INT_MAX,temp_ans; 9 | for(int k=i;k>& pairs) { 2 | int n=pairs.size(); 3 | int dp[n+1],res=1; 4 | sort(pairs.begin(),pairs.end()); 5 | for(int i=0;ipairs[j][1]){ 11 | dp[i]=max(dp[i],1+dp[j]); 12 | } 13 | } 14 | res=max(res,dp[i]); 15 | } 16 | if(n==0)return 0; 17 | else return res; 18 | } -------------------------------------------------------------------------------- /Longest_Consequetive_Subsequence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int t; 6 | cin>>t; 7 | while(t--){ 8 | int n; 9 | cin>>n; 10 | int m[100001]={0}; 11 | int a[n]; 12 | for(int i=0;i>a[i]; 13 | for(int i=0;i=1)count++; 19 | else count=0; 20 | res=max(res,count); 21 | } 22 | cout< 2 | using namespace std; 3 | 4 | int LDS(int a[],int size){ 5 | int dp[size+1],res=1; 6 | for(int i=0;i=0;i--){ 10 | for(int j=0;ja[j]){ 12 | dp[i] = max(dp[i],1+dp[j]); 13 | } 14 | } 15 | res = max(res,dp[i]); 16 | } 17 | return res; 18 | } 19 | int main(){ 20 | int a[] = {10,9,2,5,3,7,101,18}; 21 | int size = sizeof(a)/sizeof(a[0]); 22 | cout< 2 | using namespace std; 3 | 4 | // problem : https://www.lintcode.com/problem/flip-game/ 5 | 6 | class Solution { 7 | public: 8 | vector generatePossibleNextMoves(string &s) { 9 | // write your code here 10 | int n=s.length(); 11 | vectorans; 12 | for(int i=0;i 2 | using namespace std; 3 | 4 | int LIS(int a[],int size){ 5 | int dp[size+1],res=1; 6 | for(int i=0;ia[j]){ 12 | dp[i] = max(dp[i],1+dp[j]); 13 | } 14 | } 15 | res = max(res,dp[i]); 16 | } 17 | return res; 18 | } 19 | int main(){ 20 | int a[] = {10,9,2,5,3,7,101,18}; 21 | int size = sizeof(a)/sizeof(a[0]); 22 | reverse(a,a+size); 23 | cout< 2 | using namespace std; 3 | 4 | // similar to fibonacci 5 | int countWays(string s){ 6 | int n = s.length(); 7 | int dp[n+1]; 8 | for(int i=0;i '0'){ 13 | dp[i] = dp[i-1]; 14 | } 15 | if(s[i-2] == '1' || (s[i-2] == '2' && s[i-1] <= '7')){ 16 | dp[i] += dp[i-2]; 17 | } 18 | } 19 | return dp[n]; 20 | } 21 | 22 | 23 | int main(){ 24 | string s; 25 | cin>>s; 26 | cout< 2 | using namespace std; 3 | 4 | // problem : https://leetcode.com/problems/maximum-product-subarray/ 5 | 6 | class Solution { 7 | public: 8 | int maxProduct(vector& nums) { 9 | // almost similar to kadane 10 | int n=nums.size(),maxPro=1,minPro=1,best=INT_MIN; 11 | for(int i=0;i 2 | using namespace std; 3 | 4 | // O(n) approach using binomial coefficient 5 | 6 | // binomial coefficent 7 | int binomial(int n,int r){ 8 | int res=1; 9 | if(r > n-r) r = n-r; 10 | for(int i=0;i> s; 28 | int n = s.length(); 29 | cout< 2 | using namespace std; 3 | 4 | // problem : https://leetcode.com/problems/nim-game 5 | 6 | // n > 10^9 won't work, O(n) solution 7 | class Solution { 8 | public: 9 | bool canWinNim(int n) { 10 | if(n <= 3) return true; 11 | bool dp[n+1]; 12 | dp[0]=false; 13 | dp[1]=true,dp[2]=true,dp[3]=true; 14 | for(int i=4;i<=n;i++){ 15 | dp[i] = !(dp[i-1] && dp[i-2] && dp[i-3]); 16 | } 17 | return dp[n]; 18 | } 19 | }; 20 | 21 | // O(1) solution 22 | 23 | class Solution { 24 | public: 25 | bool canWinNim(int n) { 26 | return !(n%4 == 0); 27 | } 28 | }; -------------------------------------------------------------------------------- /number_of_permutations_such_that_no_three_forms_increasing_sequence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // O(n) approach using binomial coefficient 5 | 6 | // binomial coefficent 7 | int binomial(int n,int r){ 8 | int res=1; 9 | if(r > n-r) r = n-r; 10 | for(int i=0;i> n; 28 | cout< 2 | using namespace std; 3 | 4 | int LRS(string s){ 5 | int n = s.length(); 6 | int dp[n+1][n+1]; 7 | for(int i=0;i<=n;i++){ 8 | for(int j=0;j<=n;j++){ 9 | if(i==0 || j==0){ 10 | dp[i][j] = 0; 11 | } 12 | else if(s[i-1] == s[j-1] && i!=j){ 13 | dp[i][j] = 1 + dp[i-1][j-1]; 14 | } 15 | else{ 16 | dp[i][j] = max(dp[i-1][j],dp[i][j-1]); 17 | } 18 | } 19 | } 20 | return dp[n][n]; 21 | } 22 | 23 | int main() { 24 | string s = "aabebcdd"; 25 | cout< 2 | using namespace std; 3 | int getMinimumSquares(int n){ 4 | int dp[n+1]; 5 | dp[0]=0; 6 | dp[1]=1; 7 | dp[2]=2; 8 | dp[3]=3; 9 | for(int i=4;i<=n;i++){ 10 | dp[i]=i;//marking with the maximum 11 | for(int p=1;p<=ceil(sqrt(i));p++){//maximum it can go is ceil(sqrt(n) 12 | int temp=p*p; 13 | if(temp>i) break; 14 | dp[i]=min(dp[i],1+dp[i-temp]); 15 | } 16 | } 17 | return dp[n]; 18 | } 19 | int main(){ 20 | int t; 21 | cin>>t; 22 | while(t--){ 23 | int n; 24 | cin>>n; 25 | cout< 2 | using namespace std; 3 | #define int long long int 4 | int dp[6103][6103]; 5 | signed main(){ 6 | int t; cin >> t; 7 | while( t--){ 8 | string s; 9 | cin >> s; string t =s; 10 | reverse( t.begin() , t.end()); 11 | memset( dp , 0 , sizeof(dp)); 12 | int n = s.size(); 13 | for ( int i = 1 ;i <= n ; i++ ){ 14 | for ( int j = 1 ; j <= n ; j++ ){ 15 | if ( s[i-1] == t[j-1]) dp[i][j] =1 + dp[i-1][j-1] ; 16 | dp[i][j] = max( dp[i][j] ,max( dp[i-1][j] , dp[i][j-1])); 17 | } 18 | } 19 | cout << n - dp[n][n] << "\n"; 20 | } 21 | } -------------------------------------------------------------------------------- /Count_All_Pallindromic_Subsequence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int CountLPS(string s){ 5 | int n = s.length(); 6 | int dp[n][n]; 7 | memset(dp,0,sizeof(dp)); 8 | for(int i=0;i=0;i--){ 12 | for(int j=i+1;j 2 | using namespace std; 3 | //LEETCODE QUESTION 4 | 5 | int dp[1001]; 6 | int helper(vector& arr,int d,int i){ 7 | if(dp[i]!=-1) return dp[i]; 8 | int ans=1,n=arr.size(); 9 | //right 10 | for(int j=i+1;j<=min(n-1,i+d)&&arr[i]>arr[j];j++)ans=max(ans,1+helper(arr,d,j)); 11 | //left 12 | for(int j=i-1;j>=max(0,i-d)&&arr[i]>arr[j];j--)ans=max(ans,1+helper(arr,d,j)); 13 | return dp[i]=ans; 14 | } 15 | int maxJumps(vector& arr, int d) { 16 | int ans=1; 17 | for(int i=0;i 2 | using namespace std; 3 | 4 | #define int long long int 5 | 6 | signed main(){ 7 | 8 | for ( int t =1; ; t++){ 9 | string s; cin >> s; 10 | if ( s[0] == '-') break; 11 | int open = 0 ; 12 | int ans = 0 ; 13 | int n = s.size(); 14 | for ( int i = 0 ; i < n ; i ++){ 15 | if ( s[i] == '}'){ 16 | if ( !open){ 17 | ans++; 18 | open++; 19 | } 20 | else open--; 21 | } 22 | else open++; 23 | } 24 | cout < 2 | #define N 5 3 | using namespace std; 4 | 5 | int dp[N][N]; 6 | 7 | int OptimalStrategy(int coins[],int i,int j){ 8 | //Base Cases 9 | if(i==j) return coins[i]; 10 | if(i==j+1) return max(coins[i],coins[j]); 11 | if(dp[i][j]==0){ 12 | //Taking from the start 13 | int start = coins[i] + min(OptimalStrategy(coins,i+2,j),OptimalStrategy(coins,i+1,j-1)); 14 | //Taking from end 15 | int end = coins[j] + min(OptimalStrategy(coins,i,j-2),OptimalStrategy(coins,i+1,j-1)); 16 | //maximum of both 17 | dp[i][j] = max(start,end); 18 | } 19 | return dp[i][j]; 20 | } 21 | 22 | int main(){ 23 | 24 | } -------------------------------------------------------------------------------- /Russian_Doll_Envelope.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // Problem Statement : https://leetcode.com/problems/russian-doll-envelopes/ 5 | 6 | //Similar to LIS with a tweak. 7 | int maxEnvelopes(vector>& envelopes) { 8 | sort(envelopes.begin(),envelopes.end()); 9 | int n=envelopes.size(),res=1; 10 | vectordp(n,1); 11 | for(int i=1;ienvelopes[j][0] && envelopes[i][1]>envelopes[j][1]){ 14 | dp[i] = max(dp[i],1+dp[j]); 15 | } 16 | } 17 | res = max(res,dp[i]); 18 | } 19 | if(n==0) return 0; 20 | else return res; 21 | } 22 | 23 | 24 | -------------------------------------------------------------------------------- /Best_Time_To_Buy_And_Sell_Stock_.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //A very beautiful problem with a tweak of Kadane's algorithm. 5 | 6 | int best_time(vector&arr,int n){ 7 | vectordiff(n,0); 8 | for(int i=1;i>n; 22 | vectorarr(n); 23 | for(int i=0;i>arr[i]; 25 | } 26 | cout< 2 | using namespace std; 3 | int MSIS(int a[],int size){ 4 | int dp[size]; 5 | for(int i=0;ia[j] && dp[i]max){ 18 | max = dp[i]; 19 | } 20 | } 21 | return max; 22 | } 23 | 24 | int main(){ 25 | int a[] = {1, 101, 2, 3, 100, 4, 5}; 26 | int size = sizeof(a)/sizeof(a[0]); 27 | cout< 2 | using namespace std; 3 | int MPIS(int a[],int size){ 4 | int dp[size]; 5 | for(int i=0;ia[j] && dp[i]<(a[i]*dp[j])){ 11 | dp[i] = a[i]*dp[j]; 12 | } 13 | } 14 | } 15 | int max = dp[0]; 16 | for(int i=1;imax){ 18 | max = dp[i]; 19 | } 20 | } 21 | return max; 22 | } 23 | 24 | int main(){ 25 | int a[] = { 3, 100, 4, 5, 150, 6 }; 26 | int size = sizeof(a)/sizeof(a[0]); 27 | cout< 4 | using namespace std; 5 | 6 | int maxMoney(int arr[], int num) 7 | { 8 | int max_far=arr[0],max_curr=arr[0],min_far=arr[0],min_curr=arr[0],total=arr[0]; 9 | for(int i=1;i0){ 17 | return max(max_far,total-min_far); 18 | } 19 | else{ 20 | return max_far; 21 | } 22 | } -------------------------------------------------------------------------------- /video_stiching.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // problem: https://leetcode.com/problems/video-stitching/ 5 | 6 | class Solution { 7 | public: 8 | int videoStitching(vector>& clips, int T) { 9 | int minm=0; 10 | int maxm=0; 11 | int total = 0; 12 | while(maxm < T){ 13 | for(int i=0;imaxm){ 17 | maxm = r; 18 | } 19 | } 20 | if(minm == maxm) return -1; 21 | minm = maxm; 22 | total++; 23 | } 24 | return total; 25 | } 26 | }; -------------------------------------------------------------------------------- /Jump_Game_3.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool canReach(vector& arr, int start) { 4 | int N = arr.size(); 5 | vector tab(N, 0); 6 | return help(arr, start, tab, N); 7 | } 8 | bool help(vector& arr, int start, vector& tab, int N) { 9 | if ((start < 0 || start > N - 1) || tab[start] == 1) { 10 | return false; 11 | } 12 | if (arr[start] == 0) { 13 | return true; 14 | } 15 | tab[start] = 1; 16 | if (help(arr, start-arr[start], tab, N)) { 17 | return true; 18 | } 19 | if (help(arr, start+arr[start], tab, N)) { 20 | return true; 21 | } 22 | return false; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /Longest_Common_Substring.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int LCSub(string s1,string s2){ 4 | int m = s1.length(),n = s2.length(); 5 | int dp[m+1][n+1],res=0; 6 | for(int i=0;i<=m;i++){ 7 | for(int j=0;j<=n;j++){ 8 | if(i==0 || j==0){ 9 | dp[i][j] = 0; 10 | } 11 | else if(s1[i-1] == s2[j-1]){ 12 | dp[i][j] = 1 + dp[i-1][j-1]; 13 | res = max(res,dp[i][j]); 14 | } 15 | else{ 16 | dp[i][j] = 0; 17 | } 18 | } 19 | } 20 | return res; 21 | } 22 | int main(){ 23 | string s1 = "abcdgl"; 24 | string s2 = "abedgl"; 25 | cout< 2 | using namespace std; 3 | 4 | int Scs(string s1,string s2){ 5 | int m = s1.length(),n=s2.length(),lcs; 6 | int dp[m+1][n+1]; 7 | for(int i=0;i<=m;i++){ 8 | for(int j=0;j<=n;j++){ 9 | if(i==0 || j==0){ 10 | dp[i][j] = 0; 11 | } 12 | else if(s1[i-1] == s2[j-1]){ 13 | dp[i][j] = 1 + dp[i-1][j-1]; 14 | } 15 | else{ 16 | dp[i][j] = max(dp[i-1][j],dp[i][j-1]); 17 | } 18 | } 19 | } 20 | lcs = dp[m][n]; 21 | return m+n-lcs; 22 | } 23 | int main(){ 24 | string s1 = "abcdgl"; 25 | string s2 = "abedfhl"; 26 | cout< 2 | using namespace std; 3 | int MSDS(int a[],int size){ 4 | int dp[size]; 5 | for(int i=0;ia[j] && dp[i]max){ 18 | max = dp[i]; 19 | } 20 | } 21 | return max; 22 | } 23 | 24 | int main(){ 25 | int a[] = {1, 101, 2, 3, 100, 4, 5}; 26 | int size = sizeof(a)/sizeof(a[0]); 27 | reverse(a,a+size); 28 | cout< 2 | using namespace std; 3 | 4 | const int maxm=100000; 5 | int a[3][3]; 6 | int r=3,c=3; 7 | //One of the best problems come across. 8 | int MinmIniPoints(int a[][3]){ 9 | int dp[r+1]; 10 | for(int i=0;i<=r;i++){ 11 | dp[i]=INT_MAX; 12 | } 13 | dp[r-1]=1; 14 | for(int j=c-1;j>=0;j--){ 15 | for(int i=r-1;i>=0;i--){ 16 | dp[i]=min(dp[i],dp[i+1])-a[i][j]; 17 | dp[i]=max(1,dp[i]); 18 | } 19 | } 20 | return dp[0]; 21 | } 22 | int main(){ 23 | int a[3][3] = { {-2,-3,3}, 24 | {-5,-10,1}, 25 | {10,30,-5} 26 | }; 27 | cout< 2 | using namespace std; 3 | 4 | // problem : https://leetcode.com/problems/minimum-number-of-taps-to-open-to-water-a-garden/ 5 | 6 | class Solution { 7 | public: 8 | int minTaps(int n, vector& ranges) { 9 | int minm=0; 10 | int maxm=0; 11 | int open=0; 12 | while(maxm < n){ 13 | for(int i=0;imaxm){ 17 | maxm = r; 18 | } 19 | } 20 | if(minm == maxm) return -1; 21 | minm = maxm; 22 | open++; 23 | } 24 | return open; 25 | } 26 | }; -------------------------------------------------------------------------------- /Minm_Deletion_To_Make_Pallindrome.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int DelPallin(string s){ 5 | int n = s.length(),lps; 6 | int dp[n+1][n+1]; 7 | for(int i=0;i=0;i--){ 14 | for(int j=i+1;j 2 | using namespace std; 3 | 4 | #define int long long int 5 | 6 | signed main(){ 7 | int t;cin >> t; 8 | while( t--){ 9 | int n , k; cin >> n >> k ; 10 | vector prices(k); for ( int i = 0; i < k ; i++ ) cin >> prices[i]; 11 | vector dp( 1001, INT_MAX); 12 | dp[0] = 0; 13 | for ( int i =0 ; i < k ; i ++ ){ 14 | if ( prices[i] == -1) continue; 15 | for ( int weight = i+1 ; weight <= k ; weight ++) { 16 | dp[ weight ] = min( dp[weight - i - 1] + prices[i] , dp[weight]); 17 | } 18 | } 19 | if ( dp[k] == INT_MAX){ 20 | cout << -1 << "\n"; 21 | } 22 | else cout << dp[k]; 23 | } 24 | } -------------------------------------------------------------------------------- /Maximize_the_cutting.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int t; 6 | cin>>t; 7 | while(t--){ 8 | int n,x,y,z; 9 | cin>>n; 10 | cin>>x>>y>>z; 11 | int dp[n+1]; 12 | for(int i=0;i<=n;i++) dp[i]=-1; 13 | dp[0]=0; 14 | for(int i=0;i<=n;i++){ 15 | if(dp[i]==-1) continue; 16 | if(i+x<=n){ 17 | dp[i+x]=max(dp[i+x],dp[i]+1); 18 | } 19 | if(i+y<=n){ 20 | dp[i+y]=max(dp[i+y],dp[i]+1); 21 | } 22 | if(i+z<=n){ 23 | dp[i+z]=max(dp[i+z],dp[i]+1); 24 | } 25 | } 26 | for(int i=0;i<=n;i++){ 27 | cout< 2 | using namespace std; 3 | bool SubsetSum(int a[],int size,int sum) { 4 | bool dp[size+1][sum+1]; 5 | for(int i=0;i<=sum;i++){ 6 | dp[0][i] = false; 7 | } 8 | for(int i=0;i<=size;i++){ 9 | dp[i][0]=true; 10 | } 11 | for(int i=1;i<=size;i++){ 12 | for(int j=1;j<=sum;j++){ 13 | if(a[i-1] <= j) { 14 | dp[i][j] = dp[i-1][j] || dp[i-1][j-a[i-1]]; 15 | } 16 | else { 17 | dp[i][j] = dp[i-1][j]; 18 | } 19 | } 20 | } 21 | return dp[size][sum]; 22 | } 23 | 24 | int main() { 25 | int a[] = {3, 34, 4, 12, 5, 2}; 26 | int sum = 1; 27 | int size = sizeof(a)/sizeof a[0]; 28 | cout< 2 | using namespace std; 3 | 4 | int maxUncrossedLines(vector& A, vector& B,int n,int m) { 5 | vector>dp(m+1,vector(n+1,0)); 6 | for(int i=1;i<=m;i++){ 7 | for(int j=1;j<=n;j++){ 8 | if(A[i-1]==B[j-1]){ 9 | dp[i][j] = 1+dp[i-1][j-1]; 10 | } 11 | else{ 12 | dp[i][j]=max(dp[i-1][j],dp[i][j-1]); 13 | } 14 | } 15 | } 16 | return dp[m][n]; 17 | } 18 | 19 | 20 | int main(){ 21 | int m,n; 22 | cin>>m>>n; 23 | vectorA(m),B(n); 24 | for(int i=0;i>A[i]; 26 | } 27 | for(int i=0;i>B[i]; 29 | } 30 | cout< 2 | using namespace std; 3 | int knapsack(int wt[],int val[],int W,int size){ 4 | int dp[size+1][W+1]; 5 | for(int i=0;i<=size;i++){ 6 | for(int j=0;j<=W;j++){ 7 | if(i==0 || j==0){ 8 | dp[i][j] = 0; 9 | } 10 | else if(wt[i-1]<=j){ 11 | dp[i][j] = max(dp[i-1][j],val[i-1] + dp[i-1][j-wt[i-1]]); 12 | } 13 | else{ 14 | dp[i][j] = dp[i-1][j]; 15 | } 16 | } 17 | } 18 | return dp[size][W]; 19 | } 20 | 21 | int main() { 22 | 23 | int val[] = {60, 100, 120}; 24 | int wt[] = {10, 20, 30}; 25 | int W = 50; 26 | int size = sizeof(val)/sizeof(val[0]); 27 | cout< 2 | using namespace std; 3 | vectorv(100,0); 4 | vectordp(100,0); 5 | vectoradj[100]; 6 | int largest_sum(int child,int parent) { 7 | dp[child] = v[child]; 8 | int max_val = 0; 9 | for(auto ch : adj[child]) { 10 | if(ch==parent)continue; 11 | 12 | largest_sum(ch,child); 13 | max_val = max(max_val, dp[ch]); 14 | } 15 | return dp[child] += max_val; 16 | } 17 | int main() { 18 | int n; 19 | cin>>n; 20 | cout<<"Enter the nodes value\n"; 21 | for(int i=1;i<=n;i++) { 22 | cin>>v[i]; 23 | } 24 | for(int i=0;i>a>>b; 27 | adj[a].push_back(b); 28 | adj[b].push_back(a); 29 | } 30 | cout< 2 | using namespace std; 3 | 4 | int Minm_Coins(int coins[],int size,int sum) { 5 | int dp[size+1][sum+1]; 6 | for(int i=0;i<=sum;i++){ 7 | dp[0][i] = INT_MAX - 1; 8 | } 9 | for(int i=1;i<=size;i++){ 10 | dp[i][0] = 0; 11 | } 12 | for(int i=1;i<=size;i++){ 13 | for(int j=1;j<=sum;j++){ 14 | if(coins[i-1]<=j){ 15 | dp[i][j] = min(dp[i-1][j],1+dp[i][j-coins[i-1]]); 16 | } 17 | else{ 18 | dp[i][j] = dp[i-1][j]; 19 | } 20 | } 21 | } 22 | return dp[size][sum]; 23 | } 24 | int main(){ 25 | int coins[] = {1,6,5,2,8}; 26 | int sum = 11; 27 | int size = sizeof(coins)/sizeof(coins[0]); 28 | cout< 2 | using namespace std; 3 | int Lcs(string s1,string s2){ 4 | int m = s1.length(),n = s2.length(); 5 | int dp[m+1][n+1]; 6 | for(int i=0;i<=m;i++){ 7 | for(int j=0;j<=n;j++){ 8 | if(i==0 || j==0) dp[i][j]=0; 9 | else if(s1[i-1] == s2[j-1]){ 10 | dp[i][j] = 1 + dp[i-1][j-1]; 11 | } 12 | else{ 13 | dp[i][j] = max(dp[i-1][j],dp[i][j-1]); 14 | } 15 | } 16 | } 17 | for(int i=0;i<=m;i++){ 18 | for(int j=0;j<=n;j++){ 19 | cout< 2 | using namespace std; 3 | 4 | // problem : https://www.interviewbit.com/problems/maximum-sum-square-submatrix/ 5 | // very beautiful question 6 | 7 | int Solution::solve(vector > &A, int B) { 8 | int r = A.size(),c=A[0].size(); 9 | vector>dp(r+1,vector(c+1,0)); 10 | for(int i=1;i=0 && j-B >= 0){ 19 | sum = max(sum,dp[i][j]-dp[i-B][j]-dp[i][j-B]+dp[i-B][j-B]); 20 | } 21 | } 22 | } 23 | return sum; 24 | } 25 | -------------------------------------------------------------------------------- /Count_Pallindromic_SubString_Of_String_gfg.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int expandAroundCenter(string s,int left,int right){ 5 | if(s.length()==0 || left>right) return 0; 6 | int ans=0; 7 | while(left>=0 && right>t; 26 | while(t--){ 27 | int n; 28 | cin>>n; 29 | string s; 30 | cin>>s; 31 | cout< 2 | using namespace std; 3 | 4 | int Idconvert(string s1,string s2){ 5 | int m = s1.length(),n=s2.length(),lcs; 6 | int dp[m+1][n+1]; 7 | for(int i=0;i<=m;i++){ 8 | for(int j=0;j<=n;j++){ 9 | if(i==0 || j==0){ 10 | dp[i][j] = 0; 11 | } 12 | else if(s1[i-1] == s2[j-1]){ 13 | dp[i][j] = 1 + dp[i-1][j-1]; 14 | } 15 | else{ 16 | dp[i][j] = max(dp[i-1][j],dp[i][j-1]); 17 | } 18 | } 19 | } 20 | lcs = dp[m][n]; 21 | cout<<"Minimum Insertions : "< 2 | using namespace std; 3 | 4 | int expandAroundCenter(string s,int left,int right){ 5 | if(s.length()==0 || left>right) return 0; 6 | while(left>=0 && rightend-start){ 20 | start=i-(len-1)/2; 21 | end=i+len/2; 22 | } 23 | } 24 | return s.substr(start,end-start+1); 25 | } 26 | int main(){ 27 | string s = "helppreanadkada"; 28 | cout< 2 | using namespace std; 3 | 4 | //used bottom-up aproach(but similar to lcs) 5 | int longestPalindromeSubseq(string s) { 6 | int n=s.length(); 7 | int dp[n+1][n+1]; 8 | for(int i=0;i=0;i--){ 15 | for(int j=i+1;j 2 | using namespace std; 3 | 4 | int Edit_Distance(string s1,string s2){ 5 | int m = s1.length(); 6 | int n = s2.length(); 7 | int dp[m+1][n+1]; 8 | for(int i=0;i<=m;i++){ 9 | for(int j=0;j<=n;j++){ 10 | if(i==0) dp[i][j] = j; 11 | else if(j==0) dp[i][j] = i; 12 | else if(s1[i-1] == s2[j-1]){ 13 | dp[i][j] = dp[i-1][j-1]; 14 | } 15 | else{ 16 | dp[i][j] = 1 + min(dp[i-1][j-1],min(dp[i-1][j],dp[i][j-1])); 17 | } 18 | } 19 | } 20 | for(int i=0;i<=m;i++){ 21 | for(int j=0;j<=n;j++){ 22 | cout< 2 | using namespace std; 3 | 4 | bool EqualSubsetSum(int a[],int size) { 5 | int sum=0; 6 | for(int i=0;i 2 | using namespace std; 3 | 4 | // O(n^2) approach; 5 | int nCatlan(int n) { 6 | vectordp(n,0); 7 | dp[0] = dp[1] = 1; 8 | for(int i=2;i<=n;i++){ 9 | for(int j=0;j n-r) r = n-r; 22 | for(int i=0;i> n; 40 | cout< 2 | using namespace std; 3 | 4 | // problem : https://leetcode.com/problems/count-square-submatrices-with-all-ones 5 | 6 | class Solution { 7 | public: 8 | int countSquares(vector>& m) { 9 | if(m.empty()) return 0; 10 | int sum=0; 11 | int dp[m.size()][m[0].size()]; 12 | for(int i=0;i 2 | using namespace std; 3 | 4 | int CountSubsets(int a[],int size,int sum) { 5 | int dp[size+1][sum+1]; 6 | for(int i=0;i<=sum;i++){ 7 | dp[0][i] = 0; 8 | } 9 | for(int i=0;i<=size;i++){ 10 | dp[i][0]=1; 11 | } 12 | for(int i=1;i<=size;i++){ 13 | for(int j=1;j<=sum;j++){ 14 | if(a[i-1]<=j){ 15 | dp[i][j] = dp[i-1][j] + dp[i-1][j-a[i-1]]; 16 | } 17 | else { 18 | dp[i][j] = dp[i-1][j]; 19 | } 20 | } 21 | } 22 | for(int i=0;i<=size;i++){ 23 | for(int j=0;j<=sum;j++){ 24 | cout< 2 | using namespace std; 3 | int cost[1000][1000]; 4 | void solve(int n,int cost[][1000]){ 5 | int dp[n][n]; 6 | memset(dp,0,sizeof(dp)); 7 | for(int i=n-1;i>=0;i--){ 8 | for(int j=n-1;j>=0;j--){ 9 | int down=(i==n-1)?0:dp[i+1][j]; 10 | int down_left=(i==n-1 || j==0)?0:dp[i+1][j-1]; 11 | int down_right=(i==n-1 || j==n-1)?0:dp[i+1][j+1]; 12 | dp[i][j]= cost[i][j]+max(down,max(down_right,down_left)); 13 | } 14 | } 15 | int ans=dp[0][0]; 16 | for(int i=1;i>t; 24 | while(t--){ 25 | int n; 26 | cin>>n; 27 | for(int i=0;i>cost[i][j]; 30 | } 31 | } 32 | solve(n,cost); 33 | } 34 | } -------------------------------------------------------------------------------- /Unbounded_Knapsack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int knapsack(int wt[],int val[],int W,int size){ 4 | int dp[size+1][W+1]; 5 | for(int i=0;i<=size;i++){ 6 | for(int j=0;j<=W;j++){ 7 | if(i==0 || j==0){ 8 | dp[i][j] = 0; 9 | } 10 | else if(wt[i-1]<=j){ 11 | dp[i][j] = max(dp[i-1][j],val[i-1] + dp[i][j-wt[i-1]]); 12 | } 13 | else{ 14 | dp[i][j] = dp[i-1][j]; 15 | } 16 | } 17 | } 18 | for(int i=0;i<=size;i++){ 19 | for(int j=0;j<=W;j++){ 20 | cout< 2 | using namespace std; 3 | 4 | int Max_ways(int coins[],int size,int sum){ 5 | int dp[size+1][sum+1]; 6 | for(int i=0;i<=sum;i++){ 7 | dp[0][i]=0; 8 | } 9 | for(int i=0;i<=size;i++){ 10 | dp[i][0]=1; 11 | } 12 | for(int i=1;i<=size;i++){ 13 | for(int j=1;j<=sum;j++){ 14 | if(coins[i-1]<=j){ 15 | dp[i][j] = dp[i-1][j] + dp[i][j-coins[i-1]]; 16 | } 17 | else{ 18 | dp[i][j] = dp[i-1][j]; 19 | } 20 | } 21 | } 22 | for(int i=0;i<=size;i++){ 23 | for(int j=0;j<=sum;j++){ 24 | cout< 2 | using namespace std; 3 | 4 | int rodCutting(int cost[],int len[],int size) { 5 | int dp[size+1][size+1]; 6 | for(int i=0;i<=size;i++){ 7 | for(int j=0;j<=size;j++){ 8 | if(i==0 || j==0){ 9 | dp[i][j] = 0; 10 | } 11 | else if(len[i-1]<=j){ 12 | dp[i][j] = max(dp[i-1][j],cost[i-1] + dp[i][j-len[i-1]]); 13 | } 14 | else{ 15 | dp[i][j] = dp[i-1][j]; 16 | } 17 | } 18 | } 19 | for(int i=0;i<=size;i++){ 20 | for(int j=0;j<=size;j++){ 21 | cout< 2 | using namespace std; 3 | int main(){ 4 | int t; 5 | cin>>t; 6 | while(t--){ 7 | int m,n; 8 | cin>>n>>m; 9 | int gold[n][m]; 10 | for(int i=0;i>gold[i][j]; 13 | } 14 | } 15 | int dp[n][m]; 16 | int right,right_up,right_down; 17 | memset(dp,0,sizeof(dp)); 18 | for(int i=m-1;i>=0;i--){ 19 | for(int j=0;j 2 | using namespace std; 3 | 4 | // problem : https://www.lintcode.com/problem/flip-game-ii/ 5 | // very good question 6 | 7 | class Solution { 8 | public: 9 | bool canWin(string &s) { 10 | // write your code here 11 | if(s.length()<2) return false; 12 | return flipGame(s); 13 | } 14 | 15 | bool flipGame(string &s){ 16 | for(int i=0;i 2 | using namespace std; 3 | int Minm_SubsetSum_Diff(int a[],int size){ 4 | int sum = 0; 5 | for(int i=0;i=0;i--){ 27 | if(dp[size][i]==1){ 28 | diff = sum-2*i; 29 | break; 30 | } 31 | } 32 | return diff; 33 | } 34 | int main(){ 35 | int a[] = {3, 1, 4, 2, 2, 1}; 36 | int size = sizeof(a)/sizeof(a[0]); 37 | cout< productExceptSelf(vector& nums) { 6 | int n = nums.size(); 7 | vectorleft(n+1,1); 8 | vectorright(n+1,1); 9 | left[0] = nums[0]; 10 | right[n-1] = nums[n - 1 ]; 11 | for(int i = 1;i= 0 ; i--) { 15 | right[i] = right[i+1] * nums[i]; 16 | } 17 | vectorans(n); 18 | for(int i = 0;i 0 and i< n -1 ) { 26 | ans[i] = left[i - 1] * right[i + 1]; 27 | } 28 | 29 | } 30 | return ans; 31 | } 32 | }; -------------------------------------------------------------------------------- /num_products.cpp: -------------------------------------------------------------------------------- 1 | // https://codeforces.com/problemset/problem/1215/B 2 | 3 | #include 4 | using namespace std; 5 | vectord[2005]; 6 | void divisors() { 7 | for(int i = 1 ; i <= 2005 ; i++) { 8 | if(i != 1)d[i].push_back(1); 9 | for(int j = 2 ; j * j <= i ; j++) { 10 | if(i % j == 0) { 11 | d[i].push_back(j); 12 | if(i/j != j) d[i].push_back(i/j); 13 | } 14 | } 15 | } 16 | } 17 | 18 | int main() { 19 | #define int long long 20 | int n; 21 | cin>>n; 22 | vectora(n); 23 | for(int i = 0 ; i < n ;i++) { 24 | cin>>a[i]; 25 | } 26 | int a1 = 0, a2 = 0, neg = 0, pos = 0; 27 | for(int i = 0; i < n ;i++) { 28 | if(a[i] < 0) { 29 | swap(pos, neg); 30 | neg++; 31 | a1 += pos; 32 | a2 += neg; 33 | } else { 34 | pos++; 35 | a1 += pos; 36 | a2 += neg; 37 | } 38 | } 39 | cout< 2 | using namespace std; 3 | 4 | int Count_All_Distinct_Occurences(string s1,string s2){ 5 | int n=s1.length(),m=s2.length(); 6 | if(m>n) return 0; 7 | int dp[n+1][m+1]; 8 | for(int i=0;i<=m;i++) dp[0][i]=0; 9 | for(int i=0;i<=n;i++) dp[i][0]=1; 10 | for(int i=1;i<=n;i++){ 11 | for(int j=1;j<=m;j++){ 12 | if(s1[i-1]==s2[j-1]){ 13 | dp[i][j] = dp[i-1][j-1] + dp[i-1][j]; 14 | } 15 | else{ 16 | dp[i][j] = dp[i-1][j]; 17 | } 18 | } 19 | } 20 | for(int i=0;i<=n;i++){ 21 | for(int j=0;j<=m;j++){ 22 | cout<>t; 31 | while(t--){ 32 | string s1,s2; 33 | cin>>s1>>s2; 34 | cout< 2 | using namespace std; 3 | int main() { 4 | int t; 5 | cin>>t; 6 | while(t--) { 7 | int n; 8 | cin>>n; 9 | vectorv(n+1,0); 10 | for(int i=0;i>v[i]; 12 | } 13 | vectorLis(n+1,1); 14 | vectorLds(n+1,1); 15 | // cal longest increasing sequence 16 | for(int i=0;i=0;j--) { 18 | if(v[j]=0;i--) { 25 | for(int j = i+1;j 2 | using namespace std; 3 | string PrintLcs(string s1,string s2){ 4 | int m = s1.length(),n = s2.length(); 5 | int dp[m+1][n+1]; 6 | for(int i=0;i<=m;i++){ 7 | for(int j=0;j<=n;j++){ 8 | if(i==0 || j==0) dp[i][j]=0; 9 | else if(s1[i-1] == s2[j-1]){ 10 | dp[i][j] = 1 + dp[i-1][j-1]; 11 | } 12 | else{ 13 | dp[i][j] = max(dp[i-1][j],dp[i][j-1]); 14 | } 15 | } 16 | } 17 | int i=m,j=n; 18 | string ans=""; 19 | while(i>0 && j>0){ 20 | if(s1[i-1] == s2[j-1]){ 21 | ans.push_back(s1[i-1]); 22 | i--; 23 | j--; 24 | } 25 | else if(dp[i][j-1]>dp[i-1][j]){ 26 | j--; 27 | } 28 | else{ 29 | i--; 30 | } 31 | } 32 | reverse(ans.begin(),ans.end()); 33 | return ans; 34 | } 35 | 36 | int main(){ 37 | string s1 = "abcdgl"; 38 | string s2 = "abedfhl"; 39 | cout< 2 | using namespace std; 3 | 4 | //o(n^2) 5 | int Min_Jumps(int a[],int size){ 6 | int dp[size]; 7 | dp[0] = 0; 8 | for(int i=1;i=maxReach) return -1; 34 | step=maxReach-i; 35 | } 36 | } 37 | return -1; 38 | } 39 | 40 | int main(){ 41 | int a[] = { 1, 3, 6, 1, 0, 9 }; 42 | int size = sizeof(a) / sizeof(a[0]); 43 | cout< 2 | #define r 6 3 | #define c 5 4 | using namespace std; 5 | int Max_Square(int M[r][c]){ 6 | int dp[r][c],ans=0; 7 | for(int i=0;i 2 | using namespace std; 3 | 4 | // question link 5 | // https://codeforces.com/problemset/problem/987/C 6 | 7 | 8 | int main() { 9 | #define int long long 10 | int n; 11 | cin>>n; 12 | vectors(n + 1), rent(n + 1); 13 | for(int i = 1 ; i <= n ;i++) { 14 | cin>>s[i]; 15 | } 16 | for(int i = 1 ; i <= n ;i ++) { 17 | cin>>rent[i]; 18 | } 19 | 20 | vector>dp(n+1, vector(4, INT_MAX)); 21 | for(int i = 1 ; i <= n; i++) { 22 | dp[i][1] = rent[i]; 23 | } 24 | 25 | for(int k = 2 ; k <= 3 ;k++) { 26 | for(int i = 1 ; i <= n; i ++) { 27 | for(int j = 1 ; j < i ; j++) { 28 | if(s[j] < s[i]) { 29 | dp[i][k] = min(dp[i][k], dp[j][k-1] + rent[i]); 30 | } 31 | } 32 | } 33 | } 34 | 35 | int ans = INT_MAX; 36 | for(int i = 1 ; i <= n ;i++) { 37 | if(dp[i][3] != INT_MAX) { 38 | ans = min(ans, dp[i][3]); 39 | } 40 | } 41 | if(ans!=INT_MAX) 42 | cout< 2 | using namespace std; 3 | //O(n^2) solution 4 | int Count_AIS(int a[],int size){ 5 | int dp[size],ans=0; 6 | for(int i=0;ia[j]){ 12 | dp[i]+=dp[j]; 13 | } 14 | } 15 | } 16 | for(int i=0;i=0;j--){//max 10 times 31 | count[a[i]]+=count[j]; 32 | } 33 | count[a[i]]++; 34 | } 35 | for(int i=0;i<10;i++){ 36 | ans+=count[i]; 37 | } 38 | return ans; 39 | } 40 | 41 | int main(){ 42 | int a[]={3,2,4,5,4}; 43 | int size=sizeof(a)/sizeof(a[0]); 44 | cout< 2 | using namespace std; 3 | 4 | // problem : https://leetcode.com/problems/maximal-square/ 5 | 6 | class Solution { 7 | public: 8 | int maximalSquare(vector>& matrix) { 9 | if(matrix.empty()) return 0; 10 | int res=0; 11 | vector>m(matrix.size(),vector(matrix[0].size())); 12 | for(int i=0;i 2 | using namespace std; 3 | int minimumDeleteSum(string s1, string s2) { 4 | int m=s1.length(),n=s2.length(); 5 | int dp[m+1][n+1]; 6 | for(int i=0;i<=m;i++){ 7 | for(int j=0;j<=n;j++){ 8 | dp[i][j]=0; 9 | } 10 | } 11 | for(int i=1;i<=m;i++){ 12 | dp[i][0]=dp[i-1][0]+s1[i-1]; 13 | } 14 | for(int j=1;j<=n;j++){ 15 | dp[0][j]=dp[0][j-1]+s2[j-1]; 16 | } 17 | for(int i=1;i<=m;i++){ 18 | for(int j=1;j<=n;j++){ 19 | if(s1[i-1]==s2[j-1]){ 20 | dp[i][j]=dp[i-1][j-1]; 21 | } 22 | else{ 23 | dp[i][j]=min(dp[i-1][j]+s1[i-1],dp[i][j-1]+s2[j-1]); 24 | } 25 | } 26 | } 27 | for(int i=0;i<=m;i++){ 28 | for(int j=0;j<=n;j++){ 29 | cout< 2 | using namespace std; 3 | 4 | // problem : https://leetcode.com/problems/unique-paths-ii 5 | 6 | class Solution { 7 | public: 8 | int uniquePathsWithObstacles(vector>& obstacleGrid) { 9 | int n=obstacleGrid.size(),m=obstacleGrid[0].size(); 10 | vector>dp(n,vector(m,1)); 11 | int f=0; 12 | for(int i=0;i 2 | using namespace std; 3 | 4 | // given an array a difference of subset.find the count of that difference 5 | // approach ----------------> s1 + s2 = sum(arr) --eqn1 && s1-s2 = diff(given) ---eqn2; 6 | // solve eqn1 & eqn2 find s1 . s1 = (sum+diff)/2 7 | // therefore the problem reduces to count number of subset of with sum s1 8 | 9 | int count(vector &nums,int sum) { 10 | int n = nums.size(); 11 | vector>dp(n+1,vector(sum+1,0)); 12 | for(int i=0;i<=sum;i++){ 13 | dp[0][i] = 0; 14 | } 15 | for(int i=0;i<=n;i++){ 16 | dp[i][0] = 1; 17 | } 18 | for(int i=1;i<=n;i++){ 19 | for(int j=1;j<=sum;j++){ 20 | if(nums[i-1] <= j){ 21 | dp[i][j] = dp[i-1][j] + dp[i-1][j-nums[i-1]]; 22 | } 23 | else{ 24 | dp[i][j] = dp[i-1][j]; 25 | } 26 | } 27 | } 28 | return dp[n][sum]; 29 | } 30 | 31 | int main() { 32 | int n,diff,sum=0; 33 | cin>>n>>diff; 34 | vectornums(n); 35 | for(int i=0;i>nums[i]; 37 | sum += nums[i]; 38 | } 39 | int s1 = (sum+diff)/2; 40 | cout< 32 | using namespace std; 33 | #define ll long long int 34 | signed main() 35 | { 36 | while (true) 37 | { 38 | string s; 39 | cin >> s; 40 | if (s == "0")break; 41 | int n = s.size(); 42 | vector dp(n + 1, 0); 43 | dp[n-1]=1; 44 | if ( n>=2){ 45 | int x = (s[n-2] - '0') * 10 + s[n-1] - '0'; 46 | if ( x >= 10 && x <= 26 ) dp[n-2] =1; 47 | } 48 | int np = 0; 49 | for (int i = n - 2; i >= 0; i--){ 50 | if ( s[i+1] == '0' ) { 51 | if (s[i] == '0') np = 1; 52 | dp[i] += dp[i+2]; 53 | continue; 54 | } 55 | dp[i] += dp[i + 1]; 56 | int x = (s[i] - '0') * 10 + s[i + 1] - '0'; 57 | if (x >= 10 && x <= 26 && s[i+2] != '0') 58 | dp[i] += dp[i + 2]; 59 | } 60 | if ( np) cout << 0 << "\n"; 61 | else cout << dp[0] << "\n"; 62 | } 63 | } -------------------------------------------------------------------------------- /zerosAndOnes.cpp: -------------------------------------------------------------------------------- 1 | 2 | // link https://leetcode.com/problems/ones-and-zeroes/ 3 | #include 4 | using namespace std; 5 | class Solution { 6 | public: 7 | Solution(){} 8 | int findMaxForm(vector& strs, int m, int n) { 9 | int len = strs.size(); 10 | int dp[len+1][m+1][n+1]; 11 | memset(dp, 0, sizeof dp); 12 | 13 | for (int i=1;i<=len;i++){ 14 | int ones = count(strs[i-1].begin(), strs[i-1].end(), '1'); 15 | int zeros = strs[i-1].size()-ones; 16 | for (int j=0;j<=m;j++){ 17 | for (int k=0;k<=n;k++){ 18 | 19 | int res = dp[i-1][j][k]; 20 | if (zeros<=j && ones<=k) 21 | res = max(res, dp[i-1][j-zeros][k-ones]+1); 22 | dp[i][j][k] = res; 23 | } 24 | } 25 | } 26 | 27 | return dp[len][m][n]; 28 | 29 | } 30 | }; 31 | int main(){ 32 | Solution s; 33 | int m,n; 34 | cout<<"Enter the max zeros "; 35 | cin>>m; 36 | cout<<"Enter the max ones "; 37 | cin>>n; 38 | int strings; 39 | cout<<"Eneter the no of words "; 40 | cin>>strings; 41 | vectorwords; 42 | for(int i=0;i>str; 45 | words.push_back(str); 46 | } 47 | cout< 2 | #include 3 | #define INF 1000000 4 | using namespace std; 5 | 6 | int minm_Cost(int cost[],int n,int W){ 7 | vectorval,wt; 8 | int size=0; 9 | for(int i=0;i>t; 50 | while(t--){ 51 | int n,W; 52 | cin>>n>>W; 53 | int cost[n]; 54 | for(int i=0;i>cost[i]; 56 | } 57 | cout< 2 | using namespace std; 3 | 4 | struct Box{ 5 | int h; 6 | int d; 7 | int w; 8 | }; 9 | 10 | bool comparator(Box b1, Box b2){ 11 | return (b1.d*b1.w)>(b2.d*b2.w); 12 | } 13 | int maxStackHeight(int h[],int d[],int w[],int n){ 14 | struct Box ini[n]; 15 | for(int i=0;i 2 | using namespace std; 3 | 4 | 5 | int maxDotProduct(vector& A, vector& B,int size1,int size2) { 6 | vector>dp(size1,vector(size2)); 7 | for(int i=0;i=1 && j>=1) dp[i][j] = dp[i][j]+max(dp[i-1][j-1],0); 11 | if(i>=1) dp[i][j] = max(dp[i][j],dp[i-1][j]); 12 | if(j>=1) dp[i][j] = max(dp[i][j],dp[i][j-1]); 13 | } 14 | } 15 | return dp[size1-1][size2-1]; 16 | } 17 | 18 | 19 | int main(){ 20 | int size1,size2; 21 | cin>>size1>>size2; 22 | vectorA(size1); 23 | vectorB(size2); 24 | for(int i=0;i>A[i]; 26 | } 27 | for(int i=0;i>B[i]; 29 | } 30 | cout<& nums1, vector& nums2) { 37 | int n = int(nums1.size()), m = int(nums2.size()); 38 | vector> dp(n + 1, vector(m + 1, INT_MIN)); 39 | for (int i = 1; i <= n; ++i) { 40 | for (int j = 1; j <= m; ++j) { 41 | dp[i][j] = max(dp[i][j], dp[i - 1][j]); 42 | dp[i][j] = max(dp[i][j], dp[i][j - 1]); 43 | dp[i][j] = max(dp[i][j], dp[i - 1][j - 1]); 44 | dp[i][j] = max(dp[i][j], max(dp[i - 1][j - 1], 0) + nums1[i - 1] * nums2[j - 1]); 45 | } 46 | } 47 | return dp[n][m]; 48 | } 49 | }; -------------------------------------------------------------------------------- /Last_Stone_3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | //Leetcode Question 4 | 5 | //Recurssion base soln 6 | int helper(vector& s,int i){ 7 | if(i>=s.size()) return 0; 8 | else{ 9 | int ans=INT_MIN; 10 | ans=max(ans,s[i]-helper(s,i+1)); 11 | if(i+1& s) { 17 | int ans=helper(s,0); 18 | if(ans<0) return "Bob"; 19 | else if(ans==0) return "Tie"; 20 | return "Alice"; 21 | } 22 | //Memonization based soln 23 | int dp[500001]; 24 | int helper(vector&s,int i){ 25 | if(i>=s.size()) return 0; 26 | if(dp[i]!=-1) return dp[i]; 27 | else{ 28 | int ans=INT_MIN; 29 | ans=max(ans,s[i]-helper(s,i+1)); 30 | if(i+1& s) { 36 | for(int i=0;i& s) { 44 | int n=s.size(); 45 | vectordp(n+1,0); 46 | int i=n-1; 47 | while(i>=0){ 48 | int ans=INT_MIN; 49 | ans=max(ans,s[i]-dp[i+1]); 50 | if(i+1 2 | using namespace std; 3 | 4 | // problem : https://leetcode.com/problems/wiggle-subsequence 5 | 6 | // O(n^2) time and o(n) space 7 | class Solution { 8 | public: 9 | int wiggleMaxLength(vector& nums) { 10 | int n=nums.size(); 11 | if(n == 0) return 0; 12 | // up[i] refers to the length of the longest wiggle subsequence obtained so far considering ith element 13 | vectorup(n,1),down(n,1); 14 | for(int i=1;i nums[j]){ 17 | up[i] = max(up[i],1+down[j]); 18 | } 19 | else if(nums[i] < nums[j]){ 20 | down[i] = max(down[i],1+up[j]); 21 | } 22 | } 23 | } 24 | return max(up[n-1],down[n-1]); 25 | } 26 | }; 27 | 28 | 29 | // O(n) time and O(n) space 30 | 31 | class Solution { 32 | public: 33 | int wiggleMaxLength(vector& nums) { 34 | int n=nums.size(); 35 | if(n == 0) return 0; 36 | // up[i] refers to the length of the longest wiggle subsequence obtained so far considering ith element 37 | vectorup(n,1),down(n,1); 38 | for(int i=1;i nums[i-1]){ 40 | up[i] = 1+down[i-1]; 41 | down[i] = down[i-1]; 42 | } 43 | else if(nums[i] < nums[i-1]){ 44 | down[i] = 1+up[i-1]; 45 | up[i] = up[i-1]; 46 | } 47 | else{ 48 | down[i] = down[i-1]; 49 | up[i] = up[i-1]; 50 | } 51 | } 52 | return max(up[n-1],down[n-1]); 53 | } 54 | }; 55 | 56 | 57 | 58 | // O(n) ime and O(1) space 59 | class Solution { 60 | public: 61 | int wiggleMaxLength(vector& nums) { 62 | int n=nums.size(); 63 | if(n == 0) return 0; 64 | int up=1,down=1; 65 | for(int i=1;i nums[i-1]){ 67 | up = down+1; 68 | } 69 | else if(nums[i] < nums[i-1]){ 70 | down = up+1; 71 | } 72 | } 73 | return max(up,down); 74 | } 75 | }; -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Dynamic-Programming 2 | A DP a day keeps the bug away. 3 | 4 | **Cointains Dp Problems**

5 | 6 | **Parent Problem** : [*Kadane's Algorithm*](/Kadane.cpp)
7 | 1) [Best Time to Buy and sell Stock(Leetcode)](/Best_Time_To_Buy_And_Sell_Stock_.cpp)
8 | 2) [Circular Kadane](/Circular_Kadene.cpp)
9 | 10 | **Parent Problem** : *Fibonacci*.
11 | SubProblems :
12 | 1) [Reach a given Score](/Reach_a_given_score.cpp).
13 | 2) [Count number of hops](/Count_Number_of_hops.cpp).
14 | 3) [Count ways to reach n-th stair](/Count_ways_to_N'th_Stair.cpp).
15 | 4) [Count Number of Ways to Tile](/No_Of_Ways_To_Tile.cpp).
16 | 5) [Tiling Problem-General](/Tiling_Problem.cpp).
17 | 6) [Maximum Sum Dividing](/Maximum_Sum_Dividing_Recurssive.cpp).
18 | 7) [Friends Pairing Problem](/Friends_Pairing_Problem.cpp)
19 | 8) [Maximize the Cutting](/Maximize_the_cutting.cpp)
20 | 9) [Number of Ways to Decode a String](/Number_of_ways_to_decode_a_string.cpp) 21 | 22 | **Parent Problem** : [*0/1 KnapSack Problem*](/Knapsack_0_1.cpp)
23 | SubProblems :
24 | 1) [SubSet Sum](/SubsetSum.cpp).
25 | 2) [Equal Sum Partition](/Equal_Sum_Partition.cpp).
26 | 3) [Count of SubSet Sum]().
27 | 4) [Minimum SubSet Sum Difference](/Minimum_Subset_Sum_Difference.cpp).
28 | 5) [Count SubSets of given Sum](/Count_Subsets_of_given_Sum.cpp).

29 | 30 | **Parent Problem** : [*Unbounded KnapSack*](/Unbounded_Knapsack.cpp)
31 | SubProblems :
32 | 1) [Coin Changing Problem 1(No of ways)](/Coin_Changing_1.cpp).
33 | 2) [Coin Changing Problem 2(Minimum no of coins)](/Coin_Changing_2.cpp).
34 | 3) [Rod Cutting Problem](/Rod_Cutting.cpp).
35 | 4) [Minimum Cost to Fill Bag](/Minm_Cost_To_Fill_Bag.cpp).

36 | 37 | **Parent Problem** : [*Longest Common SubSequence*](/Longest_Common_Subsequence.cpp)
38 | SubProblems :
39 | 1) [Longest Common SubString](/Longest_Common_Substring.cpp).
40 | 2) [Shortest Common SuperSequence](/Shortest_Common_Supersequence.cpp).
41 | 3) [Print LCS](/Print_Longest_Common_Subsequence.cpp).
42 | 4) [Minimum Insertions And Deletion to Convert a String](/Minm_Insertion_Deletion_Convert.cpp).
43 | 5) [Longest Repeating SubSequence](/Longest_Repeating_Subsequence.cpp).
44 | 6) [Longest Pallindromic SubSequence](/Longest_Pallindromic_Subsequence.cpp).
45 | 7) [Edit Distance](/Edit_Distance.cpp).
46 | 8) [Count All Pallindromic SubSequence](/Count_All_Pallindromic_Subsequence.cpp).
47 | 9) [Minimum Deletion to make string pallindrome](/Minm_Deletion_To_Make_Pallindrome.cpp).
48 | 10) [Count Distinct Occcurences](/Distinct_Occurences.cpp).
49 | 11) [Maximum length of pairs](/Maximum_length_of_pairs.cpp).
50 | 12) [Minimum Ascii Sum deletion To Make two Strings equal](/Minimum_Ascii_Sum_deletion_To_Make_two_Strings_equal.cpp).
51 | 13) [Maximum Dot Product of Subsequences(Leetcode)](/Maximum_Dot_Product_Subsequence.cpp).
52 | 14) [Uncrossed Lines(LeetCode)](/Uncrossed_Lines.cpp).

53 | 54 | **Parent Problem** : [*Longest Increasing Subsequence*](/Longest_Increasing_Subsequence.cpp)
55 | 1) [Russian Doll Envelope(Leetcode)](/Russian_Doll_Envelope.cpp).
56 | 2) [Longest Decreasing SubSequence](/Longest_Decreasing_Subsequence.cpp).
57 | 3) [Maximum Sum Increasing SubSequence](/Maximum_Sum_Increasing_Subsequence.cpp).
58 | 4) [Maximum Sum Decreasing SubSequence](/Maximum_Sum_Decreasing_Subsequence.cpp).
59 | 5) [Maximum Product Increasing Subsequence](/Maximum_Product_Increasing_Subsequence.cpp).
60 | 6) [Minimum Jumps to reach end](/Minimum_Jumps_To_Reach_End.cpp).
61 | 7) [Box Stacking Problem](/Box_Stacking_Problem.cpp).
62 | 8) [Longest Bitonic Sequence](/BitonicSequence.cpp).
63 | 9) [Longest Alternating Subsequence](/longest_alternating_subsequence.cpp)
64 | 10) [Count All Increasing Subsequences](/Count_All_Increasing_Subsequences.cpp).
65 | 66 | **Parent Problem** : *DP + Greedy*
67 | 1) [Jump Game-2(Leetcode)](/jump_game_2..cpp).
68 | 2) [Video Stiching(Leetcode)](/video_stiching.cpp).
69 | 3) [Minm number of taps to open water](/minm_number_of_taps_to_open_water.cpp).
70 | 71 | **Parent Problem** : *Game(DP) + minimax*
72 | 1) [Nim Game](/nim_game.cpp).
73 | 2) [Flip Game-1](/flip_game.cpp).
74 | 3) [Flip Game-2](/flip_game2.cpp).
75 | 4) [Get Minimum Squares](/Get_Minimum_Squares.cpp).
76 | 5) [Optimal Strategy to play a Game](/Optimal_Strategy_To_Play_A_Game.cpp).
77 | 6) [Stone Game-3](/Last_Stone_3.cpp)
78 | 7) [Jump Game-3(Leetcode)](/Jump_Game_3.cpp)
79 | 8) [Jump Game-5(Leetcode)](/Jump_Game_5.cpp)
80 | 81 | **Parent Problem** : *Dp on Grids*
82 | 1) [Maximum Size Submatrix Square](/Maximum_Size_Submatrix_Square.cpp).
83 | 2) [Maximum Path Sum in Triangle](/Max_Path_Sum_in_Triangle.cpp).
84 | 3) [Minimum Initial Points to Reach Destination](/Minimum_Initial_Points_To_Reach_Destinaion.cpp).
85 | 4) [GoldMine Problem](/GoldMine_Problem.cpp)
86 | 5) [Path in a Matrix](/Max_path_sum.cpp)
87 | 6) [Maximal Square(Leetcode)](/maximal_square.cpp)
88 | 7) [Count Submatrices with all ones(Leetcode)](/count_submatrices_with_all_ones.cpp)
89 | 8) [Maximum Sum Submatrix](/maximum_sum_submatrix.cpp)
90 | 9) [Unique Path-1](/unique_paths_1.cpp)
91 | 10) [Unique Path-2](/unique_paths_2.cpp)
92 | 93 | 94 | **Parent Problem** : [*Matrix Chain Multiplication*](/Matrix_Chain_Multiplication.cpp)
95 | 96 | **Parent Problem** : [*Count Distinct SubSequences*](/Count_Distinct_Subsequences.cpp)
97 | 98 | **Parent Problem** : *Dp on Trees*
99 | 1) [Max Path Sum](/Max_path_sum.cpp)
100 | 2) [Maximum Path Sum in a Tree from Any node to any other node](/Maximum_Path_Sum_in_Binary_tree.cpp)
101 | 3) [Diameter of a Binary Tree](/Diameter_Of_Binary_Tree.cpp)
102 | 103 | **Others** 104 | 1) [Longest Consequetive Subsequences](/Longest_Consequetive_Subsequence.cpp).
105 | 2) [Zeroes And Ones](/zerosAndOnes.cpp) 106 | 3) [Longest Pallidromic SubString](/Longest_Pallindromic_Substring.cpp).
107 | 4) [Count Pallindromic substring of string](/Count_Pallindromic_SubString_Of_String_gfg.cpp)
108 | 5) [Product of array except self](/Count_Pallindromic_SubString_Of_String_gfg.cpp) 109 | 110 | Happy Coding!! 111 | 112 | 113 | 114 | --------------------------------------------------------------------------------