请教一道算法题

2019-01-21 17:10:02 +08:00
 lcj2class

Given an array with positive integers and another integer for example {7 2 4} and 9, you are required to generate an equation, by inserting operator add ("+") and minus ("-") among the array. The left side of equation are consist of the array and the right side of equation is the integer. Here the result is 7-2+4=9.

无意间在 http://www.raychase.net/1285 里看到的,作者没去说这题的思路。除了穷举外,还有什么好方法呢 🤔

1923 次点击
所在节点    程序员
9 条回复
geelaw
2019-01-21 17:16:44 +08:00
这个问题(很直白地)是 subset sum,数字都很小的时候用最常见的那个 dynamic programming。此外还可以用 @ChaoXu 之前的研究结果。

该问题是 NP-hard,所以不要期待一个多项式时间的算法。
enenaaa
2019-01-21 17:27:24 +08:00
@geelaw 请教下为啥数字大的时候不能用动态规划
guyeu
2019-01-21 17:29:14 +08:00
emmmm 这个问题比 subset sum 多了个限定条件,感觉用二叉树+剪枝可以试试。
geelaw
2019-01-21 17:44:12 +08:00
@enenaaa #2 数字大的时候动态规划不如枚举有效率
qinyusen
2019-01-21 18:21:25 +08:00
qinyusen
2019-01-21 18:22:38 +08:00
@geelaw 额,刚才 at 错人了。。。
aijam
2019-01-22 05:06:29 +08:00
lcj2class
2019-01-22 14:34:32 +08:00
@geelaw #4 是说数字大的时候会有额外的 overhead,有没有相关文章介绍的呢?
geelaw
2019-01-22 17:18:45 +08:00
@lcj2class 举个例子,有 n 个数,最大的数大小是 3^n,则 DP 的时间复杂度是 Omega(3^n) 但枚举的时间复杂度是 O(2^n*poly(n))。

如果你用“刷新式”实现动态规划则另谈。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/529192

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX