Skip to content
This repository has been archived by the owner on Jun 2, 2024. It is now read-only.

Commit

Permalink
Dynamic programming (#944)
Browse files Browse the repository at this point in the history
* Dynamic programming

* Changes

* removed executable files
  • Loading branch information
Nabeelcodes110 authored Aug 15, 2023
1 parent a51bdff commit 6b9b8b0
Show file tree
Hide file tree
Showing 11 changed files with 328 additions and 0 deletions.
33 changes: 33 additions & 0 deletions Java/Algorithm/DynamicProgramming/ClimbStairs/ClimbStairs1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import java.util.Scanner;

public class ClimbStairs1 {

public static int solution(int n) {
int[] dp = new int[n + 1];
// in dp array dp[i] represents no of ways to reach ith stair
// we assume to reach 0th steps we need 1 steps
dp[0] = 1;
for (int i = 1; i <= n; i++) {
if (i == 1) {
// handling edge case
dp[i] = dp[i - 1];
continue;
}
// we can reach the ith step from (i-2)th step by taking 2steps or from (i-1)th
// step by taking 1 step
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];

}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int ans = solution(n);
System.out.println(ans);
sc.close();

}

}
28 changes: 28 additions & 0 deletions Java/Algorithm/DynamicProgramming/ClimbStairs/ClimbStairs2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//this problem is very similar to climbStairs1
//But in this problem instead of 1 or 2 steps we can Take variable steps
//we given an array jumps where jump[i] represents from ith step we can go to i+jumps[i] ith steps;
//we have to start from 0th step and reach the nth step using jumps

public class ClimbStairs2 {
public static int getNoOfWays(int n, int[] jumps) {
int[] dp = new int[n + 1];
// dp[i] represents no of ways to reach nth step from ith steps
// we assume we can reach nth step in one way from nth step itself
dp[n] = 1;
for (int i = n - 1; i >= 0; i--) {
for (int j = 1; j <= jumps[i] && i + j < dp.length; j++) {
dp[i] += dp[i + j];
}
}
return dp[0];
}

public static void main(String[] args) {
int n = 6; // number of steps;
int[] jumps = { 2, 3, 0, 1, 2, 3 };
int ans = getNoOfWays(n, jumps);
System.out.println(ans);

}

}
34 changes: 34 additions & 0 deletions Java/Algorithm/DynamicProgramming/ClimbStairs/ClimbStairs3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//in ClimbStairs3 we have to find minimum no of jumps required to reach nth level

import java.util.*;

public class ClimbStairs3 {
public static int getNoOfWays(int n, int[] jumps) {
int[] dp = new int[n + 1];
Arrays.fill(dp, Integer.MAX_VALUE);
// dp[i] represents no of ways to reach nth step from ith steps
// we know that no of jumps required to reach nth step from itself is 0
dp[n] = 0;
for (int i = n - 1; i >= 0; i--) {
for (int j = 1; j <= jumps[i] && i + j < dp.length; j++) {
// find the least number of jumps requried to reach nth step
dp[i] = Math.min(dp[i], dp[i + j]);
}
if (dp[i] != Integer.MAX_VALUE)
dp[i]++; // taking a jump from ith step
}
return dp[0];
}

public static void main(String[] args) {
int n = 6; // number of steps;
int[] jumps = { 2, 3, 0, 1, 2, 3 };
int ans = getNoOfWays(n, jumps);
if (ans == Integer.MAX_VALUE) {
System.out.println("Cannot Reach");
} else
System.out.println(ans);

}

}
31 changes: 31 additions & 0 deletions Java/Algorithm/DynamicProgramming/CoinChange/CoinChange1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//this is dynamic programming approach for coin change problem
//we are given an array coins and int target
// we have tp return minimum number of coins which can make up target equal to target
public class CoinChange1 {
public static int getMinCoins(int[] coins, int target) {
int[] dp = new int[target + 1];
// dp[i] represents minimum number of coins required to make up target = i;

for (int i = 1; i < dp.length; i++) {
int min = Integer.MAX_VALUE;
for (int j = 0; j < coins.length; j++) {
if (coins[j] > i) // if coin's value is more than required target then we ignore it
continue;
min = Math.min(min, dp[i - coins[j]]); // finding minimum number of coins to make up amount = i
}
if (min == Integer.MAX_VALUE || min == 100000) // if we cannnot make amount = i then we mark it as invalid
dp[i] = 100000;
else
dp[i] = min + 1; // else we add one coin to the min no of coins needed
}

return dp[target] == 100000 ? -1 : dp[target];
}

public static void main(String[] args) {
int[] coins = { 1, 2, 5 };
int target = 11;
int ans = getMinCoins(coins, target);
System.out.println(ans);
}
}
23 changes: 23 additions & 0 deletions Java/Algorithm/DynamicProgramming/CoinChange/CoinChange2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//In this problem we have to find out no of ways we can make up amount equals to the target

public class CoinChange2 {
public static int getWays(int[] coins, int target) {
int[] dp = new int[target + 1]; // dp[i] represents number of ways to make up amount equals to i
// no of ways to make up amount equals to 0 is 1
dp[0] = 1;
for (int i = 0; i < coins.length; i++) {
for (int j = coins[i]; j < dp.length; j++) {
dp[j] += dp[j - coins[i]];

}
}
return dp[target];
}

public static void main(String[] args) {
int[] coins = { 1, 2, 5 };
int target = 7;
int ans = getWays(coins, target);
System.out.println(ans);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// we are given an array of number nums and an Integer target
// we have to return if we can choose any subset of nums whose sum is equal to target

public class CombinationSum1 {
public static boolean solve(int target, int[] nums) {
boolean[][] dp = new boolean[nums.length + 1][target + 1];
// dp[i][j] represents , is it possible to select subset from nums till index i
// to make sum equal to j;
// first row represents the empty subset;
// we can make sum equal to 0 with empty subset;
for (int i = 0; i < nums.length; i++)
dp[i][0] = true;

for (int i = 1; i <= nums.length; i++) {
for (int j = 1; j <= target; j++) {
if (nums[i - 1] <= j) {
dp[i][j] = dp[i - 1][j] | dp[i - 1][j - nums[i - 1]]; // consideing both inclusive and exclusive
} else {
dp[i][j] = dp[i - 1][j];
}
}
}
return dp[nums.length][target];

}

public static void main(String[] args) {
int[] nums = { 4, 2, 7, 1, 3 };
int target = 10;
boolean ans = solve(target, nums);
System.out.println(ans);

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//In this problem repetition of numbers are allowed

public class CombinationSum2 {
public static boolean solve(int[] nums, int target) {
boolean[] dp = new boolean[target + 1];
dp[0] = true; // we can make sum equal to 0 with an empty subset
for (int i = 1; i < dp.length; i++) {
for (int j = 0; j < nums.length; j++) {
if (nums[j] <= i) {
dp[i] |= dp[i - nums[j]];
}
}
}

return dp[target];
}

public static void main(String[] args) {
int[] nums = { 4, 2, 7, 9, 3 };
int target = 10;
boolean ans = solve(nums, target);
System.out.println(ans);
}

}
28 changes: 28 additions & 0 deletions Java/Algorithm/DynamicProgramming/HouseRobber/HouseRobber1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// We are a Professional Robber and we have to rob maximum loot from n houses
// along a street
// But we cannnot steal from two adjacent houses otherwise we would get caught
// we are given loot array where loot[i] represents ith house contains loot[i] money

public class HouseRobber1 {
public static int getMaxLoot(int n, int[] loot) {
int[] dp = new int[n + 1];
// dp represents dp[i] represents max loot we can achieve till ith house;
dp[0] = 0; // loot from 0 houses is 0
dp[1] = loot[0]; // loot from 1st house
for (int i = 2; i <= n; i++) {
// we can either loot previous house or current house
// we are maximizing out loot at every house
dp[i] = Math.max(dp[i - 1], dp[i - 2] + loot[i - 1]);

}
return dp[n];
}

public static void main(String[] args) {
int n = 5; /// no of houses
int[] loot = { 2, 7, 9, 3, 1 }; // loots in the houses
int ans = getMaxLoot(n, loot);
System.out.println(ans);

}
}
35 changes: 35 additions & 0 deletions Java/Algorithm/DynamicProgramming/HouseRobber/HouseRobber2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//This problem is very similar to HouseRobber1
//But in this problem house are arranged in circular manner
//that means 0th house is adjacent to n-1th house
public class HouseRobber2 {
public static int getMaxLoot(int n, int[] loot) {
// we will use two dp arrays inorder to solve this problem
// suppose we have 10 houses 1 to 10
// if we steal from house 1 then we cannot steal from house 10 , and for houses
// 2 to 9 will be same as HouseRobber1
// if we leave house1 then we can steal from house 10 and for houses 2 to 9 will
// be same as HouseRobber1
int[] dp1 = new int[n + 1];
int[] dp2 = new int[n + 1];

// stealing from house1
dp1[0] = 0;
dp1[1] = loot[0];
// leaving the first hosue
dp2[2] = 0;
dp2[2] = 0;
for (int i = 2; i <= n; i++) {
dp1[i] = Math.max(dp1[i - 1], dp1[i - 2] + loot[i - 1]);
dp2[i] = Math.max(dp2[i - 1], dp2[i - 2] + loot[i - 1]);
}

return Math.max(dp1[n - 1], dp2[n]);
}

public static void main(String[] args) {
int n = 5; /// no of houses
int[] loot = { 2, 7, 9, 3, 1 }; // loots in the houses
int ans = getMaxLoot(n, loot);
System.out.println(ans);
}
}
29 changes: 29 additions & 0 deletions Java/Algorithm/DynamicProgramming/KnapSack/BoundedKnapSack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
public class BoundedKnapSack {
public static int solve(int W, int N, int[] val, int[] wt) {
int[][] dp = new int[N + 1][W + 1];
// dp[i][j] represents max value achieved with sack of capacity j and i-1 items;
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= W; j++) {
if (wt[i - 1] <= j) {
dp[i][j] = Math.max(dp[i - 1][j], val[i - 1] + dp[i - 1][j - wt[i - 1]]);
} else {
dp[i][j] = dp[i - 1][j];
}

}
}

return dp[N][W];

}

public static void main(String[] args) {
int W = 4; // capacity of sack
int N = 3; // No of items
int[] val = { 1, 2, 3 }; // values of items;
int[] wt = { 3, 5, 1 }; // weights of items

int ans = solve(W, N, val, wt);
System.out.println(ans);
}
}
28 changes: 28 additions & 0 deletions Java/Algorithm/DynamicProgramming/KnapSack/UnboundedKS.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//this is similar to KnapSack but Here we can repeat the items
//this problem is quite similar to coinChange2

public class UnboundedKS {
public static int solve(int W, int N, int[] val, int[] wt) {
int[] dp = new int[W + 1];
// dp[i] represents max Value achieved with sack of capacity of i
for (int i = 0; i <= W; i++) {
for (int j = 0; j < N; j++) {
if (wt[j] <= i) {
dp[i] = Math.max(dp[i], val[j] + dp[i - wt[j]]);
}
}

}

return dp[W];
}

public static void main(String[] args) {
int W = 4; // capacity of sack
int N = 3; // No of items
int[] val = { 1, 2, 3 }; // values of items;
int[] wt = { 3, 5, 1 }; // weights of items
int ans = solve(W, N, val, wt);
System.out.println(ans);
}
}

0 comments on commit 6b9b8b0

Please sign in to comment.