Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5fa7c08

Browse files
feat: solve No.787,2092
1 parent 6607052 commit 5fa7c08

File tree

2 files changed

+256
-0
lines changed

2 files changed

+256
-0
lines changed
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
# 2092. Find All People With Secret
2+
3+
- Difficulty: Hard.
4+
- Related Topics: Depth-First Search, Breadth-First Search, Union Find, Graph, Sorting.
5+
- Similar Questions: Reachable Nodes In Subdivided Graph.
6+
7+
## Problem
8+
9+
You are given an integer `n` indicating there are `n` people numbered from `0` to `n - 1`. You are also given a **0-indexed** 2D integer array `meetings` where `meetings[i] = [xi, yi, timei]` indicates that person `xi` and person `yi` have a meeting at `timei`. A person may attend **multiple meetings** at the same time. Finally, you are given an integer `firstPerson`.
10+
11+
Person `0` has a **secret** and initially shares the secret with a person `firstPerson` at time `0`. This secret is then shared every time a meeting takes place with a person that has the secret. More formally, for every meeting, if a person `xi` has the secret at `timei`, then they will share the secret with person `yi`, and vice versa.
12+
13+
The secrets are shared **instantaneously**. That is, a person may receive the secret and share it with people in other meetings within the same time frame.
14+
15+
Return **a list of all the people that have the secret after all the meetings have taken place. **You may return the answer in **any order**.
16+
17+
18+
Example 1:
19+
20+
```
21+
Input: n = 6, meetings = [[1,2,5],[2,3,8],[1,5,10]], firstPerson = 1
22+
Output: [0,1,2,3,5]
23+
Explanation:
24+
At time 0, person 0 shares the secret with person 1.
25+
At time 5, person 1 shares the secret with person 2.
26+
At time 8, person 2 shares the secret with person 3.
27+
At time 10, person 1 shares the secret with person 5.​​​​
28+
Thus, people 0, 1, 2, 3, and 5 know the secret after all the meetings.
29+
```
30+
31+
Example 2:
32+
33+
```
34+
Input: n = 4, meetings = [[3,1,3],[1,2,2],[0,3,3]], firstPerson = 3
35+
Output: [0,1,3]
36+
Explanation:
37+
At time 0, person 0 shares the secret with person 3.
38+
At time 2, neither person 1 nor person 2 know the secret.
39+
At time 3, person 3 shares the secret with person 0 and person 1.
40+
Thus, people 0, 1, and 3 know the secret after all the meetings.
41+
```
42+
43+
Example 3:
44+
45+
```
46+
Input: n = 5, meetings = [[3,4,2],[1,2,1],[2,3,1]], firstPerson = 1
47+
Output: [0,1,2,3,4]
48+
Explanation:
49+
At time 0, person 0 shares the secret with person 1.
50+
At time 1, person 1 shares the secret with person 2, and person 2 shares the secret with person 3.
51+
Note that person 2 can share the secret at the same time as receiving it.
52+
At time 2, person 3 shares the secret with person 4.
53+
Thus, people 0, 1, 2, 3, and 4 know the secret after all the meetings.
54+
```
55+
56+
57+
**Constraints:**
58+
59+
60+
61+
- `2 <= n <= 105`
62+
63+
- `1 <= meetings.length <= 105`
64+
65+
- `meetings[i].length == 3`
66+
67+
- `0 <= xi, yi <= n - 1`
68+
69+
- `xi != yi`
70+
71+
- `1 <= timei <= 105`
72+
73+
- `1 <= firstPerson <= n - 1`
74+
75+
76+
77+
## Solution
78+
79+
```javascript
80+
/**
81+
* @param {number} n
82+
* @param {number[][]} meetings
83+
* @param {number} firstPerson
84+
* @return {number[]}
85+
*/
86+
var findAllPeople = function(n, meetings, firstPerson) {
87+
var map = Array(n).fill(0).map(() => []);
88+
var queue = new MinPriorityQueue();
89+
var hasSecretMap = Array(n);
90+
var handledMap = Array(meetings.length);
91+
hasSecretMap[0] = true;
92+
hasSecretMap[firstPerson] = true;
93+
for (var i = 0; i < meetings.length; i++) {
94+
map[meetings[i][0]].push([meetings[i][1], meetings[i][2], i]);
95+
map[meetings[i][1]].push([meetings[i][0], meetings[i][2], i]);
96+
queue.enqueue(
97+
[...meetings[i], i],
98+
(hasSecretMap[meetings[i][0]] || hasSecretMap[meetings[i][1]])
99+
? meetings[i][2] - 0.1
100+
: meetings[i][2]
101+
);
102+
}
103+
while (queue.size() !== 0) {
104+
var item = queue.dequeue().element;
105+
if (handledMap[item[3]]) continue;
106+
handledMap[item[3]] = true;
107+
if (!hasSecretMap[item[0]] && !hasSecretMap[item[1]]) continue;
108+
if (hasSecretMap[item[0]] && hasSecretMap[item[1]]) continue;
109+
var num = hasSecretMap[item[0]] ? item[1] : item[0];
110+
for (var i = 0; i < map[num].length; i++) {
111+
var data = map[num][i];
112+
if (handledMap[data[2]]) continue;
113+
if (hasSecretMap[data[0]]) continue;
114+
queue.enqueue(
115+
[num, data[0], data[1] - 0.1, data[2]],
116+
data[1] - 0.1
117+
);
118+
}
119+
hasSecretMap[num] = true;
120+
}
121+
return hasSecretMap.reduce((res, item, i) => {
122+
if (item) res.push(i);
123+
return res;
124+
}, []);
125+
};
126+
```
127+
128+
**Explain:**
129+
130+
Priority queue with dynamicly changes priority.
131+
132+
**Complexity:**
133+
134+
* Time complexity : O(n * log(n) + m).
135+
* Space complexity : O(n + m).
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
# 787. Cheapest Flights Within K Stops
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Dynamic Programming, Depth-First Search, Breadth-First Search, Graph, Heap (Priority Queue), Shortest Path.
5+
- Similar Questions: Maximum Vacation Days, Minimum Cost to Reach City With Discounts.
6+
7+
## Problem
8+
9+
There are `n` cities connected by some number of flights. You are given an array `flights` where `flights[i] = [fromi, toi, pricei]` indicates that there is a flight from city `fromi` to city `toi` with cost `pricei`.
10+
11+
You are also given three integers `src`, `dst`, and `k`, return ****the cheapest price** from **`src`** to **`dst`** with at most **`k`** stops. **If there is no such route, return** **`-1`.
12+
13+
14+
Example 1:
15+
16+
![](https://assets.leetcode.com/uploads/2022/03/18/cheapest-flights-within-k-stops-3drawio.png)
17+
18+
```
19+
Input: n = 4, flights = [[0,1,100],[1,2,100],[2,0,100],[1,3,600],[2,3,200]], src = 0, dst = 3, k = 1
20+
Output: 700
21+
Explanation:
22+
The graph is shown above.
23+
The optimal path with at most 1 stop from city 0 to 3 is marked in red and has cost 100 + 600 = 700.
24+
Note that the path through cities [0,1,2,3] is cheaper but is invalid because it uses 2 stops.
25+
```
26+
27+
Example 2:
28+
29+
![](https://assets.leetcode.com/uploads/2022/03/18/cheapest-flights-within-k-stops-1drawio.png)
30+
31+
```
32+
Input: n = 3, flights = [[0,1,100],[1,2,100],[0,2,500]], src = 0, dst = 2, k = 1
33+
Output: 200
34+
Explanation:
35+
The graph is shown above.
36+
The optimal path with at most 1 stop from city 0 to 2 is marked in red and has cost 100 + 100 = 200.
37+
```
38+
39+
Example 3:
40+
41+
![](https://assets.leetcode.com/uploads/2022/03/18/cheapest-flights-within-k-stops-2drawio.png)
42+
43+
```
44+
Input: n = 3, flights = [[0,1,100],[1,2,100],[0,2,500]], src = 0, dst = 2, k = 0
45+
Output: 500
46+
Explanation:
47+
The graph is shown above.
48+
The optimal path with no stops from city 0 to 2 is marked in red and has cost 500.
49+
```
50+
51+
52+
**Constraints:**
53+
54+
55+
56+
- `1 <= n <= 100`
57+
58+
- `0 <= flights.length <= (n * (n - 1) / 2)`
59+
60+
- `flights[i].length == 3`
61+
62+
- `0 <= fromi, toi < n`
63+
64+
- `fromi != toi`
65+
66+
- `1 <= pricei <= 104`
67+
68+
- There will not be any multiple flights between two cities.
69+
70+
- `0 <= src, dst, k < n`
71+
72+
- `src != dst`
73+
74+
75+
76+
## Solution
77+
78+
```javascript
79+
/**
80+
* @param {number} n
81+
* @param {number[][]} flights
82+
* @param {number} src
83+
* @param {number} dst
84+
* @param {number} k
85+
* @return {number}
86+
*/
87+
var findCheapestPrice = function(n, flights, src, dst, k) {
88+
var map = Array(n).fill(0).map(() => []);
89+
for (var i = 0; i < flights.length; i++) {
90+
map[flights[i][0]].push([flights[i][1], flights[i][2]]);
91+
}
92+
var dp = Array(n).fill(0).map(() => ({}));
93+
var res = dfs(src, dst, k, map, dp);
94+
return res;
95+
};
96+
97+
var dfs = function(src, dst, k, map, dp) {
98+
if (dp[src][k] !== undefined) return dp[src][k];
99+
if (src === dst) return 0;
100+
if (k === -1) return -1;
101+
var res = -1;
102+
for (var i = 0; i < map[src].length; i++) {
103+
var tmp = dfs(map[src][i][0], dst, k - 1, map, dp);
104+
if (tmp === -1) continue;
105+
if (res === -1 || res > tmp + map[src][i][1]) {
106+
res = tmp + map[src][i][1];
107+
}
108+
}
109+
dp[src][k] = res;
110+
return res;
111+
};
112+
```
113+
114+
**Explain:**
115+
116+
DFS + DP.
117+
118+
**Complexity:**
119+
120+
* Time complexity : O(n * k).
121+
* Space complexity : O(n * k).

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /