Dynamic Programing
https://leetcode.com/discuss/general-discussion/491522/dynamic-programming-questions-thread. https://www.freecodecamp.org/news/follow-these-steps-to-solve-any-dynamic-programming-interview-problem-cc
class Solution {
public:
int fib(int n) {
if (n == 0) return 0;
if (n == 1) return 1;
int one = 0, two = 1, curt = 1;
for (int i = 2; i <= n; ++i) {
curt = one + two;
one = two;
two = curt;
}
return curt;
}
};dp[i] = dp[i - 1] + dp[i - 2]
public int climbStairs(int n) {
int[] dp = new int[n + 1];
dp[0] = 1;
dp[1] = 1;
for (int i = 2; i <= n; i++) {
dp[i] = dp[i - 2] + dp[i - 1];
}
return dp[n];
}public int climbStairs(int n) {
int[] dp = new int[n + 1];
int one = 1;
int two = 1;
for (int i = 2; i <= n; i++) {
if (i % 2 == 0) {
one = one + two;
} else {
two = one + two;
}
}
return n % 2 == 0 ? one : two;
}initialization: dp[0][0] = 1, dp[i][j] = 1 when i == 0 or j == 0
transition: dp[i][j] = dp[i - 1][j] + dp[i][j - 1]
When obstacle[i][j] == 1, there is no way to reach to that grid, which means dp[i][j] = 0.
Transition: dp[i] is the max value of get max from i - 2 plus the current one, and the max of i - 2 (not choose the current one) : dp[i] = Math.max(dp[i - 2] + nums[i], dp[i - 1]);
Initialization: dp[0] = nums[0], dp[1] = Math.max(nums[0], nums[1])
Do the same calculation as 198 but in two different ranges:
0 to n - 2
1 to n - 1
Get the max of the dp result of both ranges.
DP on Tree. Bottom up traversal. Keep the max result at each node. There are two options to get the best solution at the current node:
take self value: but not take the value from left and right child
not take self value: only take max of left child and max of right child.
The maximal square is determined from its shortest edge at dp[i][j]. For example, the edge of:
111
110
gives dp result:
1 2 3
1 2 0
In this case
dp[i] : max contiguous subarray sum at i
dp[i] = Math.max(dp[i - 1] + nums[i], nums[i])
dp[i + k] : can jump to i + k position
dp[i + k] = dp[i] + k
Either move down or right, which means it comes from left and up. Find the minimum from the left and up, add to the curt grid.
T: O(m * n) S: O(1)
Get min on each grid.
Use two pointers to store the min and second min on the node.
dp[i] = Math.min(dp[i - 1] + cost[i], dp[i - 2] + cost[i])
dp[i][j] : the number of possible ways to roll j dices so the sum of the face up numbers equals i
dp[i][j] += dp[i - face][j - 1] where 1 <= face <= f and i - d >= 0
initialization: dp[0][0] = 1
dp[i][j]: how many ways to assign symbols to make sum of j nums equal to target i.
dp[i][j] = dp[i - nums[j]][j - 1] + dp[i + nums[j]][j - 1]
Since it includes the negative number and the total sum will not exceed 1000. Therefore the answer range is between -1000 and 1000. Shift everything to make it fit the matrix, which is 0 to 2001.
dp[i][sum - nums[i] + 1000] += dp[i - 1][sum + 1000]
dp[i][sum + nums[i] + 1000] += dp[i - 1][sum + 1000]
Check if the sum of all elements can be divided by 2, and array of numbers can sum up to sum / 2.
dp[i][j] = dp[i - 1][j] || dp[i - 1][j - nums[i - 1]] if j - nums[i - 1] >= 0
Dp:
dp[i]represents the LIS up to indexi.Init, fill
dparray with 1 since the LIS of a single element is 1.
T: O(n ^ 2) S: O(n)
DP: Binary Search T: O(nlogn) link
Solution1: for each number in the array, save it is difference and the longest arithmetic subsequence of that difference. T: O(n^2)
Use relation: A[k] = 2 * A[j] - A[i] where i < j < k to find the k and save the index of k to an array: T: O(n^2)
Similar as two sum technique, search for arr[i] - diff in the hashmap and extend its length if there is a value.
Use DP to calculate the probability to reach each grid on the next level. Then sums up the probabilities on all grids.
Starts with all 10 numbers as 1, rolling save all combinations from the past and mod by modulo.
dp[i] = min = Math.min(dp[j] + (i / j), dp[i / j] + j)
Initialize a map to store the current stone and the steps it can reach. Loop over the stones and create empty HashSets for all stones. Put <0, [1]> in the map to make sure that the frog move the first step.
Loop over the stones except the last one (i < stones.length - 1), get the next steps of the current stone, if stone + step reach the last stone (stone + step == stones[stones.length - 1]), return true.
Otherwise, add the k + 1, k , k - 1 step to map.get(stone + step), it indicates how many steps frog can move on the next stone.
Card Game
Two players take turns to take cards in a row. Each player take from 1 to 3 cards. Compute the max of the first player gets.
3D dp. There are four scenarios: hori, verti, diag, adiag. For each scenario we need to maintain a 2D dp matrix so that it is a 3D dp.
This is a tricky question. It asks if jump odd step, with the smallest number above current. If jump even step, with the largest number above current. This can be achieve by using TreeMap. Here are the rules:
During odd numbered jumps (ie. jumps 1, 3, 5, ...), you jump to the index j such that
A[i] <= A[j]and A[j] is the smallest possible value. If there are multiple such indexes j, you can only jump to the smallest such index j.During even numbered jumps (ie. jumps 2, 4, 6, ...), you jump to the index j such that
A[i] >= A[j]and A[j] is the largest possible value. If there are multiple such indexes j, you can only jump to the smallest such index j.
Loop over the array from end. This is due to we can reversely determine the start step from the end step. Initialize two boolean arrays to determine if it can reach by odd jumps or even jumps. Put the number and its index to the TreeMap.
Walk twice from (0,0) to (m - 1, n - 1) which gives the same result from walk over and walk back. DFS will TLE
Walk two robots simultaneously. First has 3 steps: left down, down, right down. Same thing for the 2nd one.
DFS + MEMO: TLE
DP
Last updated
Was this helpful?