|
| 1 | +package joshua.leetcode.array; |
| 2 | + |
| 3 | +import java.util.LinkedList; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +/** |
| 7 | + * 277. Find the Celebrity<br/> |
| 8 | + * <p/> |
| 9 | + * <a href="https://leetcode.com/problems/find-the-celebrity/">leetcode link</a> |
| 10 | + * |
| 11 | + * @author Joshua.Jiang on 2016/5/22. |
| 12 | + */ |
| 13 | +public abstract class FindCelebrity { |
| 14 | + |
| 15 | + /** |
| 16 | + * Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there may exist one celebrity. |
| 17 | + * <p/> |
| 18 | + * 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. |
| 19 | + * <p/> |
| 20 | + * Now you want to find out who the celebrity is or verify that there is not one. |
| 21 | + * <p/> |
| 22 | + * The only thing you are allowed to do is to ask questions like: "Hi, A. Do you know B?" to get information of whether A knows B. |
| 23 | + * <p/> |
| 24 | + * You need to find out the celebrity (or verify there is not one) by asking as few questions as possible (in the asymptotic sense). |
| 25 | + * <p/> |
| 26 | + * You are given a helper function bool knows(a, b) which tells you whether A knows B. |
| 27 | + * |
| 28 | + * Implement a function int findCelebrity(n), your function should minimize the number of calls to knows. |
| 29 | + * |
| 30 | + * @param n the number of persons |
| 31 | + * @return -1 if no celebrity or the label of celebrity |
| 32 | + */ |
| 33 | + public abstract int findCelebrity(int n); |
| 34 | + |
| 35 | + /** |
| 36 | + * |
| 37 | + * @param a |
| 38 | + * @param b |
| 39 | + * @return true if a knows b else false. |
| 40 | + */ |
| 41 | + public abstract boolean knows(int a, int b); |
| 42 | + |
| 43 | + public abstract static class Solution1 extends FindCelebrity { |
| 44 | + |
| 45 | + @Override |
| 46 | + public int findCelebrity(int n) { |
| 47 | + // the person to search is initially the persons whom person with label 0 knows. |
| 48 | + // the this list must contains celebrity |
| 49 | + List<Integer> personsToSearch = new LinkedList<Integer>(); |
| 50 | + for(int i = 0; i < n; i++) { |
| 51 | + personsToSearch.add(i); |
| 52 | + } |
| 53 | + if (personsToSearch.size() == 0) { |
| 54 | + return -1; |
| 55 | + } |
| 56 | + if (personsToSearch.size() == 1) { |
| 57 | + return 0; |
| 58 | + } |
| 59 | + int celebrity = -1; |
| 60 | + //everytime shrink the size of persons to search |
| 61 | + while(true) { |
| 62 | + List<Integer> temp = new LinkedList<Integer>(); |
| 63 | + for (int i =1; i < personsToSearch.size(); i++) { |
| 64 | + if(knows(personsToSearch.get(0), personsToSearch.get(i))) { |
| 65 | + temp.add(personsToSearch.get(i)); |
| 66 | + } |
| 67 | + } |
| 68 | + if (temp.size() == 0) { |
| 69 | + // personsToSearch.get(0) is likely to be a celebrity, 'cause he know no one else in the personToSearch |
| 70 | + // list |
| 71 | + celebrity = personsToSearch.get(0); |
| 72 | + break; |
| 73 | + } |
| 74 | + if (temp.size() == 1) { |
| 75 | + // temp.get(0) is likely to be a celebrity, 'cause the first person in personToSearch list only know |
| 76 | + // this guy |
| 77 | + celebrity = temp.get(0); |
| 78 | + break; |
| 79 | + } |
| 80 | + personsToSearch = temp; |
| 81 | + } |
| 82 | + // validate if the celebrity is true |
| 83 | + for (int i = 0; i < n; i++) { |
| 84 | + if (i != celebrity) { |
| 85 | + if (knows(celebrity, i)) { |
| 86 | + return -1; |
| 87 | + } |
| 88 | + if (!knows(i, celebrity)) { |
| 89 | + return -1; |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + return celebrity; |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments