Unit 12 Session 1 Advanced (Click for link to problem statements)
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?
HAPPY CASE
Input:
platforms = [2, 3, 1, 1, 4]
Output:
True
Explanation:
Aang can jump from platform 0 to 1, then directly to the last platform.
EDGE CASE
Input:
platforms = [3, 2, 1, 0, 4]
Output:
False
Explanation:
Aang is stuck at platform 3 because its maximum jump is 0, so he can't reach the last platform.
Match what this problem looks like to known categories of problems, e.g. Arrays or Dynamic Programming, and strategies or patterns in those categories.
For Aang’s Airbending Journey, we want to consider the following approaches:
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Start at the first platform and update the farthest point Aang can reach based on the maximum jump distance from each platform. If at any point Aang gets stuck (i.e., can't reach the next platform), return False. If he can reach or exceed the last platform, return True.
Initialize variables:
farthest: Keeps track of the farthest point Aang can reach, starting at 0.Iterate over each platform:
i, check if Aang can reach it (i.e., i <= farthest).farthest to be the maximum of the current farthest point and i + platforms[i].farthest exceeds or reaches the last platform, return True.Final check:
False.Implement the code to solve the algorithm.
def aang_journey(platforms):
n = len(platforms)
farthest = 0 # The farthest point Aang can reach
for i in range(n):
# If Aang can't reach this platform, he is stuck
if i > farthest:
return False
# Update the farthest point Aang can reach
farthest = max(farthest, i + platforms[i])
# If Aang can already reach the last platform, return True
if farthest >= n - 1:
return True
return False
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
Example 1:
platforms = [2, 3, 1, 1, 4]
True
Example 2:
platforms = [3, 2, 1, 0, 4]
False
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Assume n is the length of the input list platforms.
O(n) because we iterate through the list once.O(1) since we only use a few variables.