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

Commit 6b9b8b0

Browse files
Dynamic programming (#944)
* Dynamic programming * Changes * removed executable files
1 parent a51bdff commit 6b9b8b0

File tree

11 files changed

+328
-0
lines changed

11 files changed

+328
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import java.util.Scanner;
2+
3+
public class ClimbStairs1 {
4+
5+
public static int solution(int n) {
6+
int[] dp = new int[n + 1];
7+
// in dp array dp[i] represents no of ways to reach ith stair
8+
// we assume to reach 0th steps we need 1 steps
9+
dp[0] = 1;
10+
for (int i = 1; i <= n; i++) {
11+
if (i == 1) {
12+
// handling edge case
13+
dp[i] = dp[i - 1];
14+
continue;
15+
}
16+
// we can reach the ith step from (i-2)th step by taking 2steps or from (i-1)th
17+
// step by taking 1 step
18+
dp[i] = dp[i - 1] + dp[i - 2];
19+
}
20+
return dp[n];
21+
22+
}
23+
24+
public static void main(String[] args) {
25+
Scanner sc = new Scanner(System.in);
26+
int n = sc.nextInt();
27+
int ans = solution(n);
28+
System.out.println(ans);
29+
sc.close();
30+
31+
}
32+
33+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//this problem is very similar to climbStairs1
2+
//But in this problem instead of 1 or 2 steps we can Take variable steps
3+
//we given an array jumps where jump[i] represents from ith step we can go to i+jumps[i] ith steps;
4+
//we have to start from 0th step and reach the nth step using jumps
5+
6+
public class ClimbStairs2 {
7+
public static int getNoOfWays(int n, int[] jumps) {
8+
int[] dp = new int[n + 1];
9+
// dp[i] represents no of ways to reach nth step from ith steps
10+
// we assume we can reach nth step in one way from nth step itself
11+
dp[n] = 1;
12+
for (int i = n - 1; i >= 0; i--) {
13+
for (int j = 1; j <= jumps[i] && i + j < dp.length; j++) {
14+
dp[i] += dp[i + j];
15+
}
16+
}
17+
return dp[0];
18+
}
19+
20+
public static void main(String[] args) {
21+
int n = 6; // number of steps;
22+
int[] jumps = { 2, 3, 0, 1, 2, 3 };
23+
int ans = getNoOfWays(n, jumps);
24+
System.out.println(ans);
25+
26+
}
27+
28+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//in ClimbStairs3 we have to find minimum no of jumps required to reach nth level
2+
3+
import java.util.*;
4+
5+
public class ClimbStairs3 {
6+
public static int getNoOfWays(int n, int[] jumps) {
7+
int[] dp = new int[n + 1];
8+
Arrays.fill(dp, Integer.MAX_VALUE);
9+
// dp[i] represents no of ways to reach nth step from ith steps
10+
// we know that no of jumps required to reach nth step from itself is 0
11+
dp[n] = 0;
12+
for (int i = n - 1; i >= 0; i--) {
13+
for (int j = 1; j <= jumps[i] && i + j < dp.length; j++) {
14+
// find the least number of jumps requried to reach nth step
15+
dp[i] = Math.min(dp[i], dp[i + j]);
16+
}
17+
if (dp[i] != Integer.MAX_VALUE)
18+
dp[i]++; // taking a jump from ith step
19+
}
20+
return dp[0];
21+
}
22+
23+
public static void main(String[] args) {
24+
int n = 6; // number of steps;
25+
int[] jumps = { 2, 3, 0, 1, 2, 3 };
26+
int ans = getNoOfWays(n, jumps);
27+
if (ans == Integer.MAX_VALUE) {
28+
System.out.println("Cannot Reach");
29+
} else
30+
System.out.println(ans);
31+
32+
}
33+
34+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//this is dynamic programming approach for coin change problem
2+
//we are given an array coins and int target
3+
// we have tp return minimum number of coins which can make up target equal to target
4+
public class CoinChange1 {
5+
public static int getMinCoins(int[] coins, int target) {
6+
int[] dp = new int[target + 1];
7+
// dp[i] represents minimum number of coins required to make up target = i;
8+
9+
for (int i = 1; i < dp.length; i++) {
10+
int min = Integer.MAX_VALUE;
11+
for (int j = 0; j < coins.length; j++) {
12+
if (coins[j] > i) // if coin's value is more than required target then we ignore it
13+
continue;
14+
min = Math.min(min, dp[i - coins[j]]); // finding minimum number of coins to make up amount = i
15+
}
16+
if (min == Integer.MAX_VALUE || min == 100000) // if we cannnot make amount = i then we mark it as invalid
17+
dp[i] = 100000;
18+
else
19+
dp[i] = min + 1; // else we add one coin to the min no of coins needed
20+
}
21+
22+
return dp[target] == 100000 ? -1 : dp[target];
23+
}
24+
25+
public static void main(String[] args) {
26+
int[] coins = { 1, 2, 5 };
27+
int target = 11;
28+
int ans = getMinCoins(coins, target);
29+
System.out.println(ans);
30+
}
31+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//In this problem we have to find out no of ways we can make up amount equals to the target
2+
3+
public class CoinChange2 {
4+
public static int getWays(int[] coins, int target) {
5+
int[] dp = new int[target + 1]; // dp[i] represents number of ways to make up amount equals to i
6+
// no of ways to make up amount equals to 0 is 1
7+
dp[0] = 1;
8+
for (int i = 0; i < coins.length; i++) {
9+
for (int j = coins[i]; j < dp.length; j++) {
10+
dp[j] += dp[j - coins[i]];
11+
12+
}
13+
}
14+
return dp[target];
15+
}
16+
17+
public static void main(String[] args) {
18+
int[] coins = { 1, 2, 5 };
19+
int target = 7;
20+
int ans = getWays(coins, target);
21+
System.out.println(ans);
22+
}
23+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// we are given an array of number nums and an Integer target
2+
// we have to return if we can choose any subset of nums whose sum is equal to target
3+
4+
public class CombinationSum1 {
5+
public static boolean solve(int target, int[] nums) {
6+
boolean[][] dp = new boolean[nums.length + 1][target + 1];
7+
// dp[i][j] represents , is it possible to select subset from nums till index i
8+
// to make sum equal to j;
9+
// first row represents the empty subset;
10+
// we can make sum equal to 0 with empty subset;
11+
for (int i = 0; i < nums.length; i++)
12+
dp[i][0] = true;
13+
14+
for (int i = 1; i <= nums.length; i++) {
15+
for (int j = 1; j <= target; j++) {
16+
if (nums[i - 1] <= j) {
17+
dp[i][j] = dp[i - 1][j] | dp[i - 1][j - nums[i - 1]]; // consideing both inclusive and exclusive
18+
} else {
19+
dp[i][j] = dp[i - 1][j];
20+
}
21+
}
22+
}
23+
return dp[nums.length][target];
24+
25+
}
26+
27+
public static void main(String[] args) {
28+
int[] nums = { 4, 2, 7, 1, 3 };
29+
int target = 10;
30+
boolean ans = solve(target, nums);
31+
System.out.println(ans);
32+
33+
}
34+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//In this problem repetition of numbers are allowed
2+
3+
public class CombinationSum2 {
4+
public static boolean solve(int[] nums, int target) {
5+
boolean[] dp = new boolean[target + 1];
6+
dp[0] = true; // we can make sum equal to 0 with an empty subset
7+
for (int i = 1; i < dp.length; i++) {
8+
for (int j = 0; j < nums.length; j++) {
9+
if (nums[j] <= i) {
10+
dp[i] |= dp[i - nums[j]];
11+
}
12+
}
13+
}
14+
15+
return dp[target];
16+
}
17+
18+
public static void main(String[] args) {
19+
int[] nums = { 4, 2, 7, 9, 3 };
20+
int target = 10;
21+
boolean ans = solve(nums, target);
22+
System.out.println(ans);
23+
}
24+
25+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// We are a Professional Robber and we have to rob maximum loot from n houses
2+
// along a street
3+
// But we cannnot steal from two adjacent houses otherwise we would get caught
4+
// we are given loot array where loot[i] represents ith house contains loot[i] money
5+
6+
public class HouseRobber1 {
7+
public static int getMaxLoot(int n, int[] loot) {
8+
int[] dp = new int[n + 1];
9+
// dp represents dp[i] represents max loot we can achieve till ith house;
10+
dp[0] = 0; // loot from 0 houses is 0
11+
dp[1] = loot[0]; // loot from 1st house
12+
for (int i = 2; i <= n; i++) {
13+
// we can either loot previous house or current house
14+
// we are maximizing out loot at every house
15+
dp[i] = Math.max(dp[i - 1], dp[i - 2] + loot[i - 1]);
16+
17+
}
18+
return dp[n];
19+
}
20+
21+
public static void main(String[] args) {
22+
int n = 5; /// no of houses
23+
int[] loot = { 2, 7, 9, 3, 1 }; // loots in the houses
24+
int ans = getMaxLoot(n, loot);
25+
System.out.println(ans);
26+
27+
}
28+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//This problem is very similar to HouseRobber1
2+
//But in this problem house are arranged in circular manner
3+
//that means 0th house is adjacent to n-1th house
4+
public class HouseRobber2 {
5+
public static int getMaxLoot(int n, int[] loot) {
6+
// we will use two dp arrays inorder to solve this problem
7+
// suppose we have 10 houses 1 to 10
8+
// if we steal from house 1 then we cannot steal from house 10 , and for houses
9+
// 2 to 9 will be same as HouseRobber1
10+
// if we leave house1 then we can steal from house 10 and for houses 2 to 9 will
11+
// be same as HouseRobber1
12+
int[] dp1 = new int[n + 1];
13+
int[] dp2 = new int[n + 1];
14+
15+
// stealing from house1
16+
dp1[0] = 0;
17+
dp1[1] = loot[0];
18+
// leaving the first hosue
19+
dp2[2] = 0;
20+
dp2[2] = 0;
21+
for (int i = 2; i <= n; i++) {
22+
dp1[i] = Math.max(dp1[i - 1], dp1[i - 2] + loot[i - 1]);
23+
dp2[i] = Math.max(dp2[i - 1], dp2[i - 2] + loot[i - 1]);
24+
}
25+
26+
return Math.max(dp1[n - 1], dp2[n]);
27+
}
28+
29+
public static void main(String[] args) {
30+
int n = 5; /// no of houses
31+
int[] loot = { 2, 7, 9, 3, 1 }; // loots in the houses
32+
int ans = getMaxLoot(n, loot);
33+
System.out.println(ans);
34+
}
35+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
public class BoundedKnapSack {
2+
public static int solve(int W, int N, int[] val, int[] wt) {
3+
int[][] dp = new int[N + 1][W + 1];
4+
// dp[i][j] represents max value achieved with sack of capacity j and i-1 items;
5+
for (int i = 1; i <= N; i++) {
6+
for (int j = 1; j <= W; j++) {
7+
if (wt[i - 1] <= j) {
8+
dp[i][j] = Math.max(dp[i - 1][j], val[i - 1] + dp[i - 1][j - wt[i - 1]]);
9+
} else {
10+
dp[i][j] = dp[i - 1][j];
11+
}
12+
13+
}
14+
}
15+
16+
return dp[N][W];
17+
18+
}
19+
20+
public static void main(String[] args) {
21+
int W = 4; // capacity of sack
22+
int N = 3; // No of items
23+
int[] val = { 1, 2, 3 }; // values of items;
24+
int[] wt = { 3, 5, 1 }; // weights of items
25+
26+
int ans = solve(W, N, val, wt);
27+
System.out.println(ans);
28+
}
29+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//this is similar to KnapSack but Here we can repeat the items
2+
//this problem is quite similar to coinChange2
3+
4+
public class UnboundedKS {
5+
public static int solve(int W, int N, int[] val, int[] wt) {
6+
int[] dp = new int[W + 1];
7+
// dp[i] represents max Value achieved with sack of capacity of i
8+
for (int i = 0; i <= W; i++) {
9+
for (int j = 0; j < N; j++) {
10+
if (wt[j] <= i) {
11+
dp[i] = Math.max(dp[i], val[j] + dp[i - wt[j]]);
12+
}
13+
}
14+
15+
}
16+
17+
return dp[W];
18+
}
19+
20+
public static void main(String[] args) {
21+
int W = 4; // capacity of sack
22+
int N = 3; // No of items
23+
int[] val = { 1, 2, 3 }; // values of items;
24+
int[] wt = { 3, 5, 1 }; // weights of items
25+
int ans = solve(W, N, val, wt);
26+
System.out.println(ans);
27+
}
28+
}

0 commit comments

Comments
 (0)