LC algorithm question: dynamic programming dp

  1. Beautiful arrangement
    Suppose there are N integers from 1 to N. If an array is successfully constructed from these N numbers, such that the i-th bit of the array (1 <= i <= N) satisfies one of the following two conditions , we call this array a beautiful arrangement. condition:

The number at the i-th position is divisible by i.
i is divisible by the number at the i-th position
. Now given an integer N, how many beautiful arrangements can be constructed?

Method 1: Status dp

  1. Number of paths out of bounds
  2. You are given a grid of size mxn and a ball. The starting coordinates of the ball are [startRow, startColumn]. You can move the ball into adjacent cells in four directions (and across grid boundaries to reach outside the grid). You can move the ball up to maxMove times.

Given five integers m, n, maxMove, startRow, and startColumn, find and return the number of paths that can move the ball out of the boundary.

Method 1: Path dp

  1. Student Attendance Record II
    can represent a student's attendance record as a string, where each character is used to mark the day's attendance (absent, late, present). The record only contains the following three characters:
    'A': Absent, absent
    'L': Late, late
    'P': Present.
    If students can meet the following two conditions at the same time, they can get attendance rewards:

A student's absence ('A') is strictly less than two days based on total attendance.
Students will not be late ('L') for 3 consecutive days or more than 3 consecutive days.
You are given an integer n, which represents the length (number of times) of the attendance record. Please return the number of record situations that may receive attendance rewards when the record length is n. The answer may be large, so the result modulo 109 + 7 is returned.

## The minimum cost to reach the destination within the specified time (or number of times)
lc787. The cheapest flight within K station transfer
lc1928. The minimum cost to reach the destination within the specified time
Method: dfs rotation rule

650. Keyboard with only two keys
Initially there was only one character 'A' on Notepad. You can perform two operations on this notepad at a time:
Copy All: Copy all characters in this notepad (copying only some characters is not allowed).
Paste: Paste the last copied character.
Given a number n, you need to use the minimum number of operations to output exactly n 'A's on Notepad. Returns the minimum number of operations that can print out n 'A's.

629. K arrays of reverse-ordered pairs.
Given two integers n and k, find the number of different arrays containing numbers from 1 to n that have exactly k reverse-ordered pairs.
The definition of a reverse-order pair is as follows: For the i-th and j-th elements of the array, if i < j and a[i] > a[j], it is a reverse-order pair; otherwise, it is not.
Since the answer may be large, only the value mod 109 + 7 of the answer needs to be returned.

375. Guess the number II
We are playing a guessing game. The rules of the game are as follows:

I choose a number from 1 to n.
You guess which number I picked.
If you guess the correct number, you win the game.
If you guess wrong, then I will tell you that the number I picked is larger or smaller than yours, and you need to keep guessing.
Whenever you guess the number x and you guess wrong, you pay x amount of cash. If you run out of money, you lose the game.
Given a specific number n, return the smallest amount of cash that guarantees you a win, no matter which number I choose.

688. Probability of the knight on the chessboard Idea
: Dynamic programming f[step][i][j] represents the probability of walking step to i, j. f[step][i][j] are all obtained by transferring f[step][ni][nj]/8 in 8 directions. A trick: change the probability of taking k steps from the row and column positions to taking k steps from the entire chessboard to the row and column positions.

887. Egg Drop
gives you k identical eggs and can use a building with n floors from the 1st to the nth floor.
It is known that there is a floor f that satisfies 0 <= f <= n. Any eggs dropped from a floor higher than f will break. Eggs dropped from a floor f or a floor lower than it will not break.
For each operation, you can take an unbroken egg and drop it from any floor x (satisfying 1 <= x <= n). If the egg breaks, you can't use it again. If an egg does not break after being dropped, it can be reused in subsequent operations.
Could you please calculate and return the minimum number of operations required to determine the exact value of f?

Idea: At first glance, it seems that it can be divided into two, but in this case k is not used. In addition, when dividing into two, the cost of throwing the egg into pieces is not considered. Dynamic programming can be used for this problem, dp[k][n] represents the current There are k eggs, and the minimum number of times to throw eggs on n floors.
When throwing eggs on the xth floor:
If the eggs do not break, then the state becomes (k, nx), that is, the number of our eggs remains unchanged, but the answer can only be on the nx floors above. In other words, we have reduced the original problem into a sub-problem of size (k, nx);
if the egg is broken, the state becomes (k−1,x−1), that is, we have one less egg, but We know that the answer can only be in the x-1 floor below the x-th floor. In other words, we reduce the original problem into a sub-problem of size (k-1, x-1).
dp(k,n)=1+ 1≤x≤nmin (max(dp(k−1,x−1),dp(k,n−x))).
The overall complexity is O(k n n), so we need to optimize the logic of calculating the current dp[k][n]. Originally, we enumerated the floors, and firstly, when k is fixed, dp must be monotonic. Incrementally, this is understandable. The higher the floor, the number of operations will increase. One of dp(k−1,x−1) and dp(k,n−x) (ignoring k in the first dimension) increases and the other decreases (when x increases). At this time, it is almost as shown in the figure below Insert image description here
. If you take the larger minimum value of these two, you must get the intersection position of t1 and t2. The two sides of this intersection conform to dichotomy (as shown in the figure), so they can be divided into two. Also note that the functions of both are not continuous but discrete. For example, only integers 1, 2, and 3 can be taken. Therefore, the intersection point may not be an integer. At this time, only the x0 and x1 closest to the intersection point With these two points, you can calculate the current dp value (self-evident).

72. Edit distance
Given two words, word1 and word2, please return the minimum number of operations used to convert word1 into word2.
You can perform the following three operations on a word:
insert a character,
delete a character,
replace a character

Discuss on a case-by-case basis. First, insertion and deletion are symmetrical, so only insertion and replacement cases are considered.
Therefore, there are three situations:
A insertion, B insertion, and A or B modification.
Let dp[i][j] represent the edit distance of A0, A1,...,Ai and B0, B1,...,Bj. Based on the above situation, consider how to convert to dp[i][j].

926. Flip a string to monotonically increasing
If a binary string consists of some 0s (maybe no 0s) followed by some 1s (maybe no 1s), then the string is monotonically increasing.
Given a binary string s, you can flip any 0 to a 1 or a 1 to a 0.
Returns the minimum number of flips that causes s to increase monotonically.

871. Minimum number of refuelings
Define dp[j] to represent the maximum distance that can be traveled j times of refueling. After updating the dp array, traverse from small to large subscripts. If dp[i] is greater than target, it can be returned directly.
How to update the dp array? dp[0]=startfuel is obvious, then traverse each gas station according to the distance, and update dp i each time based on whether dp[i] is greater than the distance to the gas station .

522. The Longest Special Sequence II
is essentially a test of LCS. This question needs to know first that if one of the strs is a sub-str of another str, then there will be no special str at all. And if a str is not a substr of other str, then the special str of the str is itself. If you know this, just double cycle.

873. The length of the longest Fibonacci subsequence,
fibonacci, is determined by two numbers: f[i][j]: arri is the first number from the bottom, arrj is the second number from the bottom, and the length of the sequence is the second number from the bottom. Three numbers can be obtained and recorded. The map from arr[i] to i can be obtained, and then the index of the third number from the last can be obtained. Then the transfer equation can be obtained.

Guess you like

Origin blog.csdn.net/weixin_45719581/article/details/119740821