forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCelebrityProblem.java
85 lines (74 loc) · 2.87 KB
/
CelebrityProblem.java
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
package com.rampatra.arrays;
import com.rampatra.base.LinkedStack;
import com.rampatra.base.Stack;
/**
* Created by IntelliJ IDEA.
*
* @author rampatra
* @since 10/2/15
* @time: 10:48 PM
* <p/>
* In a party of N people, only one person is known to everyone. Such a person may be present in the party, if yes,
* (s)he doesn’t know anyone in the party. We can only ask questions like “does A know B? “. Find the stranger
* (celebrity) in minimum number of questions.
* <p>
* TODO: Not tested.
*/
public class CelebrityProblem {
/**
* Checks if person {@param a} knows person {@param b}.
*
* @param peoples
* @param a
* @param b
* @return
*/
public static boolean haveAcquaintance(int[][] peoples, int a, int b) {
return peoples[a][b] == 1;
}
/**
* Finds the celebrity in {@param peoples} where
* peoples[i][j] is 1 when person i knows person j.
* <p/>
* Algorithm:
* - If A knows B, then A can’t be celebrity. Discard A, but B may be celebrity.
* - If A does not know B, then B can’t be celebrity. Discard B, but A may be celebrity.
* - Repeat above two steps till we left with only one person.
* Find celebrity within remaining persons by performing the below operations:
* - Push all the celebrities into a stack.
* - Pop off top two persons from the stack, discard one person based on return status of HaveAcquaintance(A, B).
* - Push the remained person onto stack.
* - Repeat step 2 and 3 until only one person remains in the stack.
* - Check the remained person in stack does not have acquaintance with anyone else.
*
* @param peoples
* @return
*/
public static int findCelebrity(int[][] peoples) {
Stack<Integer> possibleCelebrities = new LinkedStack<>();
for (int i = 0; i < peoples.length; i++) {
for (int j = 0; j < peoples[0].length; j++) {
if (haveAcquaintance(peoples, i, j)) {
possibleCelebrities.push(j);
}
}
}
int firstPerson = -1, secondPerson;
while (!possibleCelebrities.isEmpty()) {
firstPerson = possibleCelebrities.pop();
// we have found the celebrity
if (possibleCelebrities.isEmpty()) break;
secondPerson = possibleCelebrities.pop();
if (haveAcquaintance(peoples, firstPerson, secondPerson)) {
possibleCelebrities.push(secondPerson);
} else {
possibleCelebrities.push(firstPerson);
}
}
return firstPerson;
}
public static void main(String[] args) {
System.out.println(findCelebrity(new int[][]{{0, 0, 1, 0}, {0, 0, 1, 0}, {0, 0, 0, 0}, {0, 0, 1, 0}}));
System.out.println(findCelebrity(new int[][]{{0, 0, 0, 1}, {0, 0, 0, 1}, {0, 0, 0, 1}, {0, 0, 0, 1}}));
}
}