Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Set of Leetcode Problems based on basic Dynamic Programming Approach

# Problem Link - https://leetcode.com/study-plan/dynamic-programming/?progress
45 changes: 45 additions & 0 deletions 02. Algorithms/09. Dynamic Programming/001. Target Sum/code.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
class Solution {
public:
//This question is same as count of number of subsets with given difference
int countSubsets(vector<int>& nums, int n, int M)
{
int t[n + 1][M + 1];

for(int i = 0; i <= n; i++)
{
for(int j = 0; j <= M; j++)
{
if(i == 0)
t[i][j] = 0;
if(j == 0)
t[i][j] = 1;
}
}

for(int i = 1; i <= n; i++)
{
for(int j = 0; j <= M; j++)
{
if(nums[i - 1] <= j)
t[i][j] = t[i - 1][j - nums[i - 1]] + t[i - 1][j];
else
t[i][j] = t[i - 1][j];
}
}

return t[n][M];
}

int findTargetSumWays(vector<int>& nums, int target)
{
int n = nums.size();
int sum = 0;
for(int i = 0; i < n; i++)
sum += nums[i];

int M = (sum + target)/2;
if(sum < abs(target) || (sum + target) % 2 != 0)
return 0;
return countSubsets(nums, n, M);
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Set of Leetcode Problems based on basic Dynamic Programming Approach

# Problem Link - https://leetcode.com/study-plan/dynamic-programming/?progress
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class Solution {
public:
/*vector<int>dp ;

Solution(): dp(46,-1){
dp[0]=1 ;
}

int climbStairs(int n) {

if(n<0)
return 0;

if(dp[n]!=-1)
return dp[n] ;

return dp[n] = climbStairs(n-1)+climbStairs(n-2);
}*/

int climbStairs(int n)
{
if(n==1)
return 1 ;

int dp[n+1];

dp[0] = 1;
dp[1] = 1;

for(int i=2;i<=n;i++)
{
dp[i] = dp[i-1]+dp[i-2] ;
}

return dp[n] ;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Set of Leetcode Problems based on basic Dynamic Programming Approach

# Problem Link - https://leetcode.com/study-plan/dynamic-programming/?progress
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution {
public:
int maxProfit(vector<int>& prices) {
int cost = INT_MAX, sell=0;

for(int i:prices)
{
cost = min(cost,i);
sell = max(sell,i-cost) ;
}

return sell ;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Set of Leetcode Problems based on basic Dynamic Programming Approach

# Problem Link - https://leetcode.com/study-plan/dynamic-programming/?progress
17 changes: 17 additions & 0 deletions 02. Algorithms/09. Dynamic Programming/005. House Robber/code.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
public:
int rob(vector<int>& nums) {
int sz = nums.size();
vector<int> dp(sz);
for (int i = 0; i < sz; i++) {
if (i == 0) {
dp[i] = nums[0];
} else if (i == 1) {
dp[i] = max(nums[1], nums[0]);
} else {
dp[i] = max(dp[i-1], dp[i-2] + nums[i]);
}
}
return dp[sz-1];
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Set of Leetcode Problems based on basic Dynamic Programming Approach

# Problem Link - https://leetcode.com/study-plan/dynamic-programming/?progress
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Solution {
public:
int rob(vector<int>& nums) {
int n = nums.size() ;
if(n==1)
return nums[0] ;
if(n==2)
return max(nums[0],nums[1]) ;

int dp1[n-1] ;

dp1[0] = nums[0] ;
dp1[1] = max(nums[0],nums[1]) ;
for(int i=2;i<n-1;i++) {
dp1[i] = max(dp1[i-1],nums[i]+dp1[i-2]);
}

int dp2[n-1] ;

dp2[0] = nums[1] ;
dp2[1] = max(nums[2],nums[1]) ;
for(int i=2;i<n-1;i++) {
dp2[i] = max(dp2[i-1],nums[i+1]+dp2[i-2]);
}

return max(dp1[n-2],dp2[n-2]) ;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Set of Leetcode Problems based on basic Dynamic Programming Approach

# Problem Link - https://leetcode.com/study-plan/dynamic-programming/?progress
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
public:
int fib(int n) {

if(n==0 || n==1)
return n;
int dp[n+1];
dp[0] = 0;
dp[1] = 1;

for(int i=2;i<=n;i++)
dp[i] = dp[i-1]+dp[i-2] ;

return dp[n] ;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Set of Leetcode Problems based on basic Dynamic Programming Approach

# Problem Link - https://leetcode.com/study-plan/dynamic-programming/?progress
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public:
int tribonacci(int n) {
if(n==0 || n==1)
return n;

if(n==2)
return 1;

int dp[n+1];

dp[0]=0;dp[1]=1;dp[2]=1;

for(int i=3;i<=n;i++)
dp[i] = dp[i-1]+dp[i-2]+dp[i-3] ;

return dp[n];
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Set of Leetcode Problems based on basic Dynamic Programming Approach

# Problem Link - https://leetcode.com/study-plan/dynamic-programming/?progress
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class Solution {
public:

int min(int a, int b) {
return a<b?a:b ;
}

int solve(vector<int>cost, vector<int>&dp, int index) {
if(index>=cost.size())
return 0 ;

if(dp[index]!=-1)
return dp[index] ;

return dp[index] = cost[index] + min(solve(cost,dp,index+1), solve(cost,dp,index+2)) ;
}

int minCostClimbingStairs(vector<int>& cost) {
int n = cost.size() ;

/*vector<int>dp(n+1,-1) ;
return min(solve(cost,dp,0), solve(cost,dp,1)) ;*/

int dp[n] ;

dp[0] = cost[0] ;
dp[1] = cost[1] ;

for(int i=2;i<n;i++) {
dp[i] = cost[i] + min(dp[i-1],dp[i-2]) ;
}

return min(dp[n-1],dp[n-2]) ;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Set of Leetcode Problems based on basic Dynamic Programming Approach

# Problem Link - https://leetcode.com/study-plan/dynamic-programming/?progress
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
public:
int deleteAndEarn(vector<int>& nums) {
vector<int> cnt(10002);
for(int i=0;i<nums.size();i++)
{
cnt[nums[i]]+=nums[i];
}

vector<int> dp(10002);
dp[1]=cnt[1];
dp[2]=max(cnt[1],cnt[2]);
for(int i=3;i<dp.size();i++)
{
dp[i]=max(dp[i-2]+cnt[i],dp[i-1]);
}

return max(dp[10000],dp[9999]);
}
};