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

Minimum jumps with fee

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 staircase with ‘n’ steps and an array of ‘n’ numbers representing the fee that you have to pay if you take the step. Implement a method to calculate the minimum fee required to reach the top of the staircase (beyond the top-most step). At every step, you have an option to take either 1 step, 2 steps, or 3 steps. You should assume that you are standing at the first step.

Example 1:

Number of stairs (n) : 6
Fee: {1,2,5,2,1,2}
Output: 3
Explanation: Starting from index '0', we can reach the top through: 0->3->top
The total fee we have to pay will be (1+2).

Example 2:

Number of stairs (n): 4
Fee: {2,3,4,5}
Output: 5
Explanation: Starting from index '0', we can reach the top through: 0->1->top
The total fee we have to pay will be (2+3).

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

Basic Solution #

At every step, we have three option: either jump 1 step, 2 steps, or 3 steps. So our algorithm will look like:

Java

Java

Java

JS

JS

Python3

Python3

C++

C++

class Staircase {

public int findMinFee(int[] fee) {
return findMinFeeRecursive(fee, 0);
}

private int findMinFeeRecursive(int[] fee, int currentIndex) {
if( currentIndex > fee.length - 1)
return 0;

// if we take 1 step, we are left with 'n-1' steps;
int take1Step = findMinFeeRecursive(fee, currentIndex + 1);
// similarly, if we took 2 steps, we are left with 'n-2' steps;
int take2Step = findMinFeeRecursive(fee, currentIndex + 2);
// if we took 3 steps, we are left with 'n-3' steps;
int take3Step = findMinFeeRecursive(fee, currentIndex + 3);

int min = Math.min(Math.min(take1Step, take2Step), take3Step);

return min + fee[currentIndex];
}

public static void main(String[] args) {
Staircase sc = new Staircase();
int[] fee = {1,2,5,2,1,2};
System.out.println(sc.findMinFee(fee));
fee = new int[]{2,3,4,5};
System.out.println(sc.findMinFee(fee));
}
}

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.

Top-down Dynamic Programming with Memoization #

To resolve overlapping subproblems, 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 Staircase {

public int findMinFee(int[] fee) {
int dp[] = new int[fee.length];
return findMinFeeRecursive(dp, fee, 0);
}

private int findMinFeeRecursive(int[] dp, int[] fee, int currentIndex) {
if( currentIndex > fee.length - 1)
return 0;

if(dp[currentIndex] == 0) {
// if we take 1 step, we are left with 'n-1' steps;
int take1Step = findMinFeeRecursive(dp, fee, currentIndex + 1);
// similarly, if we took 2 steps, we are left with 'n-2' steps;
int take2Step = findMinFeeRecursive(dp, fee, currentIndex + 2);
// if we took 3 steps, we are left with 'n-3' steps;
int take3Step = findMinFeeRecursive(dp, fee, currentIndex + 3);

dp[currentIndex] = fee[currentIndex] + Math.min(Math.min(take1Step, take2Step), take3Step);
}

return dp[currentIndex];
}

public static void main(String[] args) {
Staircase sc = new Staircase();
int[] fee = {1,2,5,2,1,2};
System.out.println(sc.findMinFee(fee));
fee = new int[]{2,3,4,5};
System.out.println(sc.findMinFee(fee));
}
}

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 findMinFeeRecursive(n) is the minimum of the three recursive calls; 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 Staircase {

public int findMinFee(int[] fee) {
int dp[] = new int[fee.length + 1]; // +1 to handle the 0th step
dp[0] = 0; // if there are no steps, we dont have to pay any fee
dp[1] = fee[0]; // only one step, so we have to pay its fee
// for 2 or 3 steps staircase, since we start from the first step so we have to pay its fee
// and from the first step we can reach the top by taking two or three steps, so we don't
// have to pay any other fee.
dp[2] = dp[3] = fee[0];

for (int i = 3; i < fee.length; i++)
dp[i + 1] = Math.min(fee[i] + dp[i], Math.min(fee[i - 1] + dp[i - 1], fee[i - 2] + dp[i - 2]));

return dp[fee.length];
}

public static void main(String[] args) {
Staircase sc = new Staircase();
int[] fee = { 1, 2, 5, 2, 1, 2 };
System.out.println(sc.findMinFee(fee));
fee = new int[] { 2, 3, 4, 5 };
System.out.println(sc.findMinFee(fee));
}
}

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. The only difference is that every Fibonacci number is a sum of the two preceding numbers, whereas in this problem every number (total fee) is the minimum of previous three numbers.

Completed

←    Back
Minimum jumps to reach the end

Next    →
House thief

Report an Issue

Ask a Question