• 277. Find the Celebrity


    题目:

    Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there may exist one celebrity. The definition of a celebrity is that all the other n - 1people know him/her but he/she does not know any of them.

    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 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 celebrity (or verify there is not one) by asking as few questions as possible (in the asymptotic sense).

    You are given a helper function bool knows(a, b) which tells you whether A knows B. Implement a function int findCelebrity(n), your function should minimize the number of calls to knows.

    Note: There will be exactly one celebrity if he/she is in the party. Return the celebrity's label if there is a celebrity in the party. If there is no celebrity, return -1.

    链接: http://leetcode.com/problems/find-the-celebrity/

    题解:

    在数组中找Celebrity。Celebrity的条件很特殊,首先所有人都认识Celebrity,其次Celebrity不认识任何人。一开始想的方法是Brute force,需要O(n2)遍历。看了Discuss以后发现我们需要好好利用第一个条件,就是假定candidate = 0,遍历数组,当knows(candidate, i)时, 假定i为candidate。这里因为所有人都认识Celebrity,所以遍历完数组以后candidate就是我们要验证的Celebrity。但也有还需要第二遍遍历来验证这个candidate是否满足我们的条件。

    Time Complexity - O(n), Space Complexity - O(1)

    /* The knows API is defined in the parent class Relation.
          boolean knows(int a, int b); */
    
    public class Solution extends Relation {
        public int findCelebrity(int n) {
            int candidate = 0;
            for(int i = 1; i < n; i++) {
                if(knows(candidate, i)) {
                    candidate = i;
                }
            }
            
            for(int i = 0; i < n; i++) {
                if(i != candidate && (knows(candidate, i) || !knows(i, candidate))) {
                    return -1;
                } 
            }
            
            return candidate;    
        }
    }

    二刷:

    还是和一刷方法一样。先假定candidate = 0,从1开始遍历数组,假如knows(candidate, i),则我们把candidate更新为i。第一次遍历完毕以后,我们再开始第二次遍历,来判断是否candidate确实为我们的解。

    Java:

    /* The knows API is defined in the parent class Relation.
          boolean knows(int a, int b); */
    
    public class Solution extends Relation {
        public int findCelebrity(int n) {
            if (n <= 1) return -1;
            int candidate = 0;
            for (int i = 1; i < n; i++) {
                if (knows(candidate, i)) candidate = i;
            }
            for (int i = 0; i < n; i++) {
                if (i != candidate && (knows(candidate, i) || !knows(i, candidate))) return -1;
            }
            return candidate;
        }
    }

    Reference:

    https://leetcode.com/discuss/56350/straight-forward-c-solution-with-explaination

    https://leetcode.com/discuss/56413/java-solution-two-pass

    https://leetcode.com/discuss/61140/java-python-o-n-calls-o-1-space-easy-to-understand-solution

    https://leetcode.com/discuss/56349/python-brute-force-solution-easy-understanding

  • 相关阅读:
    chart控件多个ChartArea
    winform chart画折线,波形图,多条数据
    C# Chart 折线图 多条数据展示
    task一个任务结束后执行另一个操作
    C#多线程同步 读写锁ReaderWriterLock的用法
    C# 多线程文件读写整理总结
    vue解决跨域问题
    接前端页面
    使用vue+zrender绘制体温单 三测单(2)
    使用vue+zrender绘制体温单 三测单(1)
  • 原文地址:https://www.cnblogs.com/yrbbest/p/5034846.html
Copyright © 2020-2023  润新知