CJ

Explore

Tracks

My Courses

Edpresso

Refer a Friend

Create

Grokking Dynamic Programming Patterns for Coding Interviews

97% completed

Introduction

What is Dynamic Programming?

Pattern 1: 0/1 Knapsack

0/1 Knapsack

Equal Subset Sum Partition

Subset Sum

Minimum Subset Sum Difference

Count of Subset Sum

Target Sum

Pattern 2: Unbounded Knapsack

Unbounded Knapsack

Rod Cutting

Coin Change

Minimum Coin Change

Maximum Ribbon Cut

Pattern 3: Fibonacci Numbers

Fibonacci numbers

Staircase

Number factors

Minimum jumps to reach the end

Minimum jumps with fee

House thief

Pattern 4: Palindromic Subsequence

Longest Palindromic Subsequence

Longest Palindromic Substring

Count of Palindromic Substrings

Minimum Deletions in a String to make it a Palindrome

Palindromic Partitioning

Pattern 5: Longest Common Substring

Longest Common Substring

Longest Common Subsequence

Minimum Deletions & Insertions to Transform a String into another

Longest Increasing Subsequence

Maximum Sum Increasing Subsequence

Shortest Common Super-sequence

Minimum Deletions to Make a Sequence Sorted

Longest Repeating Subsequence

Subsequence Pattern Matching

Longest Bitonic Subsequence

Longest Alternating Subsequence

Edit Distance

Strings Interleaving

Appendix

Contact Us

Other courses

Mark Course as Completed

Number factors

We'll cover the following

Problem Statement

Basic Solution

Top-down Dynamic Programming with Memoization

Bottom-up Dynamic Programming

Code

Fibonacci number pattern

Problem Statement #

Given a number ‘n’, implement a method to count how many possible ways there are to express ‘n’ as the sum of 1, 3, or 4.

Example 1:

n : 4
Number of ways = 4
Explanation: Following are the four ways we can exoress 'n' : {1,1,1,1}, {1,3}, {3,1}, {4}

Example 2:

n : 5
Number of ways = 6
Explanation: Following are the six ways we can express 'n' : {1,1,1,1,1}, {1,1,3}, {1,3,1}, {3,1,1},
{1,4}, {4,1}

Let’s first start with a recursive brute-force solution.

Basic Solution #

For every number ‘i’, we have three option: subtract either 1, 3, or 4 from ‘i’ and recursively process the remaining number. So our algorithm will look like:

Java

Java

Java

JS

JS

Python3

Python3

C++

C++

class ExpressNumber {

public int CountWays(int n) {
if( n == 0)
return 1; // base case, we don't need to subtract any thing, so there is only one way

if(n == 1)
return 1; // we can subtract 1 to be left with zero, and that is the only way

if(n == 2)
return 1; // we can subtract 1 twice to get zero and that is the only way

if(n == 3)
return 2; // '3' can be expressed as {1,1,1}, {3}

// if we subtract 1, we are left with 'n-1'
int subtract1 = CountWays(n-1);
// if we subtract 3, we are left with 'n-3'
int subtract3 = CountWays(n-3);
// if we subtract 4, we are left with 'n-4'
int subtract4 = CountWays(n-4);

return subtract1 + subtract3 + subtract4;
}

public static void main(String[] args) {
ExpressNumber en = new ExpressNumber();
System.out.println(en.CountWays(4));
System.out.println(en.CountWays(5));
System.out.println(en.CountWays(6));
}
}

RUN

SAVERESET

The time complexity of the above algorithm is exponential O(3n)O(3^n)O(3​n​​). The space complexity is O(n)O(n)O(n) which is used to store the recursion stack.

Let’s visually draw the recursion for CountWays(5) to see the overlapping subproblems:

Recursion tree for calculating Fibonacci numbers

We can clearly see the overlapping subproblem pattern: CountWays(3), CountWays(2) and CountWays(1) have been called twice. We can optimize this using memoization to store the results for subproblems.

Top-down Dynamic Programming with Memoization #

We can use an array to store the already solved subproblems. Here is the code:

Java

Java

Java

JS

JS

Python3

Python3

C++

C++

class ExpressNumber {

public int CountWays(int n) {
int dp[] = new int[n + 1];
return CountWaysRecursive(dp, n);
}

public int CountWaysRecursive(int[] dp, int n) {
if (n == 0)
return 1; // base case, we don't need to subtract any thing, so there is only one way

if (n == 1)
return 1; // we can take subtract 1 to be left with zero, and that is the only way

if (n == 2)
return 1; // we can subtract 1 twice to get zero and that is the only way

if (n == 3)
return 2; // '3' can be expressed as {1,1,1}, {3}

if (dp[n] == 0) {
// if we subtract 1, we are left with 'n-1'
int subtract1 = CountWaysRecursive(dp, n - 1);
// if we subtract 3, we are left with 'n-3'
int subtract3 = CountWaysRecursive(dp, n - 3);
// if we subtract 4, we are left with 'n-4'
int subtract4 = CountWaysRecursive(dp, n - 4);
dp[n] = subtract1 + subtract3 + subtract4;
}

return dp[n];
}

public static void main(String[] args) {
ExpressNumber en = new ExpressNumber();
System.out.println(en.CountWays(4));
System.out.println(en.CountWays(5));
System.out.println(en.CountWays(6));
}
}

RUN

SAVERESET

Bottom-up Dynamic Programming #

Let’s try to populate our dp[] array from the above solution, working in a bottom-up fashion. As we saw in the above code, every CountWaysRecursive(n) is the sum of the three counts. We can use this fact to populate our array.

Code #

Here is the code for our bottom-up dynamic programming approach:

Java

Java

Java

JS

JS

Python3

Python3

C++

C++

class ExpressNumber {

public int CountWays(int n) {
int dp[] = new int[n+1];
dp[0] = 1;
dp[1] = 1;
dp[2] = 1;
dp[3] = 2;

for(int i=4; i<=n; i++)
dp[i] = dp[i-1] + dp[i-3] + dp[i-4];

return dp[n];
}

public static void main(String[] args) {
ExpressNumber en = new ExpressNumber();
System.out.println(en.CountWays(4));
System.out.println(en.CountWays(5));
System.out.println(en.CountWays(6));
}
}

RUN

SAVERESET

The above solution has time and space complexity of O(n)O(n)O(n).

Fibonacci number pattern #

We can clearly see that this problem follows the Fibonacci number pattern. However, every number in a Fibonacci series is the sum of the previous two numbers, whereas in this problem every count is a sum of previous three numbers: previous-1, previous-3, and previous-4. Here is the recursive formula for this problem:

CountWays(n) = CountWays(n-1) + CountWays(n-3) + CountWays(n-4), for n >= 4

Completed

←    Back
Staircase

Next    →
Minimum jumps to reach the end

Report an Issue

Ask a Question