LeetCode 277. Find the Celebrity(找名人)

【Leetcode刷题】277. Find the Celebrity 搜寻名人 【Leetcode刷题】277. Find the Celebrity 搜寻名人 Link: leetcode277 (English version, 中文站需会员) 问题描述 相关Topics: Array, 贪心算法 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 阅读详情

原题网址:https://leetcode.com/problems/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.

方法:如果a认识b,则a不会是名人;如果a不认识b,则b不会是名人。因此每询问一次a是否认识b,都可以排除掉一个人,所以在O(n)时间内就可以排除掉n-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 celebrity = 0;
        for(int i=1; i<n; i++) {
            if (knows(celebrity, i)) celebrity = i;
        }
        for(int i=0; i<n; i++) {
            if (celebrity != i && knows(celebrity, i)) return -1;
            if (celebrity != i && !knows(i, celebrity)) return -1;
        }
        return celebrity;
    }
}


SIwave仿真手册——信号完整性仿真之S参数的提取(二) 上一篇简单了讲述了软件的各个操作界面,本次博客更新关于的仿真,因为第二章完整的内容比较多,所以分成好几篇博客进行分享。 阅读详情

相关推荐

避坑指南:Livox Mid-360双雷达SLAM配置中,那些官方文档没说的细节(IP设置、话题分离与时间同步)

本文详细解析了Livox Mid-360双雷达SLAM配置中的关键优化点,包括IP设置、话题分离与时间同步等官方文档未提及的细节。通过实战案例和代码示例,帮助开发者解决点云错位、IMU数据混乱等问题,提升SLAM系统的稳定性和精度。特别针对网络架构设计、驱动修改和时间同步机制提供了专业指导。

weixin_29745609的博客 430

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 - 1 people know him/her but he/s...

leetcode 解题思路 401

深入解析Dataloader中shuffle机制与随机种子对模型训练的影响

本文深入探讨了Dataloader中shuffle机制与随机种子对机器学习模型训练的影响。通过实际案例分析,详细解释了数据打乱顺序的必要性、随机种子的正确设置方法以及两者协同工作的原理,帮助开发者避免常见陷阱并提升模型训练效果。

weixin_29166013的博客 143

leetcode:277. 搜寻名人

graph[i][j] = 1 代表编号为 i 的人认识编号为 j 的人,而 graph[i][j] = 0 则代表编号为 i 的人不认识编号为 j 的人。“名人” 是编号 1 的人,因为 0 和 2 均认识他/她,但 1 不认识任何人。所谓 “名人” 的定义是:其他所有 n - 1 个人都认识他/她,而他/她并不认识其他任何人。该有向图是以邻接矩阵的形式给出的,是一个 n × n 的矩阵, a[i][j] = 1 代表 i 与 j 认识,a[i][j] = 0 则代表 i 与 j 不认识。...

OceanStar的博客 936

ks-快手滑块验证码-Js逆向

本文分析了快手滑块验证码的破解过程。首先通过抓包找到验证码背景图接口,发现需要动态获取captchaSession参数。然后深入分析验证流程,发现verifyParam参数被加密处理,需要追踪其生成逻辑。文章详细解析了加密参数c的构成,包括captchaSn、滑动距离、轨迹信息等,并指出轨迹验证较为严格。最后通过对比浏览器和本地生成的加密值,确认加密逻辑正确,但指出轨迹生成仍需优化。作者建议参考相关文章深入分析轨迹逻辑,并提供了验证成功的截图作为参考。

2302_76420666的博客 1887

VMware vCenter 6.7证书过期急救指南:从503报错到完美修复的全流程

本文详细解析了VMware vCenter 6.7因STS证书过期导致503报错的根本原因,并提供了一套从诊断到修复的完整实战流程。内容包括使用checksts.py脚本检测STS证书状态,通过fixsts.sh脚本更新证书,以及利用Certificate-Manager全面重置所有过期证书,帮助管理员快速恢复服务并避免常见陷阱。

weixin_29234239的博客 785

277. 搜寻名人

277. 搜寻名人 难度:中等 假设你是一个专业的狗仔,参加了一个 n 人派对,其中每个人被从 0 到 n - 1 标号。在这个派对人群当中可能存在一位 “名人”。所谓 “名人” 的定义是:其他所有 n - 1 个人都认识他/她,而他/她并不认识其他任何人。 现在你想要确认这个 “名人” 是谁,或者确定这里没有 “名人”。而你唯一能做的就是问诸如 “A 你好呀,请问你认不认识 B呀?” 的问题,以确定 A 是否认识 B。你需要在(渐近意义上)尽可能少的问题内来确定这位 “名人” 是谁(或者确定这里没有 “名

Finish_Hou 281

Tessent ATPG进阶:解锁多种Fault Model的工程实践与选型指南

本文深入探讨Tessent ATPG中多种Fault Model的工程实践与选型策略,涵盖Stuck-at、Transition、Bridge等核心模型的应用场景与配置技巧。通过实际案例解析,展示如何组合不同Fault Model提升芯片测试覆盖率,特别针对汽车电子、超低功耗芯片等复杂场景提供专业解决方案。

weixin_30219613的博客 208

LeetCode 277. 搜寻名人(思维题)

文章目录1. 题目2. 解题2.1 暴力解2.2 高效解 1. 题目 假设你是一个专业的狗仔,参加了一个 n 人派对,其中每个人被从 0 到 n - 1 标号。 在这个派对人群当中可能存在一位 “名人”。 所谓 “名人” 的定义是:其他所有 n - 1 个人都认识他/她,而他/她并不认识其他任何人。 现在你想要确认这个 “名人” 是谁,或者确定这里没有 “名人”。 而你唯一能做的就是问诸如 “A 你好呀,请问你认不认识 B呀?” 的问题,以确定 A 是否认识 B。 你需要在(渐近意义上)尽可能少的问题内来确

Michael是个半路程序员 2964

第1章 大数据概述

三次信息化浪潮 信息化浪潮 发生时间 标志 解决的问题 代表企业 第一次 1980年前后 个人计算机 信息处理 Intel、AMD、IBM、苹果、微软、联想、戴尔、惠普等 第二次 1995年前后 互联网 信息传输

wyz191的专栏 7092

扣子Coze工作流教程:采集公众号爆文,一键仿写自动发布

通过本教程,你学会了如何利用扣子(Coze)平台构建一个完整的公众号文章自动处理工作流。这个系统不仅能够大幅提升内容产出效率,还能通过AI二创保持内容新鲜度和原创性。最佳实践建议:初次使用时,建议先保存为草稿而不是直接发布,以便人工审核;从简单的工作流开始,逐步增加复杂度;定期检查工作流运行日志,优化节点配置。自动化内容创作是新媒体运营的未来趋势,掌握这项技能将使你在数字内容领域占据显著优势。

互联网架构师笔记 1253

leetcode——277.寻找名人

题目 有一个名人或者没有名人 所有人都认识名人,名人不认识所有人 思路 代码 bool knows(int a, int b); class Solution { public: int findCelebrity(int n) { int k = 0; //通过所有人认识名人 将其放上擂台 for(int i = 1; i < n; i++) k = knows(i, k)?k:i;

Dedication_的博客 610

[P4V]Perforce(P4V)使用教程

1.前言 Perforce SCM System是一款构建于可伸缩客户/服务器结构之上的软件配置管理工具。仅仅应用TCP/IP,开发人员就能够通过多种Perforce客户端(几种平台的GUI、WEB、或命令行)访问Perforce服务器。Perforce能够被快速和容易地部署,即使对大型或分布式的站点,也只要求最小限度的管理。Perforce可以使用在50多个操作系统中,它包括版...

元直的博客 2万+

[leetcode]277. Find the Celebrity 找名人

Suppose you are at a party withnpeople (labeled from0ton - 1) and among them, there may exist one celebrity. The definition of a celebrity is that all the othern - 1people know him/her but he/s...

weixin_30537391的博客 154

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

[leetcode 277]Find the Celebrity---------找名人

Question: 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 - 1 people know

清空 1072

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

[leetcode] 277. Find the Celebrity 解题报告

题目链接:https://leetcode.com/problems/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 cele

小榕流光的专栏 4005

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

leetcode 277: Find the Celebrity

Find the Celebrity Total Accepted: 1126 Total Submissions: 3603 Difficulty: Medium Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there may exist

西施豆腐渣 6616

LeetCode 277 Find the Celebrity

思路 打擂台。每一次名人测试knows(ans,i)可以排除一个人的可能性:knows为真,则ans不是名人;否则i不是名人。首先将0放在擂台ans上,从1开始依次与擂台上的元素进行pk,如果knows为真则ans不是名人,将擂台上的元素换成i;否则i不是名人,擂台上依旧为原来的元素。最后经过n-1次比较,会剩下一个擂台上的元素,对其进行名人测试即可(!knows(ans,i)+knows(i,a...

我要上岸的博客 415

[LeetCode] 277. Find the Celebrity 寻找名人$

参考:https://www.cnblogs.com/grandyang/p/5310649.html 在一个派对上有n个人,其中有一位名人。名人的定义是其他n-1个人都认识他,但他不认识任何一个人。要找出这位名人,只允许问A是否认识B。实施一个函数,找出名人,如果有返回他的label,如果没有返回-1。 Example 1: Input: graph = [ [1,1,0], ...

qq_41543888的博客 661

LeetCode #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 - 1 people know him/her b...

LawFile的博客 401

Leetcode 277——Find the Celebrity

Problem Description 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 - 1 people k...

jhhuang 277

【Leetcode】277. Find the Celebrity

题目地址: https://leetcode.com/problems/find-the-celebrity/ 有一个有向图有nnn个顶点,编号为0∼n−10\sim n-10∼n−1。其边以邻接矩阵knows(a, b)给出,若knows(a, b) = true说明从a到b有一条有向边,否则说明没有。要求在图中寻找唯一的那个入度为n−1n-1n−1出度为000的点。 只需计算每个点的入度与出度之差即可。思路可以参考https://blog.csdn.net/qq_46105170/article/det

数学、算法爱好者的博客 264

【LeetCode】277. Find the Celebrity 解题报告 (C++)

作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录题目描述题目大意解题方法暴力日期 题目地址:https://leetcode-cn.com/problems/find-the-celebrity/ 题目描述 Suppose you are at a party with n people (labeled fr...

负雪明烛 868

LeetCode 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

xinqrs01的博客 345

leetcode 277. Find the Celebrity【思维】

277.Find the Celebrity Medium 71894FavoriteShare Suppose you are at a party withnpeople (labeled from0ton - 1) and among them, there may exist one celebrity. The definition of a celebrity is ...

MissZhou要努力 276

Leetcode 277 Find the Celebrity

思路: 排除法。这边我们需要明确的是knows(i,j)= true意味着i不可能是名人,j有可能是名人;knows(i,j) = false意味着j不可能是名人,i可能是名人。我们先假设0是名人(candidate),那么我们从1开始遍历数组,假设当前的数字为i,那么我们要做什么呢?我们需要check knows(candidate,i)是否为false,如果是的话那么candidate不变,否则的话candidate变为i。每一次循环都会排除掉一个人,并且整个循环下来每个人都被check过一次。最..

GoodJobJasper的博客 180
上一篇: LeetCode 276. Paint Fence(篱笆涂色)
下一篇: LeetCode 278. First Bad Version(首个坏版本)
jmspan
博客等级 码龄11年 99粉丝 431原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值