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?
Be sure that you clarify the input and output parameters of the problem:
O(n^2)
time and O(n^2)
space will do. Run through a set of example cases:
Input: numRows = 5
Output: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
Input: numRows = 6
Output: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1], [1,5,10,10,5,1]]
EDGE CASE
Input: numRows = 1
Output: [[1]]
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.
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Bottom-Up DP Technique, we will build up our answer. We will find the next row of the pascal triangle from the previous row. We can add zeros to the beginning and end of row to help with calculation of next row.
1. Set the basecase, [1] in results
2. Generate number of rows as requested
a. Add zeros to the beginning and end of previous row for calculation
b. For each item in row, set it to be itself plus the next number.
c. Remove the last zero, because we only need one new number
d. Append the new row to results.
7. Return the results
⚠️ Common Mistakes
Implement the code to solve the algorithm.
class Solution:
def generate(self, numRows: int) -> List[List[int]]:
# Set the basecase, [1] in results
results = [[1]]
# Generate number of rows as requested
for i in range(1, numRows):
# Add zeros to the beginning and end of previous row for calculation
row = [0] + results[-1] + [0]
# For each item in row, set it to be itself plus the next number
for j in range(len(row) - 1):
row[j] = row[j] + row[j + 1]
# Remove the last zero, because we only need one new number
row.pop()
# Append the new row to results
results.append(row)
# Return the results
return results
class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> allrows = new ArrayList<List<Integer>>();
ArrayList<Integer> row = new ArrayList<Integer>();
// Generate number of rows as requested
for(int i=0;i<numRows;i++) {
// Add zeros to the beginning and end of previous row for calculation
row.add(0, 1);
// For each item in row, set it to be itself plus the next number
for(int j=1;j<row.size()-1;j++)
row.set(j, row.get(j)+row.get(j+1));
allrows.add(new ArrayList<Integer>(row));
}
// Return the results
return allrows;
}
}
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.
Assume N
represents the number of rows
O(N^2)
, because we need to build up each row, and the number of items in each rows averages N/2
items. O(N^2)
, because we need to create the results.