Skip to content

Commit 564bae0

Browse files
committed
Add suggested changes
1 parent 6bb9819 commit 564bae0

File tree

2 files changed

+50
-17
lines changed

2 files changed

+50
-17
lines changed

src/main/java/com/thealgorithms/stacks/CelebrityFinder.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.thealgorithms.stacks;
22

3+
import java.util.Stack;
4+
35
/**
46
* Solves the celebrity problem using a stack-based algorithm.
57
*
@@ -13,24 +15,34 @@ private CelebrityFinder() {
1315
}
1416

1517
/**
16-
* Finds the celebrity in the given party matrix.
18+
* Finds the celebrity in the given party matrix using a stack-based algorithm.
1719
*
1820
* @param party A 2D matrix where party[i][j] is 1 if i knows j, otherwise 0.
1921
* @return The index of the celebrity, or -1 if there is no celebrity.
2022
*/
2123
public static int findCelebrity(int[][] party) {
22-
int n = party.length;
23-
int candidate = 0;
2424

25-
// Find a potential celebrity
26-
for (int i = 1; i < n; i++) {
27-
if (party[candidate][i] == 1) {
28-
candidate = i;
25+
// Push all people onto the stack
26+
Stack<Integer> stack = new Stack<>();
27+
for (int i = 0; i < party.length; i++) {
28+
stack.push(i);
29+
}
30+
31+
// Find the potential celebrity by comparing pairs
32+
while (stack.size() > 1) {
33+
int person1 = stack.pop();
34+
int person2 = stack.pop();
35+
36+
if (party[person1][person2] == 1) {
37+
stack.push(person2); // person1 knows person2, so person2 might be the celebrity
38+
} else {
39+
stack.push(person1); // person1 doesn't know person2, so person1 might be the celebrity
2940
}
3041
}
3142

3243
// Verify the candidate
33-
for (int i = 0; i < n; i++) {
44+
int candidate = stack.pop();
45+
for (int i = 0; i < party.length; i++) {
3446
if (i != candidate && (party[candidate][i] == 1 || party[i][candidate] == 0)) {
3547
return -1;
3648
}

src/test/java/com/thealgorithms/stacks/CelebrityFinderTest.java

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,40 @@
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44

5-
import org.junit.jupiter.api.Test;
5+
import java.util.stream.Stream;
6+
import org.junit.jupiter.params.ParameterizedTest;
7+
import org.junit.jupiter.params.provider.Arguments;
8+
import org.junit.jupiter.params.provider.MethodSource;
69

710
public class CelebrityFinderTest {
811

9-
@Test
10-
public void testCelebrityExists() {
11-
int[][] party = {{0, 1, 1}, {0, 0, 1}, {0, 0, 0}};
12-
assertEquals(2, CelebrityFinder.findCelebrity(party));
12+
@ParameterizedTest
13+
@MethodSource("providePartyMatrices")
14+
public void testCelebrityFinder(int[][] party, int expected) {
15+
assertEquals(expected, CelebrityFinder.findCelebrity(party));
1316
}
1417

15-
@Test
16-
public void testNoCelebrity() {
17-
int[][] party = {{0, 1, 0}, {1, 0, 1}, {1, 1, 0}};
18-
assertEquals(-1, CelebrityFinder.findCelebrity(party));
18+
private static Stream<Arguments> providePartyMatrices() {
19+
return Stream.of(
20+
// Test case 1: Celebrity exists
21+
Arguments.of(new int[][] {{0, 1, 1}, {0, 0, 1}, {0, 0, 0}}, 2),
22+
23+
// Test case 2: No celebrity
24+
Arguments.of(new int[][] {{0, 1, 0}, {1, 0, 1}, {1, 1, 0}}, -1),
25+
26+
// Test case 3: Everyone knows each other, no celebrity
27+
Arguments.of(new int[][] {{0, 1, 1}, {1, 0, 1}, {1, 1, 0}}, -1),
28+
29+
// Test case 4: Single person, they are trivially a celebrity
30+
Arguments.of(new int[][] {{0}}, 0),
31+
32+
// Test case 5: All know the last person, and they know no one
33+
Arguments.of(new int[][] {{0, 1, 1, 1}, {0, 0, 1, 1}, {0, 0, 0, 1}, {0, 0, 0, 0}}, 3),
34+
35+
// Test case 6: Larger party with no celebrity
36+
Arguments.of(new int[][] {{0, 1, 1, 0}, {1, 0, 0, 1}, {0, 1, 0, 1}, {1, 1, 1, 0}}, -1),
37+
38+
// Test case 7: Celebrity at the start of the matrix
39+
Arguments.of(new int[][] {{0, 0, 0}, {1, 0, 1}, {1, 1, 0}}, 0));
1940
}
2041
}

0 commit comments

Comments
 (0)