dynamic-programming - 最长递增子序列背后的算法
问题描述
我需要一个演练示例来理解以下算法。它取自 Dasgupta 的算法。这是他们给出的寻找最长递增子序列的算法。
for j = 1, 2, . . . , n:
L(j) = 1 + max{L(i) : (i, j) ∈ E}
return maxj L(j)
这是图表:
我了解 L(j) 代表以 j 结尾的 LIS;但是,我不明白以下内容:max{L(i) : (i, j) ∈ E}
非常感谢一个演练示例来理解这个循环是如何工作的。谢谢!
解决方案
E 最有可能在此伪代码之前定义,它是一组对i, j
,使得i < j
和X_i < X_j
它只是表示所有剩余数字的 LIS 长度并且小于当前数字。Python代码:
X = [5, 2, 8, 6, 3, 6, 9, 7]
L = []
for current_value in X:
# the whole algorithm is in this line:
# take the LIS of a smaller preceding number and increase by one
L.append(1 + max((l for l, x in zip(L, X) if x<current_value), default=0))
result = L[-1]
推荐阅读
- sas - 如何将proc sql case语句代码转换为数据步骤
- python - 为数据点生成“K”最近邻
- excel - 使用 excel vba 将评论保持在明确的范围内
- laravel - 如何在不要求用户登录 Laravel 的情况下验证电子邮件
- c# - 如何在 C# 中消除 DataGridview 单元格中的重复值
- javascript - framework7 init 未在页面 init 上触发
- javascript - 如何使 fs.readFile 仅在范围内返回行?
- python-3.x - 在我得到“比较中超出最大递归深度”错误后,我的内核死了
- qt - 确定 TableView 中选择的开始?
- css - Rubik Black 无法正常工作或无法加载