-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcelebrityProblem.java
More file actions
27 lines (24 loc) · 837 Bytes
/
celebrityProblem.java
File metadata and controls
27 lines (24 loc) · 837 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
class Solution {
// Function to find if there is a celebrity in the party or not.
public int celebrity(int mat[][]) {
int n = mat.length;
int candidate = 0;
// Step 1: Find the potential celebrity
for (int i = 1; i < n; i++) {
if (mat[candidate][i] == 1) {
// candidate knows i, so candidate can't be a celebrity, but i could be
candidate = i;
}
}
// Step 2: Verify the candidate
for (int i = 0; i < n; i++) {
if (i != candidate) {
if (mat[candidate][i] == 1 || mat[i][candidate] == 0) {
// If the candidate knows i or i doesn't know the candidate, return -1
return -1;
}
}
}
return candidate;
}
}