Linear time algorithm to compute cartesian product

心已入冬 提交于 2019-12-02 06:12:37

问题


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). 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?


回答1:


There are mn results; the minimum work you have to do is write each result to the output. So you cannot do better than O(mn).




回答2:


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. Simply saying an algorithm is O(n) is meaningless if n is not defined.

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.



来源:https://stackoverflow.com/questions/15079069/linear-time-algorithm-to-compute-cartesian-product

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!