Advertisement
imashutosh51

The celebrity problem

Jul 31st, 2022 (edited)
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1. /*
  2.  
  3. Logic is :there can be only on celebrity possible,because for being another celebrity,first celebrity must know the other celebrity
  4. and if first celebrity knows the second celebrity,then first celebrity can't be celebrity because he knows someone else.
  5. so using stack,i found that one probable celebrity.
  6. */
  7. class Solution:
  8.     def celebrity(self, mat):
  9.         st=[i for i in range(len(mat))]
  10.         while len(st)>1:
  11.             a,b=st.pop(),st.pop()
  12.             if mat[a][b]==1:
  13.                 st.append(b)
  14.             else:
  15.                 st.append(a)
  16.         k=st[-1]
  17.         for i in range(len(mat)):
  18.             if k==i:
  19.                 continue;
  20.             if mat[i][k]==0 or mat[k][i]==1:
  21.                 return -1
  22.         return k
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement