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

前端 未结 5 809
野的像风
野的像风 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-11 05:57

    Let me take a very wild guess on what you are trying to do. Hint from the mention of:

    1. The array represent relation on people
    2. You are finding a column with all 1s
    3. You are trying to find an O(n) algorithm

    Well, you can not do that in O(n) and I can prove that it is O(n^2) only.

    But my wild guess is that you doing a classic celebrity identification problem, and that you misunderstood the problem.

    A celebrity is person that is known by every other person, but doesn't know any [other people].

    I celebrity identification problem, you are trying to find something like:

    Find the number i where
    a[i][x] = 1 for all x            -> every one knows the celebrity
    a[x][i] = 0 for all x != i       -> the celebrity doesn't know anyone else
    

    And indeed with this extra constrain on what you are trying to find, there is an O(n) solution.

提交回复
热议问题