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?
EXAMPLE CASE
Input: nums = [1,2,3,1]
Output: 4
Input: nums = [2,7,9,3,1]
Output: 12
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.
The dynamic programming approach can be used here. Every DP solution has a table that we populate starting with the base case or the simplest of cases for which we already know the answer. E.g. for our problem, we know that in the absence of houses, the robber will make 0 profit. Similarly, if there is just one house left to rob, the robber will rob that house, and that will be the maximum profit.
Plan the solution with appropriate visualizations and pseudocode.
We start by populating the dynamic programming table with these initial values and then build the table in a bottom-up fashion which is the essence of this solution.
1. Define a table which we will use to store the results of our sub-problems. Call this table maxRobbedAmount where maxRobbedAmount[i] is the same value that would be returned by recurse(i) in the previous solution
2. Set maxRobbedAmount[N] to 0
3. Set maxRobbedAmount[N - 1] to nums[N - 1]
4. Iterate from N - 2 down to 0 and set maxRobbedAmount[i] = max(maxRobbedAmount[i + 1], maxRobbedAmount[i + 2] + nums[i])
5. Return the value in maxRobbedAmount[0].
ā ļø Common Mistakes
The recursive approach may run into trouble when the recursion stack grows too large. It may also run into trouble because, for each recursive call, the compiler must do additional work to maintain the call stack (function variables, etc.) which results in unwanted overhead.
Implement the code to solve the algorithm.
class Solution:
def rob(self, nums: List[int]) -> int:
# Special handling for empty case.
if not nums:
return 0
maxRobbedAmount = [None for _ in range(len(nums) + 1)]
N = len(nums)
# Base case initialization.
maxRobbedAmount[N], maxRobbedAmount[N - 1] = 0, nums[N - 1]
# DP table calculations.
for i in range(N - 2, -1, -1):
# Same as recursive solution.
maxRobbedAmount[i] = max(maxRobbedAmount[i + 1], maxRobbedAmount[i + 2] + nums[i])
return maxRobbedAmount[0]
class Solution {
public int rob(int[] nums) {
int N = nums.length;
// Special handling for empty array case.
if (N == 0) {
return 0;
}
int[] maxRobbedAmount = new int[nums.length + 1];
// Base case initializations.
maxRobbedAmount[N] = 0;
maxRobbedAmount[N - 1] = nums[N - 1];
// DP table calculations.
for (int i = N - 2; i >= 0; --i) {
// Same as the recursive solution.
maxRobbedAmount[i] = Math.max(maxRobbedAmount[i + 1], maxRobbedAmount[i + 2] + nums[i]);
}
return maxRobbedAmount[0];
}
}
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
Evaluate the performance of your algorithm and state any strong/weak or future potential work.