Skip to content

Commit 17a9d2c

Browse files
committed
+ problem 1368
1 parent 6c47f26 commit 17a9d2c

File tree

5 files changed

+239
-0
lines changed

5 files changed

+239
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# 1368. Minimum Cost to Make at Least One Valid Path in a Grid
2+
Given an `m x n` grid. Each cell of the grid has a sign pointing to the next cell you should visit if you are currently in this cell. The sign of `grid[i][j]` can be:
3+
4+
* `1` which means go to the cell to the right. (i.e go from `grid[i][j]` to `grid[i][j + 1]`)
5+
* `2` which means go to the cell to the left. (i.e go from `grid[i][j]` to `grid[i][j - 1]`)
6+
* `3` which means go to the lower cell. (i.e go from `grid[i][j]` to `grid[i + 1][j]`)
7+
* `4` which means go to the upper cell. (i.e go from `grid[i][j]` to `grid[i - 1][j]`)
8+
9+
Notice that there could be some signs on the cells of the grid that point outside the grid.
10+
11+
You will initially start at the upper left cell `(0, 0)`. A valid path in the grid is a path that starts from the upper left cell `(0, 0)` and ends at the bottom-right cell `(m - 1, n - 1)` following the signs on the grid. The valid path does not have to be the shortest.
12+
13+
You can modify the sign on a cell with `cost = 1`. You can modify the sign on a cell **one time only**.
14+
15+
Return *the minimum cost to make the grid have at least one valid path*.
16+
17+
#### Example 1:
18+
![](https://assets.leetcode.com/uploads/2020/02/13/grid1.png)
19+
<pre>
20+
<strong>Input:</strong> grid = [[1,1,1,1],[2,2,2,2],[1,1,1,1],[2,2,2,2]]
21+
<strong>Output:</strong> 3
22+
<strong>Explanation:</strong> You will start at point (0, 0).
23+
The path to (3, 3) is as follows. (0, 0) --> (0, 1) --> (0, 2) --> (0, 3) change the arrow to down with cost = 1 --> (1, 3) --> (1, 2) --> (1, 1) --> (1, 0) change the arrow to down with cost = 1 --> (2, 0) --> (2, 1) --> (2, 2) --> (2, 3) change the arrow to down with cost = 1 --> (3, 3)
24+
The total cost = 3.
25+
</pre>
26+
27+
#### Example 2:
28+
![](https://assets.leetcode.com/uploads/2020/02/13/grid2.png)
29+
<pre>
30+
<strong>Input:</strong> grid = [[1,1,3],[3,2,2],[1,1,4]]
31+
<strong>Output:</strong> 0
32+
<strong>Explanation:</strong> You can follow the path from (0, 0) to (2, 2).
33+
</pre>
34+
35+
#### Example 3:
36+
![](https://assets.leetcode.com/uploads/2020/02/13/grid3.png)
37+
<pre>
38+
<strong>Input:</strong> grid = [[1,2],[4,3]]
39+
<strong>Output:</strong> 1
40+
</pre>
41+
42+
#### Constraints:
43+
* `m == grid.length`
44+
* `n == grid[i].length`
45+
* `1 <= m, n <= 100`
46+
* `1 <= grid[i][j] <= 4`
47+
48+
## Solutions (Rust)
49+
50+
### 1. Solution
51+
```Rust
52+
impl Solution {
53+
pub fn min_cost(grid: Vec<Vec<i32>>) -> i32 {
54+
use std::collections::BinaryHeap;
55+
use std::collections::HashSet;
56+
57+
let m = grid.len();
58+
let n = grid[0].len();
59+
let mut seen = HashSet::new();
60+
let mut heap = BinaryHeap::from([(0, 0, 0)]);
61+
62+
while let Some((cost, i0, j0)) = heap.pop() {
63+
if i0 < 0 || j0 < 0 || i0 >= m || j0 >= n {
64+
continue;
65+
}
66+
67+
if i0 == m - 1 && j0 == n - 1 {
68+
return -cost;
69+
}
70+
71+
seen.insert((i0, j0));
72+
73+
for (i1, j1, g) in [
74+
(i0, j0 + 1, 1),
75+
(i0, j0 - 1, 2),
76+
(i0 + 1, j0, 3),
77+
(i0 - 1, j0, 4),
78+
] {
79+
if !seen.contains(&(i1, j1)) {
80+
if g == grid[i0 as usize][j0 as usize] {
81+
heap.push((cost, i1, j1));
82+
} else {
83+
heap.push((cost - 1, i1, j1));
84+
}
85+
}
86+
}
87+
}
88+
89+
unreachable!()
90+
}
91+
}
92+
```
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# 1368. 使网格图至少有一条有效路径的最小代价
2+
给你一个 m x n 的网格图 `grid``grid` 中每个格子都有一个数字,对应着从该格子出发下一步走的方向。 `grid[i][j]` 中的数字可能为以下几种情况:
3+
4+
* **1** ,下一步往右走,也就是你会从 `grid[i][j]` 走到 `grid[i][j + 1]`
5+
* **2** ,下一步往左走,也就是你会从 `grid[i][j]` 走到 `grid[i][j - 1]`
6+
* **3** ,下一步往下走,也就是你会从 `grid[i][j]` 走到 `grid[i + 1][j]`
7+
* **4** ,下一步往上走,也就是你会从 `grid[i][j]` 走到 `grid[i - 1][j]`
8+
9+
注意网格图中可能会有 **无效数字** ,因为它们可能指向 `grid` 以外的区域。
10+
11+
一开始,你会从最左上角的格子 `(0,0)` 出发。我们定义一条 **有效路径** 为从格子 `(0,0)` 出发,每一步都顺着数字对应方向走,最终在最右下角的格子 `(m - 1, n - 1)` 结束的路径。有效路径 **不需要是最短路径**
12+
13+
你可以花费 `cost = 1` 的代价修改一个格子中的数字,但每个格子中的数字 **只能修改一次**
14+
15+
请你返回让网格图至少有一条有效路径的最小代价。
16+
17+
#### 示例 1:
18+
![](https://assets.leetcode.com/uploads/2020/02/13/grid1.png)
19+
<pre>
20+
<strong>输入:</strong> grid = [[1,1,1,1],[2,2,2,2],[1,1,1,1],[2,2,2,2]]
21+
<strong>输出:</strong> 3
22+
<strong>解释:</strong> You will start at point (0, 0).
23+
The path to (3, 3) is as follows. (0, 0) --> (0, 1) --> (0, 2) --> (0, 3) change the arrow to down with cost = 1 --> (1, 3) --> (1, 2) --> (1, 1) --> (1, 0) change the arrow to down with cost = 1 --> (2, 0) --> (2, 1) --> (2, 2) --> (2, 3) change the arrow to down with cost = 1 --> (3, 3)
24+
The total cost = 3.
25+
</pre>
26+
27+
#### 示例 2:
28+
![](https://assets.leetcode.com/uploads/2020/02/13/grid2.png)
29+
<pre>
30+
<strong>输入:</strong> grid = [[1,1,3],[3,2,2],[1,1,4]]
31+
<strong>输出:</strong> 0
32+
<strong>解释:</strong> You can follow the path from (0, 0) to (2, 2).
33+
</pre>
34+
35+
#### 示例 3:
36+
![](https://assets.leetcode.com/uploads/2020/02/13/grid3.png)
37+
<pre>
38+
<strong>输入:</strong> grid = [[1,2],[4,3]]
39+
<strong>输出:</strong> 1
40+
41+
#### 示例 4:
42+
<pre>
43+
<strong>输入:</strong> grid = [[2,2,2],[2,2,2]]
44+
<strong>输出:</strong> 3
45+
46+
#### 示例 5:
47+
<pre>
48+
<strong>输入:</strong> grid = [[4]]
49+
<strong>输出:</strong> 0
50+
</pre>
51+
52+
#### 提示:
53+
* `m == grid.length`
54+
* `n == grid[i].length`
55+
* `1 <= m, n <= 100`
56+
57+
## 题解 (Rust)
58+
59+
### 1. 题解
60+
```Rust
61+
impl Solution {
62+
pub fn min_cost(grid: Vec<Vec<i32>>) -> i32 {
63+
use std::collections::BinaryHeap;
64+
use std::collections::HashSet;
65+
66+
let m = grid.len();
67+
let n = grid[0].len();
68+
let mut seen = HashSet::new();
69+
let mut heap = BinaryHeap::from([(0, 0, 0)]);
70+
71+
while let Some((cost, i0, j0)) = heap.pop() {
72+
if i0 < 0 || j0 < 0 || i0 >= m || j0 >= n {
73+
continue;
74+
}
75+
76+
if i0 == m - 1 && j0 == n - 1 {
77+
return -cost;
78+
}
79+
80+
seen.insert((i0, j0));
81+
82+
for (i1, j1, g) in [
83+
(i0, j0 + 1, 1),
84+
(i0, j0 - 1, 2),
85+
(i0 + 1, j0, 3),
86+
(i0 - 1, j0, 4),
87+
] {
88+
if !seen.contains(&(i1, j1)) {
89+
if g == grid[i0 as usize][j0 as usize] {
90+
heap.push((cost, i1, j1));
91+
} else {
92+
heap.push((cost - 1, i1, j1));
93+
}
94+
}
95+
}
96+
}
97+
98+
unreachable!()
99+
}
100+
}
101+
```
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
impl Solution {
2+
pub fn min_cost(grid: Vec<Vec<i32>>) -> i32 {
3+
use std::collections::BinaryHeap;
4+
use std::collections::HashSet;
5+
6+
let m = grid.len();
7+
let n = grid[0].len();
8+
let mut seen = HashSet::new();
9+
let mut heap = BinaryHeap::from([(0, 0, 0)]);
10+
11+
while let Some((cost, i0, j0)) = heap.pop() {
12+
if i0 < 0 || j0 < 0 || i0 >= m || j0 >= n {
13+
continue;
14+
}
15+
16+
if i0 == m - 1 && j0 == n - 1 {
17+
return -cost;
18+
}
19+
20+
seen.insert((i0, j0));
21+
22+
for (i1, j1, g) in [
23+
(i0, j0 + 1, 1),
24+
(i0, j0 - 1, 2),
25+
(i0 + 1, j0, 3),
26+
(i0 - 1, j0, 4),
27+
] {
28+
if !seen.contains(&(i1, j1)) {
29+
if g == grid[i0 as usize][j0 as usize] {
30+
heap.push((cost, i1, j1));
31+
} else {
32+
heap.push((cost - 1, i1, j1));
33+
}
34+
}
35+
}
36+
}
37+
38+
unreachable!()
39+
}
40+
}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,7 @@
704704
[1365][1365l]|[How Many Numbers Are Smaller Than the Current Number][1365] |![js]
705705
[1366][1366l]|[Rank Teams by Votes][1366] |![rs]
706706
[1367][1367l]|[Linked List in Binary Tree][1367] |![py]&nbsp;&nbsp;![rb]
707+
[1368][1368l]|[Minimum Cost to Make at Least One Valid Path in a Grid][1368] |![rs]
707708
[1370][1370l]|[Increasing Decreasing String][1370] |![rs]
708709
[1371][1371l]|[Find the Longest Substring Containing Vowels in Even Counts][1371] |![rs]
709710
[1372][1372l]|[Longest ZigZag Path in a Binary Tree][1372] |![py]
@@ -1950,6 +1951,7 @@
19501951
[1365]:Problemset/1365-How%20Many%20Numbers%20Are%20Smaller%20Than%20the%20Current%20Number/README.md#1365-how-many-numbers-are-smaller-than-the-current-number
19511952
[1366]:Problemset/1366-Rank%20Teams%20by%20Votes/README.md#1366-rank-teams-by-votes
19521953
[1367]:Problemset/1367-Linked%20List%20in%20Binary%20Tree/README.md#1367-linked-list-in-binary-tree
1954+
[1368]:Problemset/1368-Minimum%20Cost%20to%20Make%20at%20Least%20One%20Valid%20Path%20in%20a%20Grid/README.md#1368-minimum-cost-to-make-at-least-one-valid-path-in-a-grid
19531955
[1370]:Problemset/1370-Increasing%20Decreasing%20String/README.md#1370-increasing-decreasing-string
19541956
[1371]:Problemset/1371-Find%20the%20Longest%20Substring%20Containing%20Vowels%20in%20Even%20Counts/README.md#1371-find-the-longest-substring-containing-vowels-in-even-counts
19551957
[1372]:Problemset/1372-Longest%20ZigZag%20Path%20in%20a%20Binary%20Tree/README.md#1372-longest-zigzag-path-in-a-binary-tree
@@ -3199,6 +3201,7 @@
31993201
[1365l]:https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/
32003202
[1366l]:https://leetcode.com/problems/rank-teams-by-votes/
32013203
[1367l]:https://leetcode.com/problems/linked-list-in-binary-tree/
3204+
[1368l]:https://leetcode.com/problems/minimum-cost-to-make-at-least-one-valid-path-in-a-grid/
32023205
[1370l]:https://leetcode.com/problems/increasing-decreasing-string/
32033206
[1371l]:https://leetcode.com/problems/find-the-longest-substring-containing-vowels-in-even-counts/
32043207
[1372l]:https://leetcode.com/problems/longest-zigzag-path-in-a-binary-tree/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,7 @@
704704
[1365][1365l]|[有多少小于当前数字的数字][1365] |![js]
705705
[1366][1366l]|[通过投票对团队排名][1366] |![rs]
706706
[1367][1367l]|[二叉树中的列表][1367] |![py]&nbsp;&nbsp;![rb]
707+
[1368][1368l]|[使网格图至少有一条有效路径的最小代价][1368] |![rs]
707708
[1370][1370l]|[上升下降字符串][1370] |![rs]
708709
[1371][1371l]|[每个元音包含偶数次的最长子字符串][1371] |![rs]
709710
[1372][1372l]|[二叉树中的最长交错路径][1372] |![py]
@@ -1950,6 +1951,7 @@
19501951
[1365]:Problemset/1365-How%20Many%20Numbers%20Are%20Smaller%20Than%20the%20Current%20Number/README_CN.md#1365-有多少小于当前数字的数字
19511952
[1366]:Problemset/1366-Rank%20Teams%20by%20Votes/README_CN.md#1366-通过投票对团队排名
19521953
[1367]:Problemset/1367-Linked%20List%20in%20Binary%20Tree/README_CN.md#1367-二叉树中的列表
1954+
[1368]:Problemset/1368-Minimum%20Cost%20to%20Make%20at%20Least%20One%20Valid%20Path%20in%20a%20Grid/README_CN.md#1368-使网格图至少有一条有效路径的最小代价
19531955
[1370]:Problemset/1370-Increasing%20Decreasing%20String/README_CN.md#1370-上升下降字符串
19541956
[1371]:Problemset/1371-Find%20the%20Longest%20Substring%20Containing%20Vowels%20in%20Even%20Counts/README_CN.md#1371-每个元音包含偶数次的最长子字符串
19551957
[1372]:Problemset/1372-Longest%20ZigZag%20Path%20in%20a%20Binary%20Tree/README_CN.md#1372-二叉树中的最长交错路径
@@ -3199,6 +3201,7 @@
31993201
[1365l]:https://leetcode.cn/problems/how-many-numbers-are-smaller-than-the-current-number/
32003202
[1366l]:https://leetcode.cn/problems/rank-teams-by-votes/
32013203
[1367l]:https://leetcode.cn/problems/linked-list-in-binary-tree/
3204+
[1368l]:https://leetcode.cn/problems/minimum-cost-to-make-at-least-one-valid-path-in-a-grid/
32023205
[1370l]:https://leetcode.cn/problems/increasing-decreasing-string/
32033206
[1371l]:https://leetcode.cn/problems/find-the-longest-substring-containing-vowels-in-even-counts/
32043207
[1372l]:https://leetcode.cn/problems/longest-zigzag-path-in-a-binary-tree/

0 commit comments

Comments
 (0)