By the end of this lab session, you should be able to:
Here are the activities for which you will upload solutions to xSITe or Gradescope:
You are climbing a staircase. It takes n steps to reach the top. Each time you can either climb 1 or 2 steps. Given the value of n, your task is to calculate the number of distinct ways you can climb to the top. Code a recursive solution to this problem. Avoid using loops or arrays.
Input: n = 2
Output: 2
Explanation: (1 step + 1 step) OR (2 steps)
Input: n = 3
Output: 3
Explanation: (1 step + 1 step + 1 step) OR (1 step + 2 steps) OR (2 steps + 1 step)
The parameter n can take values between 1 and 45 (inclusive).
You can find the template files on GitHub here. Use the provided makefile to compile and run your project.
Implement the climbStairs function using recursion. Submit your completed climb_stairs_recursive.cpp file to the Gradescope assignment titled Week 11 Lab Exercise 1A: Climbing Stairs - Recursive Solution.
Each call to climbStairs(n) recursively calls climbStairs(n-1) and climbStairs(n-2). This creates a binary recursion tree, where each node branches into two subproblems. The total number of function calls grows exponentially, leading to \(T(n)=T(n−1)+T(n−2)+O(1)\) which resolves to \(O(2^n)\).
The space complexity is \(O(n)\) due to recursion stack usage.
You are climbing a staircase. It takes n steps to reach the top. Each time you can either climb 1 or 2 steps. Given the value of n, your task is to calculate the number of distinct ways you can climb to the top.
Give a bottom-up dynamic programming solution to this problem (tabulation). You are encouraged to use arrays. Do NOT use recursion for this problem.
Input: n = 4
Output: 5
Explanation: (1+1+1+1) OR (2+1+1) OR (1+2+1) OR (1+1+2) OR (2+2)
The parameter n can take values between 1 and 45 (inclusive).
You can find the template files on GitHub here.
Implement the climbStairsDP function using dynamic programming (bottom-up). Use either an array-based approach (O(n) space) or an optimized two-variable approach (O(1) space).
Submit your completed climb_stairs_dp.cpp file to the Gradescope assignment titled Week 11 Lab Exercise 1B: Climbing Stairs - DP Solution.
The time complexity of this DP solution is \(O(n)\) (big improvement from the previous recursive solution), and the space complexity is also \(O(n)\) (due to arrray usage).
The time complexity of this DP solution is \(O(n)\) (big improvement from the previous recursive solution), and the space complexity is \(O(1)\) (better than previously).
Given two strings text1 and text2, return the length of their longest common subsequence (LCS).
A subsequence is a new sequence that can be formed from an existing sequence by deleting some characters without changing the order of the remaining characters. For example, for the string ABCDE, valid subsequences include AB, ACE, BDE. Strings like CA or EDB are NOT valid subsequences (order changed).
A common subsequence between text1 and text2 is a subsequence that appears in both strings. For example, the subsequence AE appears in both ABE and AFE.
The longest common subsequence between text1 and text2 is the a common subsequence that has maximum length.
Input: text1 = "abcde", text2 = "ace"
Output: 3 // longest common subsequence is "ace"
Input: text1 = "abcbdab", text2 = "bdcaba"
Output: 4 // longest common subsequence is "bcba"
Input: text1 = "abc", text2 = "def"
Output: 0 // No common subsequence between text1 and text2
You can find the template files on GitHub here. Implement the longestCommonSubsequence function using dynamic programming with a 2D table. This table should store the length of the LCS for different prefixes of the input strings.
Submit your completed longest_common_subsequence.cpp file to the Gradescope assignment titled Week 11 Lab Exercise 2: Longest Common Subsequence.
text1 and text2. The longest common subsequence of the text1 and text2 depends on the longest common subsequence of their prefixes.text1 with length i and the prefix of text2 with length j.LCS(i,j)=LCS(i−1,j−1)+1.LCS(i,j) = max(LCS(i−1,j),LCS(i,j−1)).\[ LCS(i, j) = \begin{cases} 0, & \text{if } i = 0 \text{ or } j = 0 \\ LCS(i-1, j-1) + 1, & \text{if } text1[i-1] = text2[j-1] \\ \max(LCS(i-1, j), LCS(i, j-1)), & \text{if } text1[i-1] \neq text2[j-1] \end{cases} \]
LCS[m][n], where m is the length of text1 and n is the length of text2.int longestCommonSubsequence(string text1, string text2) {
int m = text1.size(), n = text2.size();
vector<vector<int>> LCS(m + 1, vector<int>(n + 1, 0));
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (text1[i - 1] == text2[j - 1])
LCS[i][j] = 1 + LCS[i - 1][j - 1];
else
LCS[i][j] = max(LCS[i - 1][j], LCS[i][j - 1]);
}
}
return dp[m][n];
}You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money.
Return the fewest number of coins that you need to make up that amount.
If that amount of money cannot be made up by any combination of the coins, return -1.
Input: coins = {1,2,5}, amount = 11
Output: 3 // (5+5+1)
Explanation: The fewest number of coins to make up 11 is 3, since 11 = 5 + 5 + 1. There are other ways to make up the value of 11 (like 5 + 2 + 2 + 2), but they require a higher number of coins.
Input: coins = {2}, amount = 3
Output: -1
Explanation: There is no way to make up an amount of 3 using only coins of denomination 2, so we return -1.
You can find the template files on GitHub here.
Implement the coinChange function inside the partial coin_change.cpp file. Use a dynamic programming approach to solve this problem
Submit your completed coin_change.cpp file to the Gradescope assignment titled Week 11 Lab Exercise 3: Coin Change Problem.
Think in terms of choices. At each step, you can choose to take a coin or skip it.
For each subproblem (i=1, i=2 … i=n), try all possible choices and take the one with the fewest coins.
Break it down recursively. Let dp(amount) represent the minimum number of coins needed for amount. The recurrence relation is : dp(amount)=1+min(dp(amount−c)) for all c values in the coins array.
If no valid combination is found, return -1.
Week 11: Dynamic Programming