查找具有最大总和的最长递增子序列

编程入门 行业动态 更新时间:2024-10-15 04:26:57
本文介绍了查找具有最大总和的最长递增子序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

给定一个可以为正和负的数字序列,有几种算法可以找到最长的递增子序列。但是,如果存在多个最长增长的子序列,有人可以给我一种算法来找到最长的增长最大的子序列吗?

Given a sequence of number which can be positive and negative, there are several algorithms to find the longest increasing subsequence. But can someone give me an algorithm to find the longest increasing subsequence with the maximum sum if there are multiple longest increasing subsequences?

例如:对于20、1、4、3、10,答案是1、4、10,而不是1、3、10

Example: For 20, 1, 4, 3, 10, the answer is 1, 4, 10, not 1, 3, 10

推荐答案

dpLen[i] = maximum length of a LIS with maximum sum ending at i dpSum[i] = maximum sum of a LIS with maximum sum ending at i for i = 0 to n do dpLen[i] = 1 dpSum[i] = input[i] maxLen = 0 for j = 0 to i do if dpLen[j] > maxLen and input[j] < input[i] maxLen = dpLen[j] for j = 0 to i do if dpLen[j] == maxLen and input[j] < input[i] and dpSum[j] + input[i] > dpSum[i] dpSum[i] = dpSum[j] + input[i] dpLen[i] = maxLen + 1

更多推荐

查找具有最大总和的最长递增子序列

本文发布于:2023-11-30 00:16:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648084.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:总和   序列   最长

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!