Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* The knows API is defined for you.
- bool knows(int a, int b); */
- //1,2,3
- class Solution {
- public:
- int findCelebrity(int n) {
- int cand = 0;
- for (int i = 0; i < n; ++i) {
- if(knows(cand,i)) {
- cand = i;
- }
- }
- int ans = 0;
- for(int i = 0; i < n; ++i) {
- if(knows(i,cand) and !knows(cand,i)) {
- ans++;
- }
- }
- return ans == n - 1 ? cand : -1;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement