Aang’s Airbending Journey - codepath/compsci_guides GitHub Wiki
Unit 12 Session 1 Advanced (Click for link to problem statements)
Problem Highlights
- 💡 Difficulty: Medium
- ⏰ Time to complete: 20 mins
- 🛠️ Topics: Greedy Algorithms
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 determine if Aang can reach the last platform given the jump distances at each platform.
- What does each element in the array represent?
- Each element represents the maximum jump Aang can perform from that platform.
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.
2: M-atch
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:
- Greedy Algorithm: The problem can be solved by keeping track of the farthest platform Aang can reach as he jumps from each platform. If at any point Aang can't move forward, he gets stuck, and the answer is False.
3: P-lan
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.
Steps:
-
Initialize variables:
farthest
: Keeps track of the farthest point Aang can reach, starting at 0.
-
Iterate over each platform:
- For each platform
i
, check if Aang can reach it (i.e.,i <= farthest
). - Update
farthest
to be the maximum of the current farthest point andi + platforms[i]
. - If
farthest
exceeds or reaches the last platform, returnTrue
.
- For each platform
-
Final check:
- If the loop completes without Aang getting stuck, return
False
.
- If the loop completes without Aang getting stuck, return
4: I-mplement
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
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:
platforms = [2, 3, 1, 1, 4]
- Expected Output:
True
Example 2:
- Input:
platforms = [3, 2, 1, 0, 4]
- Expected Output:
False
6: E-valuate
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
.
- Time Complexity:
O(n)
because we iterate through the list once. - Space Complexity:
O(1)
since we only use a few variables.