- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
问题是,
给定一个大小为 n 的数组,我们必须将数组输出/划分为总和为 N 的子集。
For E,g,
I/p arr{2,4,5,7}, n=4, N(sum) = 7(given)
O/p = {2,5}, {7}
我在 url Dynamic Programming3 中看到了类似的问题/解释
我在 pdf 中有以下查询:-
- How could we find the subsets which sum to N, as the logic only tells whether the subset exist or not?
- Also, if we change the question a bit, can we find two subsets which has equal average using the same ideology?
任何人都可以对这个动态规划问题有所了解.. :)
提前致谢..
最佳答案
可以尝试递归处理:
给定一个排序数组 X={x1 ... xn} xi !=0 和一个整数 N。
首先找到仅由一个元素“制造”的所有可能性:
这里如果N=xp,则消除所有xi s.t i>=p
second 找出由 2 个元素构成的所有可能性:
{ (x1,x2) .... (xp-2,xp-1)}
按总和排序并消除所有总和 >=N你有规则:当 xi+xj >= N
时,xi 不能和 xj 一起去Third 有 3 个元素:您创建所有遵守上述规则的部分。同上步骤 2等等……
示例:
X={1,2,4,7,9,10} N=9
step one:
{9}
X'={1,2,4,7,9}
step 2: cannot chose 9 and 10
X={(1,2) (1,4) (2,4) (1,7) (2,7) (4,7)}
{2,7}
X'={(1,2) (1,4) (2,4) (1,7)}
step 3: 4 and 2 cannot go with 7:
X={(1,2,4)}
no sol
{9} {2,7} are the only solutions
这会减少您只进行的比较总数(即 2^n = 2^6=64):12 次比较
希望对你有帮助
关于c - 动态规划问题..数组分区..,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6493120/
我正在创建我的第一个 WAR 文件。我一直在试验 ant buildfile 语法,我的 buildfile 的第一部分从我的 Eclipse 项目中获取内容并将其放入 /dist 文件夹中,然后将其
我是一名学习 SQL 和 PHP 的学生,我接到了一项任务,要使用 PHP 和 mySQLi 创建学生反馈表,我真的一直在思考如何为项目设计数据库! 我正在创建一个系统,用户可以在其中登录网页,如果用
这个问题在这里已经有了答案: Is it possbile to test for expected errors when the testee exits with failure using
我目前正在设计和开发一个 Web 应用程序,该应用程序有可能快速增长。我将提供一些一般信息,然后继续我的问题。我会说我是一名中级网络程序员。 以下是一些规范:MySQL - 数据库后端PHP - 用于
我不知何故无法在我的日志解析器应用程序中实现报告功能。 这是我目前所做的: 我正在编写一个应用程序,它读取日志文件并在字符串中搜索可以在用户配置文件中定义的多个正则表达式。对于从配置中解析的每个所谓的
我有兴趣学习如何在多开发团队场景中设计/规划 Web 应用程序开发。 假设“项目经理/负责人”的角色: 成功的 Web 应用程序开发需要哪些“文档”? 需要什么 UML 图,需要什么程度? 在设计/计
table a (t_a): id name last first email state country 0 sklass klass steve
我们建立了一个广泛使用 JQuery UI 的 AJAX 网站。我们有 30 多个自制的 JQuery UI 小部件(动态加载)。我们到处都使用 JQuery native 小部件:对话框、 slid
我是一名优秀的程序员,十分优秀!