Skip to content

Commit 0020a30

Browse files
committed
24-02-2024
1 parent 31802ab commit 0020a30

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

2024/02 - February/24-02-2024.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
3+
Author : Manas Rawat
4+
Date : 24/02/2024
5+
Problem : Find All People With Secret
6+
Difficulty : Hard
7+
Problem Link : https://leetcode.com/problems/find-all-people-with-secret/description/
8+
Video Solution : NA
9+
10+
*/
11+
12+
13+
class Solution {
14+
public:
15+
vector<int> findAllPeople(int n, vector<vector<int>>& meetings, int firstPerson) {
16+
vector<int> ans(n + 1, 1e9);
17+
ans[0] = 0;
18+
ans[firstPerson] = 0;
19+
20+
vector<vector<pair<int,int>>> g(n + 1);
21+
22+
for(auto i : meetings) {
23+
g[i[0]].emplace_back(i[1], i[2]);
24+
g[i[1]].emplace_back(i[0], i[2]);
25+
}
26+
27+
priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;
28+
pq.push({0, firstPerson});
29+
pq.push({0, 0});
30+
31+
while(!pq.empty()) {
32+
int node, time;
33+
tie(time, node) = pq.top();
34+
pq.pop();
35+
36+
for(auto next : g[node]) {
37+
int child, ntime;
38+
tie(child, ntime) = next;
39+
40+
if(ntime >= time and ans[child] > ntime) {
41+
ans[child] = ntime;
42+
pq.push({ntime, child});
43+
}
44+
}
45+
}
46+
47+
vector<int> res;
48+
49+
for(int i = 0; i < n + 1; i++)
50+
if(ans[i] < 1e9)
51+
res.push_back(i);
52+
53+
return res;
54+
}
55+
};

0 commit comments

Comments
 (0)