Find All People With Secret Problem

Find All People With Secret Problem Statement

There are n people numbered from 0 to n - 1. A list of meetings is given whose each entry is a tuple [xi, yi, timei], indicating that the person xi and the person yi have a meeting at timei.
Person 0 has a secret and initially shares the secret with a given person at time 0. This secret is then shared every time a meeting takes place with the person that has the secret.
Return a list of all the people who have the secret after all the meetings have taken place.

Example One

{
"n": 6,
"meetings": [
[1, 2, 3],
[2, 3, 5],
[1, 5, 2],
[3, 5, 10]
],
"first_person": 2
}

Output:

[0, 1, 2, 3, 5]

At time 0, person 0 shares the secret with person 2.
At time 2, neither person 1 nor person 5 knows the secret.
At time 3, person 2 shares the secret with person 1.
At time 5, person 2 shares the secret with person 3.
At time 10, person 3 shares the secret with person 5.
Thus, people 0, 1, 2, 3, and 5 know the secret after all the meetings.

Example Two

{
"n": 5,
"meetings": [
[2, 3, 1],
[2, 4, 1],
[1, 2, 1]
],
"first_person": 1
}

Output:

[0, 1, 2, 3, 4]

At time 0, person 0 shares the secret with person 1.
At time 1, person 1 shares the secret with person 2, and person 2 shares the secret with persons 3 and 4.
Thus, people 0, 1, 2, 3, and 4 know the secret after all the meetings.

Notes

  • A person may attend multiple meetings at the same time.
  • The secrets are shared instantaneously, which means a person may receive the secret and share it with people in other meetings within the same time frame.
  • Return the output list in any order.

Constraints:

  • 2 <= n <= 105
  • 0 <= size of the input list <= 105
  • 0 <= xi, yi <= n – 1
  • xi != yi
  • 1 <= timei <= 105
  • 1 <= first person with whom secret is shared at time 0 <= n – 1

Code For Find All People With Secret Solution: Bfs

/*
Asymptotic complexity in terms of the number of meetings `m` and the number of people `n`:
* Time: O(m * log(m) + n).
* Auxiliary space: O(n + m).
* Total space: O(n + m).
*/
vector<int> find_all_people_with_secret(int n, vector<vector<int>> &meetings, int first_person) {
    vector<vector<pair<int,int>>> graph(n);
    for (int i = 0; i < meetings.size(); i++) {
        graph[meetings[i][0]].push_back({meetings[i][1], meetings[i][2]});
        graph[meetings[i][1]].push_back({meetings[i][0], meetings[i][2]});
    }

    // Min Heap which stores a pair of elements where the first entry denotes the current
    // time and the second entry denotes the person who knows the secret.
    priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;
    vector<int> is_secret_known(n, false);

    pq.push({0, 0});
    pq.push({0, first_person});

    while (!pq.empty()) {
        int current_time = pq.top().first;
        int person_x = pq.top().second;
        pq.pop();

        if (is_secret_known[person_x]) {
            continue;
        }

        is_secret_known[person_x] = true;

        for (int i = 0; i < graph[person_x].size(); i++) {
            int person_y = graph[person_x][i].first;
            int meeting_time = graph[person_x][i].second;
            if (is_secret_known[person_y]) {
                continue;
            }
            if (meeting_time >= current_time) {
                pq.push({meeting_time, person_y});
            }
        }
    }

    vector<int> people;
    for (int i = 0; i < n; i++) {
        if (is_secret_known[i]) {
            people.push_back(i);
        }
    }

    return people;
}

We hope that these solutions to the 4 sum problem have helped you level up your coding skills. You can expect problems like these at top tech companies like Amazon and Google.

If you are preparing for a tech interview at FAANG or any other Tier-1 tech company, register for Interview Kickstart’s FREE webinar to understand the best way to prepare.

Interview Kickstart offers interview preparation courses taught by FAANG+ tech leads and seasoned hiring managers. Our programs include a comprehensive curriculum, unmatched teaching methods, and career coaching to help you nail your next tech interview.

We offer 18 interview preparation courses, each tailored to a specific engineering domain or role, including the most in-demand and highest-paying domains and roles, such as:

‍To learn more, register for the FREE webinar.

Try yourself in the Editor

Note: Input and Output will already be taken care of.

Add Two Numbers Represented By Lists Problem

Binary Tree Level Order Traversal Problem with Examples

Jump Game

Zigzag Sort Problem

Boggle Solver Problem

Shortest String Transformation Using A Dictionary Problem

Register for our webinar

How to Nail your next Technical Interview

Loading_icon
Loading...
1 Enter details
2 Select slot
By sharing your contact details, you agree to our privacy policy.

Select a Date

Time slots

Time Zone:

Get tech interview-ready to navigate a tough job market

Best suitable for: Software Professionals with 5+ years of exprerience
Register for our FREE Webinar

Next webinar starts in

00
DAYS
:
00
HR
:
00
MINS
:
00
SEC