-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add unique_paths_with_obstacles
- Loading branch information
1 parent
0e7e349
commit c8f00df
Showing
4 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
dynamic-programing/medium/unique_paths_with_obstacles/Cargo.lock
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
9 changes: 9 additions & 0 deletions
9
dynamic-programing/medium/unique_paths_with_obstacles/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[package] | ||
name = "leetcodebyrust" | ||
version = "0.1.0" | ||
authors = ["zhangyuang <yuuang>"] | ||
edition = "2018" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] |
54 changes: 54 additions & 0 deletions
54
dynamic-programing/medium/unique_paths_with_obstacles/src/lib.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* @lc app=leetcode.cn id=63 lang=rust | ||
* | ||
* [63] 不同路径 II | ||
*/ | ||
struct Solution {} | ||
// @lc code=start | ||
impl Solution { | ||
pub fn unique_paths_with_obstacles(obstacle_grid: Vec<Vec<i32>>) -> i32 { | ||
let m = obstacle_grid.len(); | ||
let n = obstacle_grid[0].len(); | ||
let mut dp = vec![vec![1; n]; m]; | ||
// 特殊处理第一列的情况 | ||
let mut hasObstacle = false; | ||
for i in 0..n { | ||
if obstacle_grid[0][i] == 1 { | ||
hasObstacle = true | ||
} | ||
if hasObstacle { | ||
dp[0][i] = 0 | ||
} | ||
} | ||
hasObstacle = false; | ||
// 特殊处理第一行的情况 | ||
for i in 0..m { | ||
if obstacle_grid[i][0] == 1 { | ||
hasObstacle = true | ||
} | ||
if hasObstacle { | ||
dp[i][0] = 0 | ||
} | ||
} | ||
for i in 1..m { | ||
for j in 1..n { | ||
let top = if obstacle_grid[i - 1][j] == 1 { | ||
0 | ||
} else { | ||
dp[i - 1][j] | ||
}; | ||
let left = if obstacle_grid[i][j - 1] == 1 { | ||
0 | ||
} else { | ||
dp[i][j - 1] | ||
}; | ||
dp[i][j] = top + left; | ||
if obstacle_grid[i][j] == 1 { | ||
dp[i][j] = 0 | ||
} | ||
} | ||
} | ||
dp[m - 1][n - 1] | ||
} | ||
} | ||
// @lc code=end |