-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
enigma-aaa
committed
Jun 12, 2024
1 parent
64d97db
commit 3c5ea8a
Showing
7 changed files
with
207 additions
and
0 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
problem-of-the-day/day14/Best Time to Buy and Sell Stock with Transaction Fee.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
class Solution { | ||
public: | ||
long long dp[100002][2]; | ||
long long solve (vector<int>& 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<int>& nums, int fee) { | ||
memset(dp,-1,sizeof(dp)); | ||
return f(nums,0,1,fee); | ||
|
||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
#define ll long long | ||
|
||
// Time Complexity: O(n*w) | ||
|
||
int main() | ||
{ | ||
ll n , w ; | ||
cin>>n>>w; | ||
vector<ll>weight(n) , value(n); | ||
for(int i = 0 ; i<n ; i++) | ||
{ | ||
cin>>weight[i]>>value[i]; | ||
} | ||
vector<vector<ll>>dp(n+1 , vector<ll>(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<<dp[n][w]<<endl; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#include <bits/stdc++.h> | ||
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; | ||
vector<ll>weight(n) , value(n); | ||
for(int i = 0 ; i<n ; i++) | ||
{ | ||
cin>>weight[i]>>value[i]; | ||
} | ||
ll sum = 0; | ||
for(auto x : value) | ||
{ | ||
sum+=x; | ||
} | ||
vector<vector<ll>>dp(n+1 , vector<ll>(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<<ans<<endl; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
#define ll long long | ||
|
||
int main() | ||
{ | ||
string a,b ; | ||
cin>>a>>b; | ||
ll s1 = a.size() ; | ||
ll s2 = b.size() ; | ||
vector<vector<ll>>dp(s1 + 1 , vector<ll>(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<<y<<" "; | ||
// } | ||
// cout<<endl; | ||
// } | ||
cout<<dp[s1][s2]<<endl; | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
problem-of-the-day/day14/Longest Increasing Subsequence N2.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class Solution { | ||
public: | ||
int lengthOfLIS(vector<int>& nums) { | ||
int n = nums.size(); | ||
vector<int>dp(n,1); | ||
// dp[i] stores the length of longest increasing subsequence ending at i | ||
for(int i = 1 ; i<n ; i++) | ||
{ | ||
for(int j = i-1 ; j>=0 ; j--) | ||
{ | ||
if(nums[i]>nums[j]) | ||
{ | ||
dp[i] = max(dp[i] , 1 + dp[j]); | ||
} | ||
} | ||
} | ||
return *max_element(dp.begin() , dp.end()); | ||
} | ||
}; |
27 changes: 27 additions & 0 deletions
27
problem-of-the-day/day14/Longest Increasing Subsequence NlogN.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
class Solution { | ||
public: | ||
// Time Complexity: O(nlogn) | ||
// Space Complexity: O(n) | ||
|
||
int lengthOfLIS(vector<int>& nums) { | ||
int n = nums.size() ; | ||
vector<int>lis; | ||
lis.push_back(nums[0]); | ||
for(int i = 0 ; i<n ; i++) | ||
{ | ||
if(lis.back() < nums[i]) | ||
{ | ||
lis.push_back(nums[i]); | ||
} | ||
else | ||
{ | ||
int ind = lower_bound(lis.begin() , lis.end() , nums[i]) - lis.begin() ; | ||
if(lis[ind] != nums[i]) | ||
{ | ||
lis[ind] = nums[i]; | ||
} | ||
} | ||
} | ||
return lis.size(); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
int matrixMultiplication(int N, int arr[]) { | ||
vector<vector<int>> dp(N, vector<int>(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]; | ||
} | ||
}; |