vivek_ragi

CELEBRITY

Jun 30th, 2021
969
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* The knows API is defined for you.
  2.       bool knows(int a, int b); */
  3. //1,2,3
  4. class Solution {
  5. public:
  6.     int findCelebrity(int n) {
  7.        int cand = 0;
  8.         for (int i = 0; i < n; ++i) {
  9.             if(knows(cand,i)) {
  10.                 cand = i;
  11.             }
  12.         }
  13.         int ans = 0;
  14.         for(int i = 0; i < n; ++i) {
  15.             if(knows(i,cand) and !knows(cand,i)) {
  16.                 ans++;
  17.             }
  18.         }
  19.        
  20.         return ans == n - 1 ? cand : -1;
  21.     }
  22. };
RAW Paste Data