Linear time algorithm to compute cartesian product

后端 未结 2 819
臣服心动
臣服心动 2021-01-21 16:50

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)

相关标签:
2条回答
  • 2021-01-21 17:34

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

    0 讨论(0)
  • 2021-01-21 17:38

    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.

    0 讨论(0)
提交回复
热议问题