Dynamic Programming
Dynamic Programming
1. Given a positive integer n, the task is to find the nth Fibonacci number.
The Fibonacci sequence is a sequence where the next term is the sum
of the previous two terms. The first two terms of the Fibonacci sequence
are 0 followed by 1. The Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21
Example:
Input: n = 2
Output: 1
Explanation: 1 is the 2nd number of Fibonacci series.
Input: n = 5
Output: 5
Explanation: 5 is the 5th number of Fibonacci series.
2. The tribonacci series is a generalization of the Fibonacci sequence where
each term is the sum of the three preceding terms.
a(n) = a(n − 1) + a(n − 2) + a(n − 3) with a(0) = a(1) = 0, a(2) = 1. First
few numbers in the Tribonacci Sequence are 0, 0, 1, 1, 2, 4, 7, 13, 24, 44, 81, 149, 274, 504, . . .
Given a value n, task is to print first n Tribonacci Numbers.
Examples:
Input: 5
Output: 0, 0, 1, 1, 2
Input: 10
Output: 0, 0, 1, 1, 2, 4, 7, 13, 24, 44
Input: 20
Output: 0, 0, 1, 1, 2, 4, 7, 13, 24, 44, 81, 149, 274, 504, 927, 1705, 3136, 5768, 10609, 19513
3. Lucas numbers are similar to Fibonacci numbers. Lucas numbers are
also defined as the sum of its two immediately previous terms. But here
the first two terms are 2 and 1 whereas in Fibonacci numbers the first two
terms are 0 and 1 respectively.
Mathematically, Lucas Numbers may be defined as:
2
if n = 0
Ln = 1 if n = 1 (1)
Ln−1 + Ln−2 if n > 1
1
The Lucas numbers are in the following integer sequence: 2, 1, 3, 4, 7, 11, 18, 29, 47, 76, 123, . . .
Write a function int lucas(int n) with n as an argument and returns the
nth Lucas number.
Examples:
Input: 3
Output: 4
Input: 7
Output: 29
4. There are n stairs, and a person standing at the bottom wants to climb
stairs to reach the top. The person can climb either 1 stair or 2 stairs at
a time, the task is to count the number of ways that a person can reach
at the top.
Note: This problem is similar to Count ways to reach N th stair (Or-
der does not matter) with the only difference that in this problem, we
count all distinct ways where different orderings of the steps are considered
unique.
Examples:
Input: n = 1
Output: 1
Explanation: There is only one way to climb 1 stair.
Input: n = 2
Output: 2
Explanation: There are two ways to reach 2th stair: {1, 1} and {2}.
Input: n = 4
Output: 5
Explanation: There are five ways to reach 4th stair: {1, 1, 1, 1}, {1, 1, 2},
{2, 1, 1}, {1, 2, 1} and {2, 2}.
5. A child is running up a staircase with n steps and can hop either 1 step,
2 steps, or 3 steps at a time. The task is to implement a method to count
how many possible ways the child can run up the stairs.
Examples:
Input: 4
Output: 7
Explanation: There are seven ways: {1, 1, 1, 1}, {1, 2, 1}, {2, 1, 1}, {1, 1, 2},
{2, 2}, {3, 1}, {1, 3}.
Input: 3
Output: 4
Explanation: There are four ways: {1, 1, 1}, {1, 2}, {2, 1}, {3}.
2
6. Given an array of integers cost[] of length n, where cost[i] is the cost of
the ith step on a staircase. Once the cost is paid, we can either climb
one or two steps. We can either start from the step with index 0, or the
step with index 1. The task is to find the minimum cost to reach the top.
Examples:
Input: cost[] = [10, 15, 20]
Output: 15
Explanation: The cheapest option is to start from step with index 1,
pay cost[1] and go to the top.
7. Given a rod of length n, the task is to cut the rod in such a way that
the total number of segments of length x, y, and z is maximized.
The segments can only be of length x, y, and z.
3
Explanation: Total length is 4, and the cut lengths are 2, 1 and 1. We
can make maximum 4 segments each of length 1.
Input: n = 5, x = 5, y = 3, z = 2
Output: 2
Explanation: Here total length is 5, and the cut lengths are 5, 3 and 2.
We can make two segments of lengths 3 and 2.
Input: n = 7, x = 8, y = 9, z = 10
Output: 0
Explanation: Here the total length is 7, and the cut lengths are 8, 9, and
10. We cannot cut the segment into lengths that fully utilize the segment,
so the output is 0.
8. Catalan numbers are defined as a mathematical sequence that consists of
positive integers, which can be used to find the number of possibilities of
various combinations. The nth term in the sequence denoted Cn , is found
in the following formula:
(2n)!
(2)
(n + 1)!n!
The first few Catalan numbers for n = 0, 1, 2, 3, 4, 5, . . . are: 1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, . . .
and so on.
Catalan numbers occur in many interesting counting problems
like the following.
(a) Count the number of expressions containing n pairs of parentheses
that are correctly matched.
(b) Count the number of possible Binary Search Trees with n keys
(c) Count the number of full binary trees (A rooted binary tree is full if
every vertex has either two children or no children) with n + 1 leaves.
(d) Given a number n, return the number of ways you can draw n chords
in a circle with 2 × n points such that no 2 chords intersect.
Examples:
Input: n = 6
Output: 132
Explanation: C(6) = C(0)C(5) + C(1)C(4) + C(2)C(3) + C(3)C(2) +
C(4)C(1) + C(5)C(0) = 132
Input: n = 8
Output: 1430 Explanation: C(8) = C(0)C(7)+C(1)C(6)+C(2)C(5)+
C(3)C(4) + C(4)C(3) + C(5)C(2) + C(6)C(1) + C(7)C(0) = 1430
Input: n = 5
Output: 42
Explanation: C(5) = C(0)C(4) + C(1)C(3) + C(2)C(2) + C(3)C(1) +
C(4)C(0) = 42
4
9. Given an integer n, the task is to find the total number of unique BSTs
that can be made using values from 1 to n.
Examples:
Input: n = 3
Output: 5
Explanation: For n = 3, preorder traversal of Unique BSTs are:
123,
132,
213,
312,
321
10. Given a number n, the task is to find the number of valid parentheses
expressions of that length.
Examples:
Input: 2
Output: 1
Explanation: There is only possible valid expression of length 2, ”()”
Input: 4
Output: 2
Explanation: Possible valid expression of length 4 are ”(())” and ”()()”
Input: 6
Output: 5
Explanation: Possible valid expressions are ”((()))”, ”(()())”, ”(())()”,
”()(())”, and ”()()()”
11. There are n houses built in a line, each of which contains some money in
it. A robber wants to steal money from these houses, but he can’t steal
from two adjacent houses. The task is to find the maximum amount
of money which can be stolen.
Examples:
Input: hval[] = {6, 7, 1, 3, 8, 2, 4}
Output: 19
Explanation: The thief will steal from house 1, 3, 5 and 7, total money
= 6 + 1 + 8 + 4 = 19.
Input: hval[] = {5, 3, 4, 11, 2}
Output: 16
Explanation: Thief will steal from house 1 and 4, total money = 5+11 =
16.
5
12. Given a 2d matrix cost[][], the task is to calculate the minimum cost
path to reach (m, n) from (0, 0). Each cell of the matrix represents
a cost to traverse through that cell. The total cost of a path to reach
(m, n) is the sum of all the costs on that path (including both source
and destination). We can only traverse down, right and diagonally
lower cells from a given cell, i.e., from a given cell (i, j), cells (i + 1, j),
(i, j + 1), and (i + 1, j + 1) can be traversed.
Example:
1 2 3
Input: 4 8 2
1 5 3
Output: 8
Explanation: The path with minimum cost is highlighted in the following
figure. The path is (0, 0) → (0, 1) → (1, 2) → (2, 2). The cost of the path
is 8 (1 + 2 + 2 + 3).
6
Examples:
Input: digits = ”121”
Output: 3
Explanation: The possible decodings are ”ABA”, ”AU”, ”LA”
Input: digits = ”1234”
Output: 3
Explanation: The possible decodings are ”ABCD”, ”LCD”, ”AWD”
14. Given an array arr[] of non-negative integers and a value sum, the task is
to check if there is a subset of the given array whose sum is equal to the
given sum.
Examples:
Input: arr[] = [3, 34, 4, 12, 5, 2], sum = 9
Output: True
Explanation: There is a subset (4, 5) with sum 9.
Input: arr[] = [3, 34, 4, 12, 5, 2], sum = 30
Output: False
Explanation: There is no subset that add up to 30.
15. Given an integer array of coins[] of size n representing different types of
denominations and an integer sum, the task is to count all combinations
of coins to make a given value sum.
Note: Assume that you have an infinite supply of each type of coin.
Examples:
Input: sum = 4, coins[] = [1, 2, 3]
Output: 4
Explanation: There are four solutions: [1, 1, 1, 1], [1, 1, 2], [2, 2] and [1, 3]
Input: sum = 10, coins[] = [2, 5, 3, 6]
Output: 5
Explanation: There are five solutions: [2, 2, 2, 2, 2], [2, 2, 3, 3], [2, 2, 6], [2, 3, 5]
and [5, 5]
Input: sum = 10, coins[] = [10]
Output: 1
Explanation: The only is to pick 1 coin of value 10.
Input: sum = 5, coins[] = [4]
Output: 0
Explanation: We cannot make sum 5 with the given coins