From 3c5ea8a67f9da97be06c454e4e34c1c84cdf64bc Mon Sep 17 00:00:00 2001 From: enigma-aaa Date: Wed, 12 Jun 2024 15:32:16 +0530 Subject: [PATCH] Added Solutions for DSA day14 --- ...uy and Sell Stock with Transaction Fee.cpp | 26 +++++++++++ problem-of-the-day/day14/D_Knapsack_1.cpp | 29 ++++++++++++ problem-of-the-day/day14/E_Knapsack_2.cpp | 44 +++++++++++++++++++ problem-of-the-day/day14/Edit_Distance.cpp | 39 ++++++++++++++++ .../Longest Increasing Subsequence N2.cpp | 19 ++++++++ .../Longest Increasing Subsequence NlogN.cpp | 27 ++++++++++++ .../day14/Matrix Chain Multiplication.cpp | 23 ++++++++++ 7 files changed, 207 insertions(+) create mode 100644 problem-of-the-day/day14/Best Time to Buy and Sell Stock with Transaction Fee.cpp create mode 100644 problem-of-the-day/day14/D_Knapsack_1.cpp create mode 100644 problem-of-the-day/day14/E_Knapsack_2.cpp create mode 100644 problem-of-the-day/day14/Edit_Distance.cpp create mode 100644 problem-of-the-day/day14/Longest Increasing Subsequence N2.cpp create mode 100644 problem-of-the-day/day14/Longest Increasing Subsequence NlogN.cpp create mode 100644 problem-of-the-day/day14/Matrix Chain Multiplication.cpp diff --git a/problem-of-the-day/day14/Best Time to Buy and Sell Stock with Transaction Fee.cpp b/problem-of-the-day/day14/Best Time to Buy and Sell Stock with Transaction Fee.cpp new file mode 100644 index 0000000..193f846 --- /dev/null +++ b/problem-of-the-day/day14/Best Time to Buy and Sell Stock with Transaction Fee.cpp @@ -0,0 +1,26 @@ +class Solution { +public: +long long dp[100002][2]; + long long solve (vector& nums,int i,int flag,int k) + { + if(i>=nums.size())return 0; + long long take=0; + if(dp[i][flag]!=-1)return dp[i][flag]; + if(flag==0) + { + take+= max(nums[i]+f(nums,i+1,1,k),f(nums,i+1,flag,k)); + } + if(flag==1) + { + take+= max(-nums[i]-k+f(nums,i+1,0,k),f(nums,i+1,flag,k)); + //deductiong concession fee k + } + return dp[i][flag] = take; + + } + int maxProfit(vector& nums, int fee) { + memset(dp,-1,sizeof(dp)); + return f(nums,0,1,fee); + + } +}; \ No newline at end of file diff --git a/problem-of-the-day/day14/D_Knapsack_1.cpp b/problem-of-the-day/day14/D_Knapsack_1.cpp new file mode 100644 index 0000000..0879bf7 --- /dev/null +++ b/problem-of-the-day/day14/D_Knapsack_1.cpp @@ -0,0 +1,29 @@ +#include +using namespace std; +#define ll long long + +// Time Complexity: O(n*w) + +int main() +{ + ll n , w ; + cin>>n>>w; + vectorweight(n) , value(n); + for(int i = 0 ; i>weight[i]>>value[i]; + } + vector>dp(n+1 , vector(w+1 , 0)); + for(int i = 1 ; i<=n ; i++) + { + for(int j = 1 ; j<=w ; j++) + { + if(weight[i-1] <= j) + { + dp[i][j] = max(dp[i][j] , value[i-1] + dp[i-1][j-weight[i-1]]); + } + dp[i][j] = max(dp[i][j] , dp[i-1][j]); + } + } + cout< + using namespace std; + #define ll long long + + // Time Complexity: O(n*sum(v)) + // IN dp[i][j] , we store the minimum weight required to get value j using first i items + + int main() + { + ll n , w ; + cin>>n>>w; + vectorweight(n) , value(n); + for(int i = 0 ; i>weight[i]>>value[i]; + } + ll sum = 0; + for(auto x : value) + { + sum+=x; + } + vector>dp(n+1 , vector(sum+1 , INT_MAX)); + dp[0][0] = 0; + for(int i = 1 ; i<=n ; i++) + { + for(int j = 0 ; j<=sum ; j++) + { + dp[i][j] = dp[i-1][j]; + if(j>=value[i-1]) + { + dp[i][j] = min(dp[i][j] , weight[i-1] + dp[i-1][j-value[i-1]]); + } + } + } + ll ans = 0; + for(int i = 0 ; i<=sum ; i++) + { + if(dp[n][i]<=w) + { + ans = i; + } + } + cout< +using namespace std; +#define ll long long + +int main() +{ + string a,b ; + cin>>a>>b; + ll s1 = a.size() ; + ll s2 = b.size() ; + vector>dp(s1 + 1 , vector(s2 + 1 , INT_MAX)) ; + for(int i = 0 ; i<=s1 ; i++) + { + dp[i][0] = i; + } + for(int i = 0 ; i<=s2 ; i++) + { + dp[0][i] = i; + } + for(int i = 1 ; i<=s1 ; i++) + { + for(int j = 1 ; j<=s2 ; j++) + { + dp[i][j] = min(dp[i][j],1 + dp[i-1][j]); + dp[i][j] = min(dp[i][j],1 + dp[i][j-1]); + dp[i][j] = min(dp[i][j] , (a[i-1]!=b[j-1]) + dp[i-1][j-1]); + } + } + // for(auto x : dp) + // { + // for(auto y : x) + // { + // cout<& nums) { + int n = nums.size(); + vectordp(n,1); + // dp[i] stores the length of longest increasing subsequence ending at i + for(int i = 1 ; i=0 ; j--) + { + if(nums[i]>nums[j]) + { + dp[i] = max(dp[i] , 1 + dp[j]); + } + } + } + return *max_element(dp.begin() , dp.end()); + } +}; \ No newline at end of file diff --git a/problem-of-the-day/day14/Longest Increasing Subsequence NlogN.cpp b/problem-of-the-day/day14/Longest Increasing Subsequence NlogN.cpp new file mode 100644 index 0000000..d1774d3 --- /dev/null +++ b/problem-of-the-day/day14/Longest Increasing Subsequence NlogN.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + // Time Complexity: O(nlogn) + // Space Complexity: O(n) + + int lengthOfLIS(vector& nums) { + int n = nums.size() ; + vectorlis; + lis.push_back(nums[0]); + for(int i = 0 ; i> dp(N, vector(N)); + + // Initialize the main diagonal to 0 + for (int i = 1; i < N; i++) { + dp[i][i] = 0; + } + + // L is the chain length + for (int len = 2; len < N; len++) { + for (int i = 1; i < N - len + 1; i++) { + int j = i + len - 1; + dp[i][j] = INT_MAX; + for (int k = i; k <= j - 1; k++) { + int q = dp[i][k] + dp[k + 1][j] + arr[i - 1] * arr[k] * arr[j]; + dp[i][j] = min(dp[i][j], q); + } + } + } + + return dp[1][N - 1]; + } +}; \ No newline at end of file