-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindCelebrity.java
More file actions
82 lines (72 loc) · 1.99 KB
/
FindCelebrity.java
File metadata and controls
82 lines (72 loc) · 1.99 KB
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
package datastructure.stackandqueue.stack;
/**
* The type Find celebrity.
*/
public class FindCelebrity {
private static boolean aqStatus(int[][] party, int x, int y) {
if (party[x][y] == 1) return true;
return false;
}
/**
* Find int.
*
* @param party the party
* @param people the people
* @return the int
*/
public static int find(int[][] party, int people){
Stack<Integer> stack = new Stack<>(people);
int celeb = -1;
for(int i=0;i<people;i++){
stack.push(i);
}
while(!stack.isEmpty()){
int x = stack.pop();
if(stack.isEmpty()){
celeb=x;
break;
}
int y = stack.pop();
if(aqStatus(party,x,y)){
//x knows y , discard x and push y in stack
stack.push(y);
}else
stack.push(x);
}
//At this point we will have last element of stack as celebrity
//Check it to make sure it's the right celebrity
for (int j = 0; j < people; j++) {
//Celebrity knows no one while everyone knows celebrity
if (celeb != j && (aqStatus(party, celeb, j) || !(aqStatus(party, j, celeb)))) return -1;
}
return celeb;
}
/**
* Main.
*
* @param args the args
*/
public static void main(String args[]) {
int [][] party1 = {
{0,1,1,0},
{1,0,1,1},
{0,0,0,0},
{0,1,1,0},
};
int [][] party2 = {
{0,1,1,0},
{1,0,1,1},
{0,0,0,1},
{0,1,1,0},
};
int [][] party3 = {
{0,0,0,0},
{1,0,0,1},
{1,0,0,1},
{1,1,1,0},
};
System.out.println(find(party1,4));
System.out.println(find(party2,4));
System.out.println(find(party3,4));
}
}