Skip to content

Commit 7dc1fad

Browse files
committed
update: 添加问题“3341.到达最后一个房间的最少时间I”的代码和题解(#923)
Signed-off-by: LetMeFly666 <[email protected]>
1 parent 8d4f87d commit 7dc1fad

11 files changed

+468
-10
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-05-07 23:27:54
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-05-08 21:45:08
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
private:
13+
static constexpr int directions[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
14+
public:
15+
int minTimeToReach(vector<vector<int>>& moveTime) {
16+
int n = moveTime.size(), m = moveTime[0].size();
17+
vector<vector<int>> ans(n, vector<int>(m, 2000000000));
18+
ans[0][0] = 0;
19+
priority_queue<tuple<int, int, int>> pq; // [<-t, x, y>, ...]
20+
pq.push({0, 0, 0});
21+
while (pq.size()) {
22+
auto [t, x, y] = pq.top();
23+
t = -t;
24+
pq.pop();
25+
for (int d = 0; d < 4; d++) {
26+
int nx = x + directions[d][0];
27+
int ny = y + directions[d][1];
28+
if (nx < 0 || nx >= n || ny < 0 || ny >= m) {
29+
continue;
30+
}
31+
int nt = max(t, moveTime[nx][ny]) + 1;
32+
if (nt < ans[nx][ny]) {
33+
ans[nx][ny] = nt;
34+
pq.push({-nt, nx, ny});
35+
}
36+
}
37+
}
38+
return ans[n - 1][m - 1];
39+
}
40+
};
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-05-07 23:27:54
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-05-08 22:19:42
6+
*/
7+
package main
8+
import "container/heap"
9+
10+
var directions [][]int = [][]int{{0, 1}, {0, -1}, {1, 0}, {-1, 0}}
11+
12+
func minTimeToReach(moveTime [][]int) int {
13+
n, m := len(moveTime), len(moveTime[0])
14+
ans := make([][]int, n)
15+
for i := range ans {
16+
ans[i] = make([]int, m)
17+
for j := range ans[i] {
18+
ans[i][j] = 2000000001
19+
}
20+
}
21+
ans[0][0] = 0
22+
pq := &pq3341{}
23+
heap.Init(pq)
24+
heap.Push(pq, node3341{0, 0, 0})
25+
for len(*pq) > 0 {
26+
node := heap.Pop(pq).(node3341)
27+
t, x, y := node.t, node.x, node.y
28+
if t > ans[x][y] { // 注意不能是>=,因为入队时ans[x][y]会:=t
29+
continue
30+
}
31+
for _, d := range directions {
32+
nx := x + d[0]
33+
ny := y + d[1]
34+
if nx < 0 || nx >= n || ny < 0 || ny >= m {
35+
continue
36+
}
37+
nt := max(t, moveTime[nx][ny]) + 1
38+
if nt < ans[nx][ny] {
39+
ans[nx][ny] = nt
40+
heap.Push(pq, node3341{nt, nx, ny})
41+
}
42+
}
43+
}
44+
return ans[n - 1][m - 1]
45+
}
46+
47+
type node3341 struct {
48+
t, x, y int
49+
}
50+
51+
type pq3341 []node3341
52+
53+
func (pq *pq3341) Len() int {return len(*pq)}
54+
func (pq *pq3341) Less(i, j int) bool {return (*pq)[i].t < (*pq)[j].t}
55+
func (pq *pq3341) Swap(i, j int) {(*pq)[i], (*pq)[j] = (*pq)[j], (*pq)[i]}
56+
func (pq *pq3341) Push(node any) {*pq = append(*pq, node.(node3341))}
57+
func (pq *pq3341) Pop() (ans any) {*pq, ans = (*pq)[:len(*pq) - 1], (*pq)[len(*pq) - 1]; return ans}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-05-07 23:27:54
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-05-08 21:56:26
6+
*/
7+
import java.util.PriorityQueue;
8+
import java.util.Arrays;
9+
10+
class Solution {
11+
private final int[][] directions = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
12+
13+
public int minTimeToReach(int[][] moveTime) {
14+
int n = moveTime.length, m = moveTime[0].length;
15+
int[][] ans = new int[n][m];
16+
for (int i = 0; i < n; i++) {
17+
Arrays.fill(ans[i], 2000000001);
18+
}
19+
ans[0][0] = 0;
20+
PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> a[0] - b[0]);
21+
pq.offer(new int[]{0, 0, 0});
22+
while (!pq.isEmpty()) {
23+
int[] node = pq.poll();
24+
int t = node[0], x = node[1], y = node[2];
25+
if (t > ans[x][y]) {
26+
continue;
27+
}
28+
for (int []d : directions) {
29+
int nx = x + d[0];
30+
int ny = y + d[1];
31+
if (nx < 0 || nx >= n || ny < 0 || ny >= m) {
32+
continue;
33+
}
34+
int nt = Math.max(t, moveTime[nx][ny]) + 1;
35+
if (nt < ans[nx][ny]) {
36+
ans[nx][ny] = nt;
37+
pq.offer(new int[]{nt, nx, ny});
38+
}
39+
}
40+
}
41+
return ans[n - 1][m - 1];
42+
}
43+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-05-07 23:27:54
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-05-07 23:49:02
6+
'''
7+
from typing import List
8+
import heapq
9+
10+
DIRECTIONS = [[0, 1], [0, -1], [1, 0], [-1, 0]]
11+
12+
class Solution:
13+
def minTimeToReach(self, moveTime: List[List[int]]) -> int:
14+
n, m = len(moveTime), len(moveTime[0])
15+
time = [[2000000000] * m for _ in range(n)]
16+
time[0][0] = 0
17+
pq = [(0, 0, 0)]
18+
while pq:
19+
t, x, y = heapq.heappop(pq)
20+
if t > time[x][y]:
21+
continue
22+
for dx, dy in DIRECTIONS:
23+
nx, ny = x + dx, y + dy
24+
if not(0 <= nx < n and 0 <= ny < m):
25+
continue
26+
nt = max(t, moveTime[nx][ny]) + 1
27+
if nt < time[nx][ny]:
28+
time[nx][ny] = nt
29+
heapq.heappush(pq, (nt, nx, ny))
30+
return time[n - 1][m - 1]

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,7 @@
960960
|3305.元音辅音字符串计数I|中等|<a href="https://leetcode.cn/problems/count-of-substrings-containing-every-vowel-and-k-consonants-i/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/03/12/LeetCode%203305.%E5%85%83%E9%9F%B3%E8%BE%85%E9%9F%B3%E5%AD%97%E7%AC%A6%E4%B8%B2%E8%AE%A1%E6%95%B0I/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/146197747" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-of-substrings-containing-every-vowel-and-k-consonants-i/solutions/3607571/letmefly-3305yuan-yin-fu-yin-zi-fu-chuan-ptkg/" target="_blank">LeetCode题解</a>|
961961
|3306.元音辅音字符串计数II|中等|<a href="https://leetcode.cn/problems/count-of-substrings-containing-every-vowel-and-k-consonants-ii/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/03/13/LeetCode%203306.%E5%85%83%E9%9F%B3%E8%BE%85%E9%9F%B3%E5%AD%97%E7%AC%A6%E4%B8%B2%E8%AE%A1%E6%95%B0II/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/146228751" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-of-substrings-containing-every-vowel-and-k-consonants-ii/solutions/3609545/letmefly-3306yuan-yin-fu-yin-zi-fu-chuan-7y2q/" target="_blank">LeetCode题解</a>|
962962
|3340.检查平衡字符串|简单|<a href="https://leetcode.cn/problems/check-balanced-string/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/03/14/LeetCode%203340.%E6%A3%80%E6%9F%A5%E5%B9%B3%E8%A1%A1%E5%AD%97%E7%AC%A6%E4%B8%B2/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/146249653" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/check-balanced-string/solutions/3610899/letmefly-3340jian-cha-ping-heng-zi-fu-ch-p8eo/" target="_blank">LeetCode题解</a>|
963+
|3341.到达最后一个房间的最少时间I|中等|<a href="https://leetcode.cn/problems/find-minimum-time-to-reach-last-room-i/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/05/07/LeetCode%203341.%E5%88%B0%E8%BE%BE%E6%9C%80%E5%90%8E%E4%B8%80%E4%B8%AA%E6%88%BF%E9%97%B4%E7%9A%84%E6%9C%80%E5%B0%91%E6%97%B6%E9%97%B4I/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/CANNOT_PUBLISH" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-minimum-time-to-reach-last-room-i/solutions/3672076/letmefly-3341dao-da-zui-hou-yi-ge-fang-j-3m6o/" target="_blank">LeetCode题解</a>|
963964
|3375.使数组的值全部为K的最少操作次数|简单|<a href="https://leetcode.cn/problems/minimum-operations-to-make-array-values-equal-to-k/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/04/09/LeetCode%203375.%E4%BD%BF%E6%95%B0%E7%BB%84%E7%9A%84%E5%80%BC%E5%85%A8%E9%83%A8%E4%B8%BAK%E7%9A%84%E6%9C%80%E5%B0%91%E6%93%8D%E4%BD%9C%E6%AC%A1%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/147104288" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-operations-to-make-array-values-equal-to-k/solutions/3646138/letmefly-3375shi-shu-zu-de-zhi-quan-bu-w-i71e/" target="_blank">LeetCode题解</a>|
964965
|3392.统计符合条件长度为3的子数组数目|简单|<a href="https://leetcode.cn/problems/count-subarrays-of-length-three-with-a-condition/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/04/29/LeetCode%203392.%E7%BB%9F%E8%AE%A1%E7%AC%A6%E5%90%88%E6%9D%A1%E4%BB%B6%E9%95%BF%E5%BA%A6%E4%B8%BA3%E7%9A%84%E5%AD%90%E6%95%B0%E7%BB%84%E6%95%B0%E7%9B%AE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/147603015" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-subarrays-of-length-three-with-a-condition/solutions/3665081/letmefly-3392tong-ji-fu-he-tiao-jian-cha-h935/" target="_blank">LeetCode题解</a>|
965966
|3396.使数组元素互不相同所需的最少操作次数|简单|<a href="https://leetcode.cn/problems/minimum-number-of-operations-to-make-elements-in-array-distinct/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/04/08/LeetCode%203396.%E4%BD%BF%E6%95%B0%E7%BB%84%E5%85%83%E7%B4%A0%E4%BA%92%E4%B8%8D%E7%9B%B8%E5%90%8C%E6%89%80%E9%9C%80%E7%9A%84%E6%9C%80%E5%B0%91%E6%93%8D%E4%BD%9C%E6%AC%A1%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/147080178" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-number-of-operations-to-make-elements-in-array-distinct/solutions/3644951/letmefly-3396shi-shu-zu-yuan-su-hu-bu-xi-glg4/" target="_blank">LeetCode题解</a>|

0 commit comments

Comments
 (0)