-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCelebrityProblem.cpp
More file actions
49 lines (39 loc) · 943 Bytes
/
Copy pathCelebrityProblem.cpp
File metadata and controls
49 lines (39 loc) · 943 Bytes
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
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
int celebrity(vector<vector<int>>& arr) {
int n = arr.size();
stack<int> s;
for(int i = 0; i < n; i++){
s.push(i);
}
while(s.size() > 1){
int i = s.top();
s.pop();
int j = s.top();
s.pop();
if(arr[i][j] == 0){
s.push(i);
}else{
s.push(j);
}
}
int celeb = s.top();
for(int i = 0; i < n; i++){
if(i != celeb){
if(arr[i][celeb] == 0 || arr[celeb][i] == 1){
return -1;
}
}
}
return celeb;
}
int main() {
vector<vector<int>> arr = {{0, 1, 0},
{0, 0, 0},
{0, 1, 0}};
int ans = celebrity(arr);
cout << "celebrity = " << ans << endl;
return 0;
}