WITH ANSWERS GRADED A+
✔✔what is important to remember when calculating longest common subsequence as
opposed to substring? - ✔✔Subsequence: The length of the LCS is usually found at the
bottom-right corner of the DP table, representing the LCS of the entire sequences.
Substring: The length of the longest common substring is the maximum value in the
entire DP table, not just the bottom-right corner. This is because the longest common
substring can end at any position within the input strings.
✔✔if you are presented with coins (1,5,6,8) and your knapsack is k = 11, what is the
minimum number of coins, the computational time, and the recurrence? - ✔✔The
computational time for this method is O(nk), where n represents the number of coin
denominations, and k is the target sum.
Here's how it works:
Recurrence Relation: The core idea lies in a recurrence relation:
dp[i] represents the minimum number of coins needed to reach the sum i.
The formula for dp[i] is as follows:
dp[i] = min(dp[i], dp[i - c] + 1) for c in {1, 5, 6, 8} if i >= c
In simpler terms, we iterate through the target sums from 1 to 11, considering each coin
denomination (1, 5, 6, 8). We calculate dp[i] by choosing the minimum between the
current value of dp[i] and dp[i - c] + 1, where c represents the coin denomination. This
process continues as long as i is greater than or equal to c.
Algorithm:
Initialize an array dp of size (k+1) with all values set to Infinity except for dp[0], which is
set to 0.
For each value from 1 to 11 (the target sum):
For each coin denomination c in {1, 5, 6, 8}:If i - c is non-negative (i.e., i is greater than
or equal to c), update dp[i] as the minimum of dp[i] and dp[i - c] + 1.
Solution:
Initially, we have dp = [0, Inf, Inf, Inf, Inf, Inf, Inf, Inf, Inf, Inf, Inf, Inf].
After running the algorithm, dp[11] will store the minimum number of coins needed to
achieve a sum of 11.
In this particular case, the minimum number of coins required to make 11 is 2
(specifically, using coins with denominations 5 and 6).
✔✔how to calculate longest increasing substring. Try it using the following:
541208563 - ✔✔The idea is to maintain an array dp where dp[i] stores the length of the
longest increasing substring ending at index i.
Algorithm
, Initialize an array dp of size n with all elements set to 1. This is because a single
element is always an increasing substring of length 1.
Loop through the array from index 1 to n-1:
For each i, loop from 0 to i-1:
If sequence[i] > sequence[j],
update dp[i] = max(dp[i], dp[j] + 1).
✔✔what do you need to remember when it comes to longest increasing subsequence? -
✔✔Working with a Single Input Array: Start with a single input array, denoted as "arr,"
which has a length of "n."
Initialization: Create a Dynamic Programming (DP) array, denoted as "dp," with the
same size as the "arr" array. Initialize all elements of "dp" to 1 because each individual
element in the "arr" array is itself a valid increasing subsequence of length 1.
Value and Index Comparison: For each element "arr[i]" in the "arr" array, compare it with
all the previous elements "arr[j]" where j < i. If arr[i]>arr[j], then you have a candidate for
an increasing subsequence.
DP Update: If "arr[i]" is greater than "arr[j]," then it means you have a candidate for
extending the increasing subsequence. Update the "dp[i]" value as follows:
"dp[i]" becomes the maximum of its current value and "dp[j] + 1." This step ensures that
"dp[i]" will contain the length of the longest increasing subsequence ending at the index
"i."
✔✔what takes more time, longest increasing subsequence or longest common
subsequence? - ✔✔Longest Increasing Subsequence (LIS): The standard dynamic
programming approach for LIS has a time complexity of O(n^2), where n is the length of
the array.
Longest Common Subsequence (LCS): The standard dynamic programming approach
for LCS has a time complexity of O(mn), where m and n are the lengths of the two
sequences being compared.
Comparison:
Same Length Sequences: If both sequences in LCS have the same length n, then the
time complexity becomes O(n^2), which is the same as that of LIS.
Different Length Sequences: If the sequences in LCS have different lengths m and n,
then the time complexity could be higher or lower than O(n^2) depending on the values
of m and n.
✔✔explain minimum distance algorithm: perform it using
"abcdef"
and
"a3ced" - ✔✔the minimum number of edits to transform one string into another
Initialize dp[m+1][n+1]