Dynamic programming [leetcode]

Notes: Random Thoughts on Code

overview

Dynamic Programming is referred to as DP. Dynamic programming is to derive the current state from the previous state. Greedy has nothing to do with the previous state.

five steps

  1. Determine the meaning of the dp array and the subscript
  2. Determine the recursive formula
  3. How to initialize dp array
  4. Determine traversal order
  5. Example derivation dp array

Leek

basic question

1. Fibonacci numbers

2. Climb the stairs

3. Use the minimum cost to climb the stairs

4. Different paths

5. Different paths two

6. Integer split

7. Different Binary Search Trees

knapsack problem

8.0-1 Knapsack Theoretical Basis 1

Two-dimensional array

The best way to do dynamic programming is to give an example on paper to deduce the value of the corresponding dp array, and then write the code by hand!

The biggest difference between the 01 backpack and the complete backpack is that the complete backpack has unlimited items for each item. A full backpack can be transformed into a 01 backpack.

9.0-1 Knapsack Theoretical Basis II

One-dimensional array (rolling array)

10. Split equal sum subsets

If the values ​​given by the title are all positive integers, then all non-zero subscripts can be initialized to 0. If the value given by the title is negative, then the non-zero subscripts must be initialized to negative infinity.

In this way, the dp array can obtain the maximum value during the recursion process, instead of being overwritten by the initial value .

dp[j] = max(dp[j], dp[j - nums[i]] + nums[i])

11. The Weight of the Last Stone II

12. Goals and

The formula for finding combination problems is similar to this:

dp[j] += dp[j - nums[i]]

13. Ones and Zeros

14. Theoretical basis of complete knapsack

Each item has an infinite number (that is, it can be put into the backpack multiple times). The only difference between the complete backpack and the 01 backpack problem is that each item has infinite pieces . In the complete knapsack, for a one-dimensional dp array, the nesting order of the two for loops does not matter!

15. Change Exchange II

If you want to find the number of combinations, the outer for loop traverses the items, and the inner for traverses the backpack .

If you want to find the number of permutations, the outer for loop traverses the backpack, and the inner loop traverses the items .

16. Combined sum four

17. Climb the stairs

18. Change Exchange

19. Perfect square numbers

20. Word splitting

21. Theoretical basis of multiple knapsacks

house robbery problem

22. Robbery

23. Robbery 2

The loop is closed, consider the case where the beginning is nums[0] and the end is nums[0].

24. Robbery

tree dp

stock question

25. The best time to buy and sell stocks

26. The best time to buy and sell stocks II

27. The best time to buy and sell stocks 3

28. The best time to buy and sell stocks IV

29. The best time to buy and sell stocks includes a freezing period

30. The best time to buy and sell stocks includes handling fees

subsequence problem

31. Longest increasing subsequence

dp classic problem

32. Longest Continuous Increasing Sequence

33. Longest repeating subarray

34. Longest Common Subsequence

35. Disjoint lines

36. Maximum Subsequence Sum

Classic topic: edit distance

37. Judgment subsequence

38. Different subsequences

If it is a continuous sequence, consider KMP

39. Delete operation of two strings

40. Edit Distance

41. Palindrome Skewers

42. Longest Palindromic Subsequence

Guess you like

Origin blog.csdn.net/qq_41804812/article/details/130251516