Toph and Katara's Training Synchronization - codepath/compsci_guides GitHub Wiki
Unit 12 Session 1 Standard (Click for link to problem statements)
Unit 12 Session 1 Advanced (Click for link to problem statements)
Problem Highlights
- 💡 Difficulty: Medium
- ⏰ Time to complete: 30 mins
- 🛠️ Topics: Dynamic Programming
1: U-nderstand
Understand what the interviewer is asking for by using test cases and questions about the problem.
- Established a set (2-3) of test cases to verify their own solution later.
- Established a set (1-2) of edge cases to verify their solution handles complexities.
- Have fully understood the problem and have no clarifying questions.
- Have you verified any Time/Space Constraints for this problem?
- What is the goal of the problem?
- The goal is to find the length of the longest common subsequence between two training sequences, preserving the order of the moves.
- What is a subsequence?
- A subsequence is a sequence that can be derived by deleting some characters without changing the order of the remaining characters.
HAPPY CASE
Input:
katara_moves = ""waterbend"", toph_moves = ""earthbend""
Output:
5
Explanation:
The longest common subsequence is ""bend"" and its length is 5.
EDGE CASE
Input:
katara_moves = ""fire"", toph_moves = ""air""
Output:
0
Explanation:
There is no common subsequence between ""fire"" and ""air"", so the result is 0.
2: M-atch
Match what this problem looks like to known categories of problems, e.g. Linked List or Dynamic Programming, and strategies or patterns in those categories.
For longest common subsequence problems, we want to consider the following approaches:
- Dynamic Programming (DP): This problem can be solved by creating a DP table where we iteratively build the solution by checking if characters match between the two strings.
3: P-lan
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Use dynamic programming to calculate the length of the longest common subsequence between two strings. We'll build a DP table where dp[i][j]
represents the length of the longest common subsequence between the first i
characters of katara_moves
and the first j
characters of toph_moves
.
Steps:
-
Initialization:
- Create a DP table of size
(m + 1) x (n + 1)
wherem
is the length ofkatara_moves
andn
is the length oftoph_moves
. Initialize all values to0
.
- Create a DP table of size
-
Filling the DP Table:
- For each
i
from 1 tom
and eachj
from 1 ton
:- If
katara_moves[i - 1] == toph_moves[j - 1]
, thendp[i][j] = dp[i - 1][j - 1] + 1
(we found a matching character). - Otherwise,
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
(choose the longest subsequence found so far).
- If
- For each
-
Return the Result:
- The value at
dp[m][n]
will give the length of the longest common subsequence.
- The value at
4: I-mplement
Implement the code to solve the algorithm.
def training_synchronization(katara_moves, toph_moves):
m, n = len(katara_moves), len(toph_moves)
# Create a DP table with dimensions (m+1) x (n+1)
dp = [[0] * (n + 1) for _ in range(m + 1)]
# Fill the DP table
for i in range(1, m + 1):
for j in range(1, n + 1):
if katara_moves[i - 1] == toph_moves[j - 1]:
dp[i][j] = dp[i - 1][j - 1] + 1
else:
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
# The value in dp[m][n] is the length of the longest common subsequence
return dp[m][n]
5: R-eview
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
Example 1:
- Input:
katara_moves = ""waterbend"", toph_moves = ""earthbend""
- Expected Output:
5
Example 2:
- Input:
katara_moves = ""bend"", toph_moves = ""bend""
- Expected Output:
4
Example 3:
- Input:
katara_moves = ""fire"", toph_moves = ""air""
- Expected Output:
0
6: E-valuate
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Assume m
is the length of katara_moves
and n
is the length of toph_moves
.
- Time Complexity:
O(m * n)
because we fill a DP table of size(m+1) x (n+1)
. - Space Complexity:
O(m * n)
to store the DP table.