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

前端 未结 5 798
野的像风
野的像风 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:43

    If you don't assume arbitrary content (as in Oli's answer) and you can encode each row as an unsigned integer with binary flags, then you can do it in O(n) and O(1) by just repeatedely performing a logical AND of each row with the latest result.

    The final set of flags will only have ones where the relevant column was also one.

提交回复
热议问题