From 40fe8ef0c99a85fdb41f5592318cb6e1581f9594 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sun, 14 Apr 2024 20:24:10 +0800 Subject: [PATCH] feat: add solutions to lc problems: No.3111,3112 * No.3111.Minimum Rectangles to Cover Points * No.3112.Minimum Time to Visit Disappearing Nodes --- .../README.md | 77 +++++++- .../README_EN.md | 77 +++++++- .../Solution.cpp | 15 ++ .../Solution.go | 11 ++ .../Solution.java | 15 ++ .../Solution.py | 9 + .../Solution.ts | 12 ++ .../README.md | 168 +++++++++++++++++- .../README_EN.md | 168 +++++++++++++++++- .../Solution.cpp | 41 +++++ .../Solution.go | 53 ++++++ .../Solution.java | 35 ++++ .../Solution.py | 20 +++ 13 files changed, 683 insertions(+), 18 deletions(-) create mode 100644 solution/3100-3199/3111.Minimum Rectangles to Cover Points/Solution.cpp create mode 100644 solution/3100-3199/3111.Minimum Rectangles to Cover Points/Solution.go create mode 100644 solution/3100-3199/3111.Minimum Rectangles to Cover Points/Solution.java create mode 100644 solution/3100-3199/3111.Minimum Rectangles to Cover Points/Solution.py create mode 100644 solution/3100-3199/3111.Minimum Rectangles to Cover Points/Solution.ts create mode 100644 solution/3100-3199/3112.Minimum Time to Visit Disappearing Nodes/Solution.cpp create mode 100644 solution/3100-3199/3112.Minimum Time to Visit Disappearing Nodes/Solution.go create mode 100644 solution/3100-3199/3112.Minimum Time to Visit Disappearing Nodes/Solution.java create mode 100644 solution/3100-3199/3112.Minimum Time to Visit Disappearing Nodes/Solution.py diff --git a/solution/3100-3199/3111.Minimum Rectangles to Cover Points/README.md b/solution/3100-3199/3111.Minimum Rectangles to Cover Points/README.md index 2e13be7206b49..32464aab8d1cf 100644 --- a/solution/3100-3199/3111.Minimum Rectangles to Cover Points/README.md +++ b/solution/3100-3199/3111.Minimum Rectangles to Cover Points/README.md @@ -93,24 +93,93 @@ ## 解法 -### 方法一 +### 方法一:贪心 + 排序 + +根据题目描述,我们不需要考虑矩形的高度,只需要考虑矩形的宽度。 + +我们可以将所有的点按照横坐标进行排序,用一个变量 $x_1$ 记录当前矩形的左下角的横坐标。然后遍历所有的点,如果当前点的横坐标 $x$ 比 $x_1 + w$ 大,说明当前点不能被当前的矩形覆盖,我们就需要增加一个新的矩形,然后更新 $x_1$ 为当前点的横坐标。 + +遍历完成后,我们就得到了最少需要多少个矩形。 + +时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 是点的数量。 ```python - +class Solution: + def minRectanglesToCoverPoints(self, points: List[List[int]], w: int) -> int: + points.sort() + ans, x1 = 0, -inf + for x, _ in points: + if x1 + w < x: + x1 = x + ans += 1 + return ans ``` ```java - +class Solution { + public int minRectanglesToCoverPoints(int[][] points, int w) { + Arrays.sort(points, (a, b) -> a[0] - b[0]); + int ans = 0; + int x1 = -(1 << 30); + for (int[] p : points) { + int x = p[0]; + if (x1 + w < x) { + x1 = x; + ++ans; + } + } + return ans; + } +} ``` ```cpp - +class Solution { +public: + int minRectanglesToCoverPoints(vector>& points, int w) { + sort(points.begin(), points.end()); + int ans = 0, x1 = -(1 << 30); + for (auto& p : points) { + int x = p[0]; + if (x1 + w < x) { + x1 = x; + ++ans; + } + } + return ans; + } +}; ``` ```go +func minRectanglesToCoverPoints(points [][]int, w int) (ans int) { + sort.Slice(points, func(i, j int) bool { return points[i][0] < points[j][0] }) + x1 := -(1 << 30) + for _, p := range points { + if x := p[0]; x1+w < x { + x1 = x + ans++ + } + } + return +} +``` +```ts +function minRectanglesToCoverPoints(points: number[][], w: number): number { + points.sort((a, b) => a[0] - b[0]); + let ans = 0; + let x1 = -Infinity; + for (const [x, _] of points) { + if (x1 + w < x) { + x1 = x; + ++ans; + } + } + return ans; +} ``` diff --git a/solution/3100-3199/3111.Minimum Rectangles to Cover Points/README_EN.md b/solution/3100-3199/3111.Minimum Rectangles to Cover Points/README_EN.md index 3f214e2927667..1fe0d621c7a91 100644 --- a/solution/3100-3199/3111.Minimum Rectangles to Cover Points/README_EN.md +++ b/solution/3100-3199/3111.Minimum Rectangles to Cover Points/README_EN.md @@ -134,24 +134,93 @@ ## Solutions -### Solution 1 +### Solution 1: Greedy + Sorting + +According to the problem description, we don't need to consider the height of the rectangle, only the width. + +We can sort all the points according to the x-coordinate and use a variable $x_1$ to record the current x-coordinate of the lower left corner of the rectangle. Then we traverse all the points. If the x-coordinate $x$ of the current point is greater than $x_1 + w$, it means that the current point cannot be covered by the current rectangle. We need to add a new rectangle and update $x_1$ to the x-coordinate of the current point. + +After the traversal, we get the minimum number of rectangles needed. + +The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$, where $n$ is the number of points. ```python - +class Solution: + def minRectanglesToCoverPoints(self, points: List[List[int]], w: int) -> int: + points.sort() + ans, x1 = 0, -inf + for x, _ in points: + if x1 + w < x: + x1 = x + ans += 1 + return ans ``` ```java - +class Solution { + public int minRectanglesToCoverPoints(int[][] points, int w) { + Arrays.sort(points, (a, b) -> a[0] - b[0]); + int ans = 0; + int x1 = -(1 << 30); + for (int[] p : points) { + int x = p[0]; + if (x1 + w < x) { + x1 = x; + ++ans; + } + } + return ans; + } +} ``` ```cpp - +class Solution { +public: + int minRectanglesToCoverPoints(vector>& points, int w) { + sort(points.begin(), points.end()); + int ans = 0, x1 = -(1 << 30); + for (auto& p : points) { + int x = p[0]; + if (x1 + w < x) { + x1 = x; + ++ans; + } + } + return ans; + } +}; ``` ```go +func minRectanglesToCoverPoints(points [][]int, w int) (ans int) { + sort.Slice(points, func(i, j int) bool { return points[i][0] < points[j][0] }) + x1 := -(1 << 30) + for _, p := range points { + if x := p[0]; x1+w < x { + x1 = x + ans++ + } + } + return +} +``` +```ts +function minRectanglesToCoverPoints(points: number[][], w: number): number { + points.sort((a, b) => a[0] - b[0]); + let ans = 0; + let x1 = -Infinity; + for (const [x, _] of points) { + if (x1 + w < x) { + x1 = x; + ++ans; + } + } + return ans; +} ``` diff --git a/solution/3100-3199/3111.Minimum Rectangles to Cover Points/Solution.cpp b/solution/3100-3199/3111.Minimum Rectangles to Cover Points/Solution.cpp new file mode 100644 index 0000000000000..cb90fdb4e6c24 --- /dev/null +++ b/solution/3100-3199/3111.Minimum Rectangles to Cover Points/Solution.cpp @@ -0,0 +1,15 @@ +class Solution { +public: + int minRectanglesToCoverPoints(vector>& points, int w) { + sort(points.begin(), points.end()); + int ans = 0, x1 = -(1 << 30); + for (auto& p : points) { + int x = p[0]; + if (x1 + w < x) { + x1 = x; + ++ans; + } + } + return ans; + } +}; \ No newline at end of file diff --git a/solution/3100-3199/3111.Minimum Rectangles to Cover Points/Solution.go b/solution/3100-3199/3111.Minimum Rectangles to Cover Points/Solution.go new file mode 100644 index 0000000000000..576eb533bc39b --- /dev/null +++ b/solution/3100-3199/3111.Minimum Rectangles to Cover Points/Solution.go @@ -0,0 +1,11 @@ +func minRectanglesToCoverPoints(points [][]int, w int) (ans int) { + sort.Slice(points, func(i, j int) bool { return points[i][0] < points[j][0] }) + x1 := -(1 << 30) + for _, p := range points { + if x := p[0]; x1+w < x { + x1 = x + ans++ + } + } + return +} \ No newline at end of file diff --git a/solution/3100-3199/3111.Minimum Rectangles to Cover Points/Solution.java b/solution/3100-3199/3111.Minimum Rectangles to Cover Points/Solution.java new file mode 100644 index 0000000000000..8f965d1e11c45 --- /dev/null +++ b/solution/3100-3199/3111.Minimum Rectangles to Cover Points/Solution.java @@ -0,0 +1,15 @@ +class Solution { + public int minRectanglesToCoverPoints(int[][] points, int w) { + Arrays.sort(points, (a, b) -> a[0] - b[0]); + int ans = 0; + int x1 = -(1 << 30); + for (int[] p : points) { + int x = p[0]; + if (x1 + w < x) { + x1 = x; + ++ans; + } + } + return ans; + } +} \ No newline at end of file diff --git a/solution/3100-3199/3111.Minimum Rectangles to Cover Points/Solution.py b/solution/3100-3199/3111.Minimum Rectangles to Cover Points/Solution.py new file mode 100644 index 0000000000000..8f7b702ffadfd --- /dev/null +++ b/solution/3100-3199/3111.Minimum Rectangles to Cover Points/Solution.py @@ -0,0 +1,9 @@ +class Solution: + def minRectanglesToCoverPoints(self, points: List[List[int]], w: int) -> int: + points.sort() + ans, x1 = 0, -inf + for x, _ in points: + if x1 + w < x: + x1 = x + ans += 1 + return ans diff --git a/solution/3100-3199/3111.Minimum Rectangles to Cover Points/Solution.ts b/solution/3100-3199/3111.Minimum Rectangles to Cover Points/Solution.ts new file mode 100644 index 0000000000000..e59de2938075f --- /dev/null +++ b/solution/3100-3199/3111.Minimum Rectangles to Cover Points/Solution.ts @@ -0,0 +1,12 @@ +function minRectanglesToCoverPoints(points: number[][], w: number): number { + points.sort((a, b) => a[0] - b[0]); + let ans = 0; + let x1 = -Infinity; + for (const [x, _] of points) { + if (x1 + w < x) { + x1 = x; + ++ans; + } + } + return ans; +} diff --git a/solution/3100-3199/3112.Minimum Time to Visit Disappearing Nodes/README.md b/solution/3100-3199/3112.Minimum Time to Visit Disappearing Nodes/README.md index 813f1845ca723..34ad733137908 100644 --- a/solution/3100-3199/3112.Minimum Time to Visit Disappearing Nodes/README.md +++ b/solution/3100-3199/3112.Minimum Time to Visit Disappearing Nodes/README.md @@ -86,24 +86,182 @@ ## 解法 -### 方法一 +### 方法一:堆优化的 Dijkstra + +我们先创建一个邻接表 $g$,用于存储图的边。然后创建一个数组 $dist$,用于存储从节点 $0$ 到其他节点的最短距离。初始化 $dist[0] = 0$,其余节点的距离初始化为无穷大。 + +然后,我们使用 Dijkstra 算法计算从节点 $0$ 到其他节点的最短距离。具体步骤如下: + +1. 创建一个优先队列 $q$,用于存储节点的距离和节点编号,初始时将节点 $0$ 加入队列,距离为 $0$。 +2. 从队列中取出一个节点 $u$,如果 $u$ 的距离 $du$ 大于 $dist[u]$,说明 $u$ 已经被更新过了,直接跳过。 +3. 遍历节点 $u$ 的所有邻居节点 $v$,如果 $dist[v] > dist[u] + w$ 且 $dist[u] + w < disappear[v]$,则更新 $dist[v] = dist[u] + w$,并将节点 $v$ 加入队列。 +4. 重复步骤 2 和步骤 3,直到队列为空。 + +最后,我们遍历 $dist$ 数组,如果 $dist[i] < disappear[i]$,则 $answer[i] = dist[i]$,否则 $answer[i] = -1$。 + +时间复杂度 $O(m \times \log m)$,空间复杂度 $O(m)$。其中 $m$ 是边的数量。 ```python - +class Solution: + def minimumTime( + self, n: int, edges: List[List[int]], disappear: List[int] + ) -> List[int]: + g = defaultdict(list) + for u, v, w in edges: + g[u].append((v, w)) + g[v].append((u, w)) + dist = [inf] * n + dist[0] = 0 + q = [(0, 0)] + while q: + du, u = heappop(q) + if du > dist[u]: + continue + for v, w in g[u]: + if dist[v] > dist[u] + w and dist[u] + w < disappear[v]: + dist[v] = dist[u] + w + heappush(q, (dist[v], v)) + return [a if a < b else -1 for a, b in zip(dist, disappear)] ``` ```java - +class Solution { + public int[] minimumTime(int n, int[][] edges, int[] disappear) { + List[] g = new List[n]; + Arrays.setAll(g, k -> new ArrayList<>()); + for (var e : edges) { + int u = e[0], v = e[1], w = e[2]; + g[u].add(new int[] {v, w}); + g[v].add(new int[] {u, w}); + } + int[] dist = new int[n]; + Arrays.fill(dist, 1 << 30); + dist[0] = 0; + PriorityQueue pq = new PriorityQueue<>((a, b) -> a[0] - b[0]); + pq.offer(new int[] {0, 0}); + while (!pq.isEmpty()) { + var e = pq.poll(); + int du = e[0], u = e[1]; + if (du > dist[u]) { + continue; + } + for (var nxt : g[u]) { + int v = nxt[0], w = nxt[1]; + if (dist[v] > dist[u] + w && dist[u] + w < disappear[v]) { + dist[v] = dist[u] + w; + pq.offer(new int[] {dist[v], v}); + } + } + } + int[] ans = new int[n]; + for (int i = 0; i < n; ++i) { + ans[i] = dist[i] < disappear[i] ? dist[i] : -1; + } + return ans; + } +} ``` ```cpp - +class Solution { +public: + vector minimumTime(int n, vector>& edges, vector& disappear) { + vector>> g(n); + for (const auto& e : edges) { + int u = e[0], v = e[1], w = e[2]; + g[u].push_back({v, w}); + g[v].push_back({u, w}); + } + + vector dist(n, 1 << 30); + dist[0] = 0; + + using pii = pair; + priority_queue, greater> pq; + pq.push({0, 0}); + + while (!pq.empty()) { + auto [du, u] = pq.top(); + pq.pop(); + + if (du > dist[u]) { + continue; + } + + for (auto [v, w] : g[u]) { + if (dist[v] > dist[u] + w && dist[u] + w < disappear[v]) { + dist[v] = dist[u] + w; + pq.push({dist[v], v}); + } + } + } + + vector ans(n); + for (int i = 0; i < n; ++i) { + ans[i] = dist[i] < disappear[i] ? dist[i] : -1; + } + + return ans; + } +}; ``` ```go - +func minimumTime(n int, edges [][]int, disappear []int) []int { + g := make([][]pair, n) + for _, e := range edges { + u, v, w := e[0], e[1], e[2] + g[u] = append(g[u], pair{v, w}) + g[v] = append(g[v], pair{u, w}) + } + + dist := make([]int, n) + for i := range dist { + dist[i] = 1 << 30 + } + dist[0] = 0 + + pq := hp{{0, 0}} + + for len(pq) > 0 { + du, u := pq[0].dis, pq[0].u + heap.Pop(&pq) + + if du > dist[u] { + continue + } + + for _, nxt := range g[u] { + v, w := nxt.dis, nxt.u + if dist[v] > dist[u]+w && dist[u]+w < disappear[v] { + dist[v] = dist[u] + w + heap.Push(&pq, pair{dist[v], v}) + } + } + } + + ans := make([]int, n) + for i := 0; i < n; i++ { + if dist[i] < disappear[i] { + ans[i] = dist[i] + } else { + ans[i] = -1 + } + } + + return ans +} + +type pair struct{ dis, u int } +type hp []pair + +func (h hp) Len() int { return len(h) } +func (h hp) Less(i, j int) bool { return h[i].dis < h[j].dis } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(pair)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } ``` diff --git a/solution/3100-3199/3112.Minimum Time to Visit Disappearing Nodes/README_EN.md b/solution/3100-3199/3112.Minimum Time to Visit Disappearing Nodes/README_EN.md index 08a5b2950ba4e..672fe662c50a0 100644 --- a/solution/3100-3199/3112.Minimum Time to Visit Disappearing Nodes/README_EN.md +++ b/solution/3100-3199/3112.Minimum Time to Visit Disappearing Nodes/README_EN.md @@ -82,24 +82,182 @@ ## Solutions -### Solution 1 +### Solution 1: Heap Optimized Dijkstra + +First, we create an adjacency list $g$ to store the edges of the graph. Then we create an array $dist$ to store the shortest distance from node $0$ to other nodes. We initialize $dist[0] = 0$ and the distance of other nodes is initialized to infinity. + +Then, we use Dijkstra's algorithm to calculate the shortest distance from node $0$ to other nodes. The specific steps are as follows: + +1. Create a priority queue $q$ to store the distance and node number of nodes. Initially, add node $0$ to the queue with a distance of $0$. +2. Take out a node $u$ from the queue. If the distance $du$ of $u$ is greater than $dist[u]$, it means that $u$ has been updated, so skip it directly. +3. Traverse all neighbor nodes $v$ of node $u$. If $dist[v] > dist[u] + w$ and $dist[u] + w < disappear[v]$, then update $dist[v] = dist[u] + w$ and add node $v$ to the queue. +4. Repeat steps 2 and 3 until the queue is empty. + +Finally, we traverse the $dist$ array. If $dist[i] < disappear[i]$, then $answer[i] = dist[i]$, otherwise $answer[i] = -1$. + +The time complexity is $O(m \times \log m)$, and the space complexity is $O(m)$, where $m$ is the number of edges. ```python - +class Solution: + def minimumTime( + self, n: int, edges: List[List[int]], disappear: List[int] + ) -> List[int]: + g = defaultdict(list) + for u, v, w in edges: + g[u].append((v, w)) + g[v].append((u, w)) + dist = [inf] * n + dist[0] = 0 + q = [(0, 0)] + while q: + du, u = heappop(q) + if du > dist[u]: + continue + for v, w in g[u]: + if dist[v] > dist[u] + w and dist[u] + w < disappear[v]: + dist[v] = dist[u] + w + heappush(q, (dist[v], v)) + return [a if a < b else -1 for a, b in zip(dist, disappear)] ``` ```java - +class Solution { + public int[] minimumTime(int n, int[][] edges, int[] disappear) { + List[] g = new List[n]; + Arrays.setAll(g, k -> new ArrayList<>()); + for (var e : edges) { + int u = e[0], v = e[1], w = e[2]; + g[u].add(new int[] {v, w}); + g[v].add(new int[] {u, w}); + } + int[] dist = new int[n]; + Arrays.fill(dist, 1 << 30); + dist[0] = 0; + PriorityQueue pq = new PriorityQueue<>((a, b) -> a[0] - b[0]); + pq.offer(new int[] {0, 0}); + while (!pq.isEmpty()) { + var e = pq.poll(); + int du = e[0], u = e[1]; + if (du > dist[u]) { + continue; + } + for (var nxt : g[u]) { + int v = nxt[0], w = nxt[1]; + if (dist[v] > dist[u] + w && dist[u] + w < disappear[v]) { + dist[v] = dist[u] + w; + pq.offer(new int[] {dist[v], v}); + } + } + } + int[] ans = new int[n]; + for (int i = 0; i < n; ++i) { + ans[i] = dist[i] < disappear[i] ? dist[i] : -1; + } + return ans; + } +} ``` ```cpp - +class Solution { +public: + vector minimumTime(int n, vector>& edges, vector& disappear) { + vector>> g(n); + for (const auto& e : edges) { + int u = e[0], v = e[1], w = e[2]; + g[u].push_back({v, w}); + g[v].push_back({u, w}); + } + + vector dist(n, 1 << 30); + dist[0] = 0; + + using pii = pair; + priority_queue, greater> pq; + pq.push({0, 0}); + + while (!pq.empty()) { + auto [du, u] = pq.top(); + pq.pop(); + + if (du > dist[u]) { + continue; + } + + for (auto [v, w] : g[u]) { + if (dist[v] > dist[u] + w && dist[u] + w < disappear[v]) { + dist[v] = dist[u] + w; + pq.push({dist[v], v}); + } + } + } + + vector ans(n); + for (int i = 0; i < n; ++i) { + ans[i] = dist[i] < disappear[i] ? dist[i] : -1; + } + + return ans; + } +}; ``` ```go - +func minimumTime(n int, edges [][]int, disappear []int) []int { + g := make([][]pair, n) + for _, e := range edges { + u, v, w := e[0], e[1], e[2] + g[u] = append(g[u], pair{v, w}) + g[v] = append(g[v], pair{u, w}) + } + + dist := make([]int, n) + for i := range dist { + dist[i] = 1 << 30 + } + dist[0] = 0 + + pq := hp{{0, 0}} + + for len(pq) > 0 { + du, u := pq[0].dis, pq[0].u + heap.Pop(&pq) + + if du > dist[u] { + continue + } + + for _, nxt := range g[u] { + v, w := nxt.dis, nxt.u + if dist[v] > dist[u]+w && dist[u]+w < disappear[v] { + dist[v] = dist[u] + w + heap.Push(&pq, pair{dist[v], v}) + } + } + } + + ans := make([]int, n) + for i := 0; i < n; i++ { + if dist[i] < disappear[i] { + ans[i] = dist[i] + } else { + ans[i] = -1 + } + } + + return ans +} + +type pair struct{ dis, u int } +type hp []pair + +func (h hp) Len() int { return len(h) } +func (h hp) Less(i, j int) bool { return h[i].dis < h[j].dis } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(pair)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } ``` diff --git a/solution/3100-3199/3112.Minimum Time to Visit Disappearing Nodes/Solution.cpp b/solution/3100-3199/3112.Minimum Time to Visit Disappearing Nodes/Solution.cpp new file mode 100644 index 0000000000000..b38aa7cb2bdc7 --- /dev/null +++ b/solution/3100-3199/3112.Minimum Time to Visit Disappearing Nodes/Solution.cpp @@ -0,0 +1,41 @@ +class Solution { +public: + vector minimumTime(int n, vector>& edges, vector& disappear) { + vector>> g(n); + for (const auto& e : edges) { + int u = e[0], v = e[1], w = e[2]; + g[u].push_back({v, w}); + g[v].push_back({u, w}); + } + + vector dist(n, 1 << 30); + dist[0] = 0; + + using pii = pair; + priority_queue, greater> pq; + pq.push({0, 0}); + + while (!pq.empty()) { + auto [du, u] = pq.top(); + pq.pop(); + + if (du > dist[u]) { + continue; + } + + for (auto [v, w] : g[u]) { + if (dist[v] > dist[u] + w && dist[u] + w < disappear[v]) { + dist[v] = dist[u] + w; + pq.push({dist[v], v}); + } + } + } + + vector ans(n); + for (int i = 0; i < n; ++i) { + ans[i] = dist[i] < disappear[i] ? dist[i] : -1; + } + + return ans; + } +}; \ No newline at end of file diff --git a/solution/3100-3199/3112.Minimum Time to Visit Disappearing Nodes/Solution.go b/solution/3100-3199/3112.Minimum Time to Visit Disappearing Nodes/Solution.go new file mode 100644 index 0000000000000..d1b413014fad8 --- /dev/null +++ b/solution/3100-3199/3112.Minimum Time to Visit Disappearing Nodes/Solution.go @@ -0,0 +1,53 @@ +func minimumTime(n int, edges [][]int, disappear []int) []int { + g := make([][]pair, n) + for _, e := range edges { + u, v, w := e[0], e[1], e[2] + g[u] = append(g[u], pair{v, w}) + g[v] = append(g[v], pair{u, w}) + } + + dist := make([]int, n) + for i := range dist { + dist[i] = 1 << 30 + } + dist[0] = 0 + + pq := hp{{0, 0}} + + for len(pq) > 0 { + du, u := pq[0].dis, pq[0].u + heap.Pop(&pq) + + if du > dist[u] { + continue + } + + for _, nxt := range g[u] { + v, w := nxt.dis, nxt.u + if dist[v] > dist[u]+w && dist[u]+w < disappear[v] { + dist[v] = dist[u] + w + heap.Push(&pq, pair{dist[v], v}) + } + } + } + + ans := make([]int, n) + for i := 0; i < n; i++ { + if dist[i] < disappear[i] { + ans[i] = dist[i] + } else { + ans[i] = -1 + } + } + + return ans +} + +type pair struct{ dis, u int } +type hp []pair + +func (h hp) Len() int { return len(h) } +func (h hp) Less(i, j int) bool { return h[i].dis < h[j].dis } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(pair)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } \ No newline at end of file diff --git a/solution/3100-3199/3112.Minimum Time to Visit Disappearing Nodes/Solution.java b/solution/3100-3199/3112.Minimum Time to Visit Disappearing Nodes/Solution.java new file mode 100644 index 0000000000000..d6fcaee1bed4c --- /dev/null +++ b/solution/3100-3199/3112.Minimum Time to Visit Disappearing Nodes/Solution.java @@ -0,0 +1,35 @@ +class Solution { + public int[] minimumTime(int n, int[][] edges, int[] disappear) { + List[] g = new List[n]; + Arrays.setAll(g, k -> new ArrayList<>()); + for (var e : edges) { + int u = e[0], v = e[1], w = e[2]; + g[u].add(new int[] {v, w}); + g[v].add(new int[] {u, w}); + } + int[] dist = new int[n]; + Arrays.fill(dist, 1 << 30); + dist[0] = 0; + PriorityQueue pq = new PriorityQueue<>((a, b) -> a[0] - b[0]); + pq.offer(new int[] {0, 0}); + while (!pq.isEmpty()) { + var e = pq.poll(); + int du = e[0], u = e[1]; + if (du > dist[u]) { + continue; + } + for (var nxt : g[u]) { + int v = nxt[0], w = nxt[1]; + if (dist[v] > dist[u] + w && dist[u] + w < disappear[v]) { + dist[v] = dist[u] + w; + pq.offer(new int[] {dist[v], v}); + } + } + } + int[] ans = new int[n]; + for (int i = 0; i < n; ++i) { + ans[i] = dist[i] < disappear[i] ? dist[i] : -1; + } + return ans; + } +} \ No newline at end of file diff --git a/solution/3100-3199/3112.Minimum Time to Visit Disappearing Nodes/Solution.py b/solution/3100-3199/3112.Minimum Time to Visit Disappearing Nodes/Solution.py new file mode 100644 index 0000000000000..8695ae255103f --- /dev/null +++ b/solution/3100-3199/3112.Minimum Time to Visit Disappearing Nodes/Solution.py @@ -0,0 +1,20 @@ +class Solution: + def minimumTime( + self, n: int, edges: List[List[int]], disappear: List[int] + ) -> List[int]: + g = defaultdict(list) + for u, v, w in edges: + g[u].append((v, w)) + g[v].append((u, w)) + dist = [inf] * n + dist[0] = 0 + q = [(0, 0)] + while q: + du, u = heappop(q) + if du > dist[u]: + continue + for v, w in g[u]: + if dist[v] > dist[u] + w and dist[u] + w < disappear[v]: + dist[v] = dist[u] + w + heappush(q, (dist[v], v)) + return [a if a < b else -1 for a, b in zip(dist, disappear)]