I. Algorithms

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Steps of solving DP problem

1) Identify if it is a DP problem 2) Decide a state expression with least parameters 3) Formulate state relationship 4) Do tabulation (or add memoization)

Dynamic programming

Break down a problem into smaller and smaller subproblems. At their lowest levels, the subproblems are solved and their answers stored in memory. These saved answers are used again with other larger (sub)problems which may call for a recomputation of the same information for their own answer. Reusing the stored answers allows for optimization by combining the answers of previously solved subproblems.

Problem: Bytelandian gold coins In Byteland they have a very strange monetary system. Each Bytelandian gold coin has an integer number written on it. A coin n can be exchanged in a bank into three coins: n/2, n/3 and n/4. But these numbers are all rounded down (the banks have to make a profit). You can also sell Bytelandian coins for American dollars. The exchange rate is 1:1. But you can not buy Bytelandian coins. You have one gold coin. What is the maximum amount of American dollars you can get for it?

DP[n]= max{n, DP[n\2]+DP[n\3]+DP[n\4]} DP[n], n - initial coin value DP[0] = 0 DP[2] = 2 DP[2]= max{2, DP[1]+DP[0]+DP[0]}=2 Arrays.fill(cache, -1)

When to use dynamic programming

Overlapping sub problems. Dynamic Programming is mainly used when solutions of same subproblems are needed again and again.

How to classify a problem as a Dynamic Programming Problem?

Typically, all the problems that require to maximize or minimize certain quantity or counting problems that say to count the arrangements under certain condition or certain probability problems can be solved by using Dynamic Programming. All dynamic programming problems satisfy the overlapping subproblems property and most of the classic dynamic problems also satisfy the optimal substructure property. Once, we observe these properties in a given problem, be sure that it can be solved using DP. https://www.geeksforgeeks.org/solve-dynamic-programming-problem/

Problem: ACODE - Alphacode

if (n < 0) { return 0; } if (n == 1 || n == 0) { return 1; } int result = 0; if (code.charAt(n-1) == '0') { result = solve(code, n-2, cache); } else if (code.charAt(n-2) == '0') { result = solve(code, n-1, cache); } else { int lasttwo = Integer.parseInt(code.substring(n-2, n)); if (lasttwo > 26) { result = solve (code, n-1, cache); } else { result = solve(code, n-1, cache) + solve(code, n-2, cache); } } cache[n] = result; return result;

Problem: Given 3 numbers {1, 3, 5}, we need to tell the total number of ways we can form a number 'N' using the sum of the given three numbers. (allowing repetitions and different arrangements).

state(n) = state(n-1) + state(n-3) + state(n-5) https://www.geeksforgeeks.org/solve-dynamic-programming-problem/


Ensembles d'études connexes

Science 902 self test 3 study guide

View Set

Registration & Licensing Study Questions

View Set

GCP - Data Engineer Certification

View Set

Principles of Management_Chapter 7

View Set