dp

package
v0.0.0-...-8a7fe10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 23, 2024 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GroupKnapsack

func GroupKnapsack(groups [][]struct{ v, w int }, maxW int) int

GroupKnapsack 分组背包·每组至多选一个 LC2218 https://leetcode.cn/problems/maximum-value-of-k-coins-from-piles/

func GroupKnapsackWaysToSum

func GroupKnapsackWaysToSum(stocks, weights []int, maxW int) int

GroupKnapsackWaysToSum 方案数

https://www.luogu.com.cn/problem/P1077 (可以用前缀和优化) LC2585 https://leetcode.cn/problems/number-of-ways-to-earn-points/

func LIS

func LIS(nums []int) int

LIS 返回最长递增子序列的长度

LC300 https://leetcode.cn/problems/longest-increasing-subsequence/ LC1626 https://leetcode.cn/problems/best-team-with-no-conflicts/ - 时间复杂度:O(n^2)。 - 空间复杂度:O(n)。

func LISGreedy

func LISGreedy(nums []int) int

LISGreedy

贪心下二分

LC1964 https://leetcode.cn/problems/find-the-longest-valid-obstacle-course-at-each-position/ 需转换: - LC354 https://leetcode.cn/problems/russian-doll-envelopes

- 时间复杂度:O(nlogn) - 空间复杂度:O(n)。

func LISPath

func LISPath(nums []int) []int

LISPath 返回最长递增子序列本身,需要额外的 Last 字段来记录是从哪里转移过来的

LC2901 https://leetcode.cn/problems/longest-unequal-adjacent-groups-subsequence-ii/

- 时间复杂度:O(n^2)。 - 空间复杂度:O(n)。

func MemorySearch

func MemorySearch[X comparable, Y any](dfs *func(X) Y) (_cache map[X]Y)

MemorySearch 实现 Python 的 cache 装饰器,返回 cache 用于 debug

func MemorySearch2

func MemorySearch2[X, Y comparable, Z any](dfs *func(X, Y) Z) (_cache map[[2]any]Z)

MemorySearch2 todo 整合 MemorySearch

实现 Python 的 cache 装饰器,返回 cache 用于 debug

func MemorySearch3

func MemorySearch3[X, Y, Z comparable, R any](dfs *func(X, Y, Z) R) (_cache map[[3]any]R)

MemorySearch3 todo 整合 MemorySearch

实现 Python 的 cache 装饰器,返回 cache 用于 debug

func UnboundedKnapsackAtLeastFillUp

func UnboundedKnapsackAtLeastFillUp(values, weights []int, maxW int) int

UnboundedKnapsackAtLeastFillUp 至少装入重量和为 maxW 的物品,求价值和的最小值 https://www.luogu.com.cn/problem/P2918

func UnboundedKnapsackExactlyFull

func UnboundedKnapsackExactlyFull(values, weights []int, maxW int) int

UnboundedKnapsackExactlyFull 完全背包变形:恰好装满时的 最大价值和 / 最小价值和 /方案数 最小价值和:LC322 https://leetcode.cn/problems/coin-change/ 方案数: LC518 https://leetcode.cn/problems/coin-change-ii/

func UnboundedKnapsackWaysToSum

func UnboundedKnapsackWaysToSum(nums []int, sum int) int

UnboundedKnapsackWaysToSum 完全背包,恰好装满背包时的方案数 LC518 https://leetcode.cn/problems/coin-change-ii/

func ZeroOneKnapsack

func ZeroOneKnapsack(values, weights []int, maxW int) int

ZeroOneKnapsack 0-1 背包 (n 个物品,背包容量为 maxW,每个物品只能选 1 次) 状态:从前 i 个物品中选择若干个,当容量限制为 j 时能获得的最大价值和 i∈[0,n-1], j∈[0,maxW] 初始值:f(0,j)=0 j∈[0,maxW] 除开初始状态,每个状态有两个来源,决策为 max: - 不选第 i 个物品:f(i-1,j) -> f(i,j) -  选第 i 个物品:f(i-1,j-wi)+vi -> f(i,j) j ≥ wi

最优解为 f(n-1,maxW)

https://oi-wiki.org/dp/knapsack/

func ZeroOneKnapsackAtLeastFillUp

func ZeroOneKnapsackAtLeastFillUp(values, weights []int, maxW int) int

ZeroOneKnapsackAtLeastFillUp 0-1 背包 EXTRA: 至少装入重量和为 maxW 的物品,求价值和的最小值 https://www.luogu.com.cn/problem/P4377 f[0] 表示至少为 0 的情况,也表示没有任何约束的情况 比如选第 i 个物品后容量 <=0 了,那就表示前面的 i-1 个物品可以不受约束地随意选或不选了 - 转换 https://codeforces.com/problemset/problem/19/B LC2742 https://leetcode.cn/problems/painting-the-walls/ 二维费用的情况+价值最小 https://ac.nowcoder.com/acm/contest/6218/C

func ZeroOneWaysToSum

func ZeroOneWaysToSum(nums []int, sum int) int

ZeroOneWaysToSum 0-1 背包 EXTRA: 从序列 a 中选若干个数,使其总和为 sum 的方案数 常见题目是算严格分拆(必须用不同数字) LC2787 https://leetcode.cn/problems/ways-to-express-an-integer-as-sum-of-powers/ - https://oeis.org/A000009 NOTE: 1,1,1,...1(32个1),s-32,s-31,...,s 可以让方案数恰好为 2^32 二维+上限+下限 LC879 https://leetcode.cn/problems/profitable-schemes/ https://atcoder.jp/contests/arc060/tasks/arc060_a https://codeforces.com/problemset/problem/1673/C

- 转换 https://atcoder.jp/contests/abc169/tasks/abc169_f https://codeforces.com/problemset/problem/478/D LC494 https://leetcode.cn/problems/target-sum/ LC1434 https://leetcode.cn/problems/number-of-ways-to-wear-different-hats-to-each-other/ 由于顺序不同也算方案,所以这题需要正序递推 LC377 https://leetcode.cn/problems/combination-sum-iv/

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL