简体   繁体   English

线性时间算法计算笛卡尔积

[英]Linear time algorithm to compute cartesian product

I was asked in an interview to come up with a solution with linear time for cartesian product. 我在一次采访中被要求为笛卡尔积提供线性时间的解决方案。 I did the iterative manner O(mn) and a recursive solution also which is also O(mn). 我做了迭代方式O(mn)和一个递归解,它也是O(mn)。 But I could not reduce the complexity further. 但是我无法进一步降低复杂性。 Does anyone have ideas on how this complexity can be improved? 是否有人对如何改善这种复杂性有想法? Also can anyone suggest an efficient recursive approach? 也有人可以建议一种有效的递归方法吗?

There are mn results; mn结果; the minimum work you have to do is write each result to the output. 您要做的最低工作是将每个结果写入输出。 So you cannot do better than O(mn) . 因此,您做不到比O(mn)

The question that comes to my mind reading this is, "Linear with respect to what?" 读这本书时,我想到的问题是:“相对于什么线性?” Remember that in mathematics, all variables must be defined to have meaning. 请记住,在数学中,所有变量都必须定义为具有含义。 Big-O notation is no exception. 大O表示法也不例外。 Simply saying an algorithm is O(n) is meaningless if n is not defined. 如果未定义n,则简单地说算法为O(n)是没有意义的。

Assuming the question was meaningful, and not a mistake, my guess is that they wanted you to ask for clarification. 假设问题是有意义的,而不是错误,我的猜测是他们希望您要求澄清。 Another possibility is that they wanted to see how you would respond when presented with an impossible situation. 另一个可能性是,他们想看看您在遇到不可能的情况时会如何应对。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM