谜题..求解数组 X 中值的乘积
你能帮我解决这个问题吗?
您有一个由 n 个整数组成的无序数组 X。查找包含 n 个元素的数组 M,其中 Mi 是 X 中除 Xi 之外的所有整数的乘积。您不能使用除法。您可以使用额外的内存。 (提示:有比 O(n^2) 更快的解决方案。)
基本解决方案 - O(n^2) 并且使用除法很容易。但我就是找不到比 O(n^2) 更快的另一种解决方案。
Can you please help me solving this one?
You have an unordered array X of n integers. Find the array M containing n elements where Mi is the product of all integers in X except for Xi. You may not use division. You can use extra memory. (Hint: There are solutions faster than O(n^2).)
The basic ones - O(n^2) and one using division is easy. But I just can't get another solution that is faster than O(n^2).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
令
left[i]
为X
中来自1..i
的所有元素的乘积。令right[i]
为X
中来自i..N
的所有元素的乘积。您可以按以下方式在O(n)
中计算两者,无需除法:left[i] = left[i - 1] * X[i]
和右[i] = 右[i + 1] * X[i]
;现在我们将计算
M
:M[i] = left[i - 1] * right[i + 1]
注意:
left
和 < code>right 是数组。希望很清楚:)
Let
left[i]
be the product of all elements inX
from1..i
. Letright[i]
be the product of all elements inX
fromi..N
. You can compute both inO(n)
without division in the following way:left[i] = left[i - 1] * X[i]
andright[i] = right[i + 1] * X[i]
;Now we will compute
M
:M[i] = left[i - 1] * right[i + 1]
Note:
left
andright
are arrays.Hope it is clear:)
这是Python 中的解决方案。我用除法的简单方法与没有除法的困难方法进行比较。我能得到这份工作吗?
Here's a solution in Python. I did the easy way with division to compare against the hard way without. Do I get the job?
O(n)
- http:// /nbl.cewit.stonybrook.edu:60128/mediawiki/index.php/TADM2E_3.28两遍 -
O(n)
- http://nbl.cewit.stonybrook.edu:60128/mediawiki/index.php/TADM2E_3.28two passes -
旧但非常酷,我自己在一次采访中被问到这个问题,并看到了几种解决方案,但这是我最喜欢的,取自
http://www.polygenelubricants.com/2010 /04/on-all-other-products-no-division.html
这是发生的事情,如果您为所有迭代写出 prods[i],您将看到以下计算结果,
因此每个 prods[ i] 被击中两次,一次是从头到尾,一次是从尾到头,这两次迭代都在累积乘积
向中心遍历,这样很容易看到我们会得到我们需要的东西,我们只需要小心,看看它错过了元素本身,这就是
这变得很棘手。关键在于
for 循环条件本身,而不是主体,直到循环结束才发生
迭代。所以对于
产品[0],
它从 1*1 开始,然后 pi 设置为 120,因此 prods[0] 错过了第一个元素
prods[1],
它是 1 * 120 = 120,然后 pi 被设置为 120*60等等等等
Old but very cool, I've been asked this at an interview myself and seen several solutions since but this is my favorite as taken from
http://www.polygenelubricants.com/2010/04/on-all-other-products-no-division.html
Here's what's going on, if you write out prods[i] for all the iterations, you'll see the following being calculated
so each prods[i] get hit twice, one from the going from head to tail and once from tail to head, and both of these iterations are accumulating the product as they
traverse towards the center so it's easy to see we'll get exactly what we need, we just need to be careful and see that it misses the element itself and that's where
it gets tricky. the key lies in the
in the for loop conditional itself and not in the body which do not happen until the end of the
iteration. so for
prods[0],
it starts at 1*1 and then pi gets set to 120 after, so prods[0] misses the first elements
prods[1],
it's 1 * 120 = 120 and then pi gets set to 120*60 afterso on and so on
O(nlogn)
方法:O(nlogn)
approach:这是我在Python中的解决方案:简单的方法,但计算成本可能很高?
Here is my solution in Python: Easy way but with high computational cost may be?