Dynamic Programming and Recursion
numRows, return the first numRows rows of Pascal's triangle. In Pascal's triangle, each number is the sum of the two numbers directly above it.
This can be solved without recursion just as easily (if not easier) as it can with recursion. It is included as a warmup as there are not many problems in this topic graded less than medium. If you want recursion practice, go recursive.
Each row starts and ends with 1. Every other value is the sum of the two values directly above it in the previous row.
This is a straightforward construction problem. Build the triangle row by row. Each row has one more element than the last. The first and last elements are always 1; the middle elements come from summing pairs in the previous row.
⚙ Knowledge Hub: Recursion[1]i, create an array of length i + 1 filled with 1sj (from 1 to i - 1), set row[j] = previous[j-1] + previous[j]numRows rowsnumRows - 1 rows first, then builds the final row from the last row returned.2-9 inclusive, return all possible letter combinations that the number could represent. The mapping of digits to letters is the same as on a telephone keypad (2 = abc, 3 = def, …, 9 = wxyz). Return the answer in any order.
This can be solved using recursion. It is very similar to the string permutation problem in this week's lecture. You need to accumulate the results in a list, rather than print the values out.
A straightforward way to do this in an object-oriented language is to add a list to the solution class provided by LeetCode, and append the strings to the list as you find them, rather than printing them out. Alternatively, a reference to a list can be passed as a parameter to the recursive function.
Use recursion / backtracking. Map each digit to its letters. At each step, pick one letter for the current digit and recurse on the remaining digits. When you have processed every digit, the current string is a complete combination.
⚙ Knowledge Hub: Recursion'2' -> "abc")backtrack(index, current)index == len(digits), append current to the result listdigits[index], call backtrack(index + 1, current + letter)backtrack(0, "")A-Z can be encoded as numbers using the mapping 'A' -> "1", 'B' -> "2", ..., 'Z' -> "26". Given a string s containing only digits, return the total number of ways to decode it. The answer is guaranteed to fit in a 32-bit integer.
Solve this with top-down dynamic programming. Get a recursive solution working first, then you will probably find the larger test cases time out. At this point, add memoisation and you should succeed.
At each position you have a choice: decode one digit (if it is 1-9) or two digits (if they form a number 10-26). A '0' on its own cannot be decoded.
Use top-down dynamic programming (recursion with memoisation). Define a function dp(i) that returns the number of ways to decode the substring from index i to the end. Cache the result for each i so you never recompute it.
dp(i) which returns the number of decodings from index i onwardsi == len(s) -> return 1 (one valid decoding reached)s[i] == '0', return 0 - no valid decoding starts with zeroways = dp(i + 1)i + 1 < len(s) and the two-digit number s[i..i+1] is ≤ 26, also add dp(i + 2)n and a list of coin denominations c, determine the number of ways to make change for n. Each denomination may be used any number of times. The order of coins does not matter - {1, 2} and {2, 1} count as the same combination.
This is a classic dynamic programming problem, which you can solve using top-down dynamic programming. As always with top-down DP, get the recursive solution working on the small test cases, then bolt on the memoisation.
The key subtlety is avoiding duplicate combinations. Because order does not matter, you must not count {1, 2} and {2, 1} separately. The standard way to handle this is to process coins in order and never go back to an earlier coin.
Use top-down dynamic programming. Define dp(amount, idx) where amount is the remaining value to fill and idx is the current coin index. At each step you either use the current coin (stay at idx) or skip it (move to idx + 1).
dp(amount, idx)amount == 0 -> return 1 (found a valid combination)amount < 0 or idx == len(c) -> return 0 (invalid path)dp(amount - c[idx], idx) + dp(amount, idx + 1)(amount, idx) to avoid recomputation