0% found this document useful (0 votes)
16 views

Dynamic Programming Solutions

Uploaded by

kaushikanirudh38
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Dynamic Programming Solutions

Uploaded by

kaushikanirudh38
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Dynamic Programming Solutions

DP_DAY_1.pdf
PDF

solve these programs in dynamic programming and in java language

Here are Java solutions for the given dynamic programming problems:

1. Enchanted Tower: Unique Ways to Climb the Tower

This problem is a variation of the Fibonacci sequence.

java

public class EnchantedTower {


public static int climbStairs(int n) {
if (n <= 2) return n;
int[] dp = new int[n + 1];
dp[1] = 1;
dp[2] = 2;
for (int i = 3; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];
}

public static void main(String[] args) {


int n = 3; // Example input
System.out.println(climbStairs(n)); // Output: 3
}
}

2. Magical Mountain Journey: Minimum Toll

This problem is similar to the "minimum cost to climb stairs."

java

public class MagicalMountain {


public static int minToll(int[] toll) {
int n = toll.length;
if (n == 1) return toll[0];
int[] dp = new int[n + 1];
dp[0] = 0;
dp[1] = toll[0];
for (int i = 2; i <= n; i++) {
dp[i] = Math.min(dp[i - 1], dp[i - 2]) + toll[i - 1];
}
return Math.min(dp[n], dp[n - 1]);
}

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 1/3
public static void main(String[] args) {
int[] toll = {10, 15, 20}; // Example input
System.out.println(minToll(toll)); // Output: 25
}
}

3. The Great Vault Escape: Maximum Gold Without Triggering Alarms

This problem is the "house robber" problem.

java

public class GreatVaultEscape {


public static int maxGold(int[] coins) {
int n = coins.length;
if (n == 0) return 0;
if (n == 1) return coins[0];
int[] dp = new int[n];
dp[0] = coins[0];
dp[1] = Math.max(coins[0], coins[1]);
for (int i = 2; i < n; i++) {
dp[i] = Math.max(dp[i - 1], dp[i - 2] + coins[i]);
}
return dp[n - 1];
}

public static void main(String[] args) {


int[] coins = {2, 7, 9, 3, 1}; // Example input
System.out.println(maxGold(coins)); // Output: 12
}
}

4. Three-Dragon Prophecy: Dragon Strength in n-th Generation

This problem is a variation of the Tribonacci sequence.

java

public class ThreeDragonProphecy {


public static int dragonStrength(int n) {
if (n == 1 || n == 2) return 1;
if (n == 3) return 2;
int[] dp = new int[n + 1];
dp[1] = 1;
dp[2] = 1;
dp[3] = 2;
for (int i = 4; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2] + dp[i - 3];
}
return dp[n];
}

public static void main(String[] args) {


int n = 6; // Example input
System.out.println(dragonStrength(n)); // Output: 13
}
}

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 2/3
These solutions use dynamic programming to solve the problems efficiently. Let me know if you need
further explanations or modifications!

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 3/3

You might also like