Algorithm: how to find a column in matrix filled with all 1, time complexity O(n)?

前端 未结 5 801
野的像风
野的像风 2020-12-11 05:40

I have a matrix which looks like this:

| 1 | 0 | 0 | 1 | 0 |
| 1 | 1 | 0 | 1 | 0 |
| 1 | 0 | 1 | 1 | 0 |
| 1 | 0 | 0 | 1 | 0 |
| 0 | 0 | 0 | 1 | 1 |
<         


        
5条回答
  •  执念已碎
    2020-12-11 05:53

    Assuming arbitrary content, you cannot avoid a worst-case of O(n2).* You have to visit every element in each column that you want to consider, and in the worst-case, you have to consider all columns.


    * Also assuming that n is the matrix dimension here, not the total number of elements.

提交回复
热议问题