-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathcelebrity_problem.cpp
101 lines (86 loc) · 2.14 KB
/
celebrity_problem.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include<bits/stdc++.h>
using namespace std;
class Solution
{
public:
//Function to find if there is a celebrity in the party or not.
int celebrity(vector<vector<int> >& M, int n)
{
//M1: N^2time and N space approach: Using indegree and outdegree
// int indegree[n];
// int outdegree[n];
// for(int i=0;i<n;i++)
// {
// for(int j=0;j<n;j++)
// {
// if(M[i][j]==1)
// {
// indegree[j]++;
// outdegree[i]++;
// }
// }
// }
// for(int i=0;i<n;i++)
// {
// if(indegree[i]==n-1 && outdegree[i]==0)
// return i;
// }
// return -1;
//M2: Linear Time : Using Stack : N time and space
// stack<int> stk;
// for(int i=0;i<n;i++)
// stk.push(i);
// while(stk.size()>1)
// {
// int a= stk.top();
// stk.pop();
// int b= stk.top();
// stk.pop();
// if(M[a][b]==1)
// stk.push(b);
// else
// stk.push(a);
// }
// //Rechecking
// int cel=stk.top();
// for(int i=0;i<n;i++)
// if(i!=cel && (M[cel][i]==1 || M[i][cel]==0))
// return -1;
// return cel;
//M3: N time and constant Space: Two Pointers
int low=0;
int high=n-1;
while(low<high)
{
if(M[low][high]==1)
low++;
else
high--;
}
int cel=low;
for(int i=0;i<n;i++)
if(i!=cel && (M[cel][i]==1 || M[i][cel]==0))
return -1;
return cel;
}
};
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
vector<vector<int> > M( n , vector<int> (n, 0));
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cin>>M[i][j];
}
}
Solution ob;
cout<<ob.celebrity(M,n)<<endl;
}
}