📜  名人问题 |第 3 组

📅  最后修改于: 2022-05-13 01:56:09.660000             🧑  作者: Mango

名人问题 |第 3 组

在 N 人的聚会中,每个人都只认识一个人。这样的人可能出现聚会上,如果是,(s)他不认识聚会中的任何人。我们只能问“ A知道B吗? “。在最少的问题中找到陌生人(名人)。
我们可以将问题输入描述为代表聚会中人员的数字/字符数组。我们还有一个假设函数HaveAcquaintance(A, B)如果 A 知道 B,则返回true ,否则返回false
还给定一个列表know[],其中know[i] 以{a, b} 的形式表示,表示人a 认识人b。

例子

方法:创建一个大小为N的 int向量,如果有人认识他,则增加第 K个人的值,当他认识某人时,减少其值。最终,价值为 N-1将成为派对名人
下面是上述问题的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the party celebrity
int findPartyCelebrity(
    int n,
    vector >& know)
{
 
    vector celebrity(n + 1, 0);
    for (int i = 0; i < know.size(); i++) {
        celebrity[know[i][0]]--;
        celebrity[know[i][1]]++;
    }
    int party_celebrity = -1;
    for (int i = 1; i <= n; i++)
        if (celebrity[i] == n - 1)
            party_celebrity = i;
 
    return party_celebrity;
}
 
// Driver Code
int main()
{
    int n = 3;
    vector > know
        = { { 1, 3 }, { 2, 3 }, { 3, 1 } };
    cout << findPartyCelebrity(n, know);
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG {
 
  // Function to find the party celebrity
  static int findPartyCelebrity( int n, int[][] know)
  {
 
    int celebrity[] = new int[n + 1];
    for (int i = 0; i < know.length; i++) {
      celebrity[know[i][0]]--;
      celebrity[know[i][1]]++;
    }
    int party_celebrity = -1;
    for (int i = 1; i <= n; i++)
      if (celebrity[i] == n - 1)
        party_celebrity = i;
 
    return party_celebrity;
  }
 
  // Driver Code
  public static void main (String[] args) {
    int n = 3;
    int[][] know = { { 1, 3 }, { 2, 3 }, { 3, 1 } };
    System.out.println(findPartyCelebrity(n, know));
  }
}
 
// This code is contributed by hrithikgarg03188.


Python3
# Python code for the above approach
 
# Function to find the party celebrity
def findPartyCelebrity(n, know):
 
    celebrity = [0] * (n + 1)
    for i in range(len(know)):
        celebrity[know[i][0]] -= 1
        celebrity[know[i][1]] += 1
 
    party_celebrity = -1;
    for i in range(1, n + 1):
        if (celebrity[i] == n - 1):
            party_celebrity = i;
 
    return party_celebrity;
 
# Driver Code
n = 3;
know = [[1, 3], [2, 3], [3, 1]];
print(findPartyCelebrity(n, know));
 
# This code is contributed by Saurabh Jaiswal


C#
// C# program for the above approach
using System;
class GFG {
 
  // Function to find the party celebrity
  static int findPartyCelebrity( int n, int[,] know)
  {
 
    int []celebrity = new int[n + 1];
    for (int i = 0; i < know.GetLength(0); i++) {
      celebrity[know[i, 0]]--;
      celebrity[know[i, 1]]++;
    }
    int party_celebrity = -1;
    for (int i = 1; i <= n; i++)
      if (celebrity[i] == n - 1)
        party_celebrity = i;
 
    return party_celebrity;
  }
 
  // Driver Code
  public static void Main () {
    int n = 3;
    int[,] know = { { 1, 3 }, { 2, 3 }, { 3, 1 } };
    Console.WriteLine(findPartyCelebrity(n, know));
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
-1

时间复杂度:O(N)
辅助空间:O(N)