Similar Problems
Similar Problems not available
Number Of People Aware Of A Secret - Leetcode Solution
Companies:
LeetCode: Number Of People Aware Of A Secret Leetcode Solution
Difficulty: Medium
Topics: dynamic-programming simulation
Problem Statement: There are n people standing in a circle, labeled from 0 to n - 1. You are given an array of integer arrays called 'graph' where graph[i] is a list of all the people that person i trusts. More formally, the ith person trusts all the people in the list graph[i]. Return the total number of people that are aware of the secret if the secret is initially known by only one person.
Example: Input: n = 3, graph = [[1,2],[0,2],[0,1]] Output: 3
Solution Approach: We can solve this problem through graph traversal using either BFS or DFS. Since we want to count the number of people that are aware of the secret, we can start a BFS or DFS traversal from the person who knows the secret and keep a count of the visited nodes. We can keep track of visited nodes by maintaining a set of integers.
Steps:
- Start with the person who knows the secret and label this person as visited.
- Add this person to a queue or a stack.
- While the queue or stack is not empty, perform the following operations:
- Dequeue or pop the first element from the queue or stack.
- For the dequeued element, mark all its unvisited neighbors as visited and add them to the queue or stack.
- Increment the count of visited nodes by 1.
- Return the count of visited nodes.
We can use either the iterative or recursive approach for DFS traversal, both will give us the same result. The time complexity of the BFS and DFS traversal is O(n + m), where n is the number of nodes and m is the number of edges in the graph.
Code:
Here is the Python code for the iterative BFS approach to solve the above problem:
from collections import deque
class Solution:
def countNodes(self, n: int, graph: List[List[int]]) -> int:
# Initialize the visited set with the first person who knows the secret
visited = set([0])
# Initialize a queue with the first person
queue = deque([0])
# Initialize the count of visited nodes to 1
count = 1
# Start BFS traversal
while queue:
# Dequeue the first element from the queue
curr = queue.popleft()
# Iterate over the neighbors of the dequeued node
for neighbor in graph[curr]:
# If the neighbor is not already visited, mark it as visited and add it to the queue
if neighbor not in visited:
visited.add(neighbor)
queue.append(neighbor)
# Increment the count of visited nodes
count += 1
return count
We can solve this problem through DFS traversal also, here is the code:
class Solution:
def countNodes(self, n: int, graph: List[List[int]]) -> int:
# Initialize the visited set with the first person who knows the secret
visited = set([0])
# Initialize the count of visited nodes to 1
count = 1
# Recursive DFS traversal
def dfs(node: int):
nonlocal visited, count
# Iterate over the neighbors of the current node
for neighbor in graph[node]:
# If the neighbor is not already visited, mark it as visited and increment the count of visited nodes
if neighbor not in visited:
visited.add(neighbor)
count += 1
# Recursively visit the neighbor node
dfs(neighbor)
# Start DFS traversal from the first person who knows the secret
dfs(0)
return count
Both implementations will give us the same result, the choice between BFS and DFS depends on the nature of the problem.
Number Of People Aware Of A Secret Solution Code
1