- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
这是一个类似问题的链接,答案很好:Java Algorithm for finding the largest set of independent nodes in a binary tree .
我想出了一个不同的答案,但我的教授说它行不通,我想知道为什么(他不回复电子邮件)。
问题:
Given an array A with n integers, its indexes start with 0 (i.e,
A[0]
,A[1]
, …,A[n-1]
). We can interpret A as a binary tree in which the two children ofA[i]
areA[2i+1]
andA[2i+2]
, and the value of each element is the node weight of the tree. In this tree, we say that a set of vertices is "independent" if it does not contain any parent-child pair. The weight of an independent set is just the summation of all weights of its elements. Develop an algorithm to calculate the maximum weight of any independent set.
我得出的答案使用了以下关于二叉树中独立集的两个假设:
警告:我在考试中想到了这个,它并不漂亮,但我只是想看看我是否可以至少获得部分学分。
那么,为什么不能只构建两个独立的集合(一个用于奇数级别,一个用于偶数级别)?
如果每个集合中的任何权重是非负的,则将它们相加(丢弃负元素,因为这不会对最大权重集有贡献)以找到具有最大权重的独立集合。
如果集合中的权重都是负数(或等于0),则对其进行排序,返回最接近0的负数作为权重。
比较两个集合中每一个的最大独立集合的权重,并将其作为最终解决方案返回。
我的教授声称它行不通,但我不明白为什么。为什么它不起作用?
最佳答案
Interjay 已经注意到您的回答不正确的原因。这个问题可以用递归算法find-max-independent
来解决,给定一个二叉树,考虑两种情况:
在情况 1 中,由于包含了根节点,因此它的子节点都不能。因此,我们将 root 的孙子的 find-max-independent
的值加上 root 的值(必须包括在内)相加,然后返回它。
在情况 2 中,我们返回子节点的 find-max-independent
的最大值,如果有的话(我们只能选择一个)
算法可能看起来像这样(在 python 中):
def find_max_independent ( A ):
N=len(A)
def children ( i ):
for n in (2*i+1, 2*i+2):
if n<N: yield n
def gchildren ( i ):
for child in children(i):
for gchild in children(child):
yield gchild
memo=[None]*N
def rec ( root ):
"finds max independent set in subtree tree rooted at root. memoizes results"
assert(root<N)
if memo[root] != None:
return memo[root]
# option 'root not included': find the child with the max independent subset value
without_root = sum(rec(child) for child in children(root))
# option 'root included': possibly pick the root
# and the sum of the max value for the grandchildren
with_root = max(0, A[root]) + sum(rec(gchild) for gchild in gchildren(root))
val=max(with_root, without_root)
assert(val>=0)
memo[root]=val
return val
return rec(0) if N>0 else 0
部分测试用例说明:
tests=[
[[1,2,3,4,5,6], 16], #1
[[-100,2,3,4,5,6], 6], #2
[[1,200,3,4,5,6], 200], #3
[[1,2,3,-4,5,-6], 6], #4
[[], 0],
[[-1], 0],
]
for A, expected in tests:
actual=find_max_independent(A)
print("test: {}, expected: {}, actual: {} ({})".format(A, expected, actual, expected==actual))
示例输出:
test: [1, 2, 3, 4, 5, 6], expected: 16, actual: 16 (True)
test: [-100, 2, 3, 4, 5, 6], expected: 15, actual: 15 (True)
test: [1, 200, 3, 4, 5, 6], expected: 206, actual: 206 (True)
test: [1, 2, 3, -4, 5, -6], expected: 8, actual: 8 (True)
test: [], expected: 0, actual: 0 (True)
test: [-1], expected: 0, actual: 0 (True)
测试用例1
测试用例2
测试用例 3
测试用例4
memoized 算法的复杂度为 O(n)
,因为 rec(n)
为每个节点调用一次。这是一个使用深度优先搜索的自上而下的动态规划解决方案。
(测试用例插图由 leetcode 的交互式二叉树编辑器提供)
关于algorithm - 查找二叉树中最大独立集的大小 - 为什么错误的 "solution"不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10486089/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!