| 
 | 1 | +package com.leetcode.arrays;  | 
 | 2 | + | 
 | 3 | +import static org.junit.jupiter.api.Assertions.assertEquals;  | 
 | 4 | + | 
 | 5 | +/**  | 
 | 6 | + * Level: Medium  | 
 | 7 | + * Problem Link: https://leetcode.com/problems/find-the-celebrity/  | 
 | 8 | + * Problem Description:  | 
 | 9 | + * Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there may exist one celebrity.  | 
 | 10 | + * The definition of a celebrity is that all the other n - 1 people know him/her but he/she does not know any of them.  | 
 | 11 | + *  | 
 | 12 | + * Now you want to find out who the celebrity is or verify that there is not one. The only thing you are allowed to do  | 
 | 13 | + * is to ask questions like: "Hi, A. Do you know B?" to get information of whether A knows B. You need to find out the  | 
 | 14 | + * celebrity (or verify there is not one) by asking as few questions as possible (in the asymptotic sense).  | 
 | 15 | + *  | 
 | 16 | + * You are given a helper function bool knows(a, b) which tells you whether A knows B. Implement a  | 
 | 17 | + * function int findCelebrity(n). There will be exactly one celebrity if he/she is in the party. Return the celebrity's  | 
 | 18 | + * label if there is a celebrity in the party. If there is no celebrity, return -1.  | 
 | 19 | + *  | 
 | 20 | + * Example 1:  | 
 | 21 | + *  | 
 | 22 | + * Input: graph = [  | 
 | 23 | + *   [1,1,0],  | 
 | 24 | + *   [0,1,0],  | 
 | 25 | + *   [1,1,1]  | 
 | 26 | + * ]  | 
 | 27 | + * Output: 1  | 
 | 28 | + * Explanation: There are three persons labeled with 0, 1 and 2. graph[i][j] = 1 means person i knows person j, otherwise  | 
 | 29 | + * graph[i][j] = 0 means person i does not know person j. The celebrity is the person labeled as 1 because both 0 and 2  | 
 | 30 | + * know him but 1 does not know anybody.  | 
 | 31 | + *  | 
 | 32 | + *  | 
 | 33 | + * Example 2:  | 
 | 34 | + *  | 
 | 35 | + * Input: graph = [  | 
 | 36 | + *   [1,0,1],  | 
 | 37 | + *   [1,1,0],  | 
 | 38 | + *   [0,1,1]  | 
 | 39 | + * ]  | 
 | 40 | + * Output: -1  | 
 | 41 | + * Explanation: There is no celebrity.  | 
 | 42 | + *  | 
 | 43 | + *  | 
 | 44 | + * Note: The directed graph is represented as an adjacency matrix, which is an n x n matrix where a[i][j] = 1 means  | 
 | 45 | + * person i knows person j while a[i][j] = 0 means the contrary. Remember that you won't have direct access to the  | 
 | 46 | + * adjacency matrix.  | 
 | 47 | + *  | 
 | 48 | + * @author rampatra  | 
 | 49 | + * @since 2019-08-04  | 
 | 50 | + */  | 
 | 51 | +public class FindTheCelebrity {  | 
 | 52 | + | 
 | 53 | +    private int[][] knowsMatrix;  | 
 | 54 | + | 
 | 55 | +    FindTheCelebrity(int[][] knowsMatrix) {  | 
 | 56 | +        this.knowsMatrix = knowsMatrix;  | 
 | 57 | +    }  | 
 | 58 | + | 
 | 59 | +    public boolean knows(int a, int b) {  | 
 | 60 | +        return knowsMatrix[a][b] == 1;  | 
 | 61 | +    }  | 
 | 62 | + | 
 | 63 | +    /**  | 
 | 64 | +     * Time Complexity: O(n)  | 
 | 65 | +     * Space Complexity: O(1)  | 
 | 66 | +     * Runtime: <a href="https://leetcode.com/submissions/detail/249123409/">6 ms</a>.  | 
 | 67 | +     *  | 
 | 68 | +     * @param n  | 
 | 69 | +     * @return  | 
 | 70 | +     */  | 
 | 71 | +    public int findCelebrity(int n) {  | 
 | 72 | +        int celebrityIndex = 0;  | 
 | 73 | + | 
 | 74 | +        for (int i = 1; i < n; i++) {  | 
 | 75 | +            // if a person doesn't know another person then he maybe a celebrity  | 
 | 76 | +            if (!knows(i, celebrityIndex)) {  | 
 | 77 | +                celebrityIndex = i;  | 
 | 78 | +            }  | 
 | 79 | +        }  | 
 | 80 | + | 
 | 81 | +        for (int i = 0; i < n; i++) {  | 
 | 82 | +            // verify whether the celebrity only knows himself and all other people know the celebrity  | 
 | 83 | +            if ((knows(celebrityIndex, i) && i != celebrityIndex) || !knows(i, celebrityIndex)) {  | 
 | 84 | +                return -1;  | 
 | 85 | +            }  | 
 | 86 | +        }  | 
 | 87 | + | 
 | 88 | +        return celebrityIndex;  | 
 | 89 | +    }  | 
 | 90 | + | 
 | 91 | +    public static void main(String[] args) {  | 
 | 92 | +        FindTheCelebrity findTheCelebrity = new FindTheCelebrity(new int[][]{  | 
 | 93 | +                {1, 1, 0},  | 
 | 94 | +                {0, 1, 0},  | 
 | 95 | +                {1, 1, 1}});  | 
 | 96 | + | 
 | 97 | +        assertEquals(1, findTheCelebrity.findCelebrity(3));  | 
 | 98 | + | 
 | 99 | +        findTheCelebrity = new FindTheCelebrity(new int[][]{  | 
 | 100 | +                {1, 0},  | 
 | 101 | +                {0, 1}});  | 
 | 102 | + | 
 | 103 | +        assertEquals(-1, findTheCelebrity.findCelebrity(2));  | 
 | 104 | +    }  | 
 | 105 | +}  | 
0 commit comments