[leetcode] Dynamic Programming

Backpack

Understand the Nine Lectures on Backpacks

01 Backpack , that is, items are limited.

for 物品
	for 容量(倒序)

Complete backpack , i.e. unlimited items.

for 物品
	for 容量(正序)

Note that climbing stairs (LeetCode 70) requires an orderly sequence , but a general complete backpack does not need to be ordered, such as the classic coin collection: interview question 08.11. Coins . Note the difference between the two cases! The former recycles capacity first and recycles items, while the latter recycles items first and then recycles capacity.

  • P1832 A+B Problem (re-upgrade) [ Original question | Solution]

  • Change Exchange ( LeetCode 322 )

  • Change Change II (LeetCode 518)

  • Combined Sums IV (leetcode 377)

  • Perfect Square Numbers (LeetCode 279)

  • Word Splitting (leetcode 139)

Group backpacks . The items are divided into several groups, and the items in each group conflict with each other, and at most one is selected.

for 组号
	for 容量
		for 组内物品

Multiple knapsacks , there are at most n[i] items available for the i-th item. It is a special case of grouping backpacks. It can be understood as follows: for the i-th item, only one of 0, 1, 2, 3, 4...n[i] can be selected.

for 物品种类
	for 容量
		for 该类物品数量

Requires binary optimization to 01 knapsack if timeout

mixed backpack

Some items are infinite (complete), and some items are finite (multiple). Process the complete knapsack first, and then convert multiple knapsacks into 01 knapsacks through binary optimization.

A backpack that satisfies a certain condition

2D true false backpack

Invest in stocks, multiple dp

dynamic programming

First record the most recently encountered state dp.

  • Robbery ( LeetCode 198 ) [ Original Problem Solution ]
  • House robbery (upgrade topic, after selecting i, i-2, i-1, i+1, i+2 can not be selected, ask the maximum value that can be stolen) [original question solution]
  • House robbery (upgrade question, you can steal 2 consecutive households, but you can't steal 3 consecutive households, ask the maximum value that can be stolen) [Original Problem Solution]
  • House robbery (upgrade question, you can't steal consecutive residents, but there are k opportunities to steal consecutive residents, ask the maximum value that can be stolen) [Original Problem Solution]
  • Robbery III ( LeetCode 337 ) [Original Problem Solution]
  • Best Time to Buy and Sell Stocks III (LeetCode 123)
  • Best Time to Buy and Sell Stocks IV (LeetCode 188)
  • The best time to buy and sell stocks includes a freezing period (LeetCode 309) [ Original Problem Solution ]

Guess you like

Origin blog.csdn.net/weixin_43742643/article/details/129786388