Skip to content

Commit

Permalink
feat: 重新规划路线
Browse files Browse the repository at this point in the history
  • Loading branch information
yi-ge committed Dec 7, 2023
1 parent e7ee45a commit 2ef7d19
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 5 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,10 @@ Rust标准库`std::collections`提供了4种通用容器类型,包含一下8

###

- [重新规划路线](src/graphs/reorder_routes_to_make_all_paths_lead_to_the_city_zero.rs) [深度优先搜索, 广度优先搜索, 图]

- LeetCode 1466. 重新规划路线 <https://leetcode.cn/problems/reorder-routes-to-make-all-paths-lead-to-the-city-zero>

- [最小化旅行的价格总和](src/graphs/minimize_the_total_price_of_the_trips.rs) [树, 深度优先搜索, 图, 数组, 动态规划]

- LeetCode 2646. 最小化旅行的价格总和 <https://leetcode.cn/problems/minimize-the-total-price-of-the-trips>
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
},
"dependencies": {
"lcov-parse": "^1.0.0",
"leetcode-test-helper": "^0.4.7",
"leetcode-test-helper": "^0.4.8",
"puppeteer": "^15.3.0",
"puppeteer-extra": "^3.3.1",
"puppeteer-extra-plugin-stealth": "^2.10.2",
Expand Down
1 change: 1 addition & 0 deletions src/graphs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ pub mod flower_planting_with_no_adjacent;
pub mod frog_position_after_t_seconds;
pub mod minimum_fuel_cost_to_report_to_the_capital;
pub mod minimize_the_total_price_of_the_trips;
pub mod reorder_routes_to_make_all_paths_lead_to_the_city_zero;
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// 重新规划路线
// https://leetcode.cn/problems/reorder-routes-to-make-all-paths-lead-to-the-city-zero
// INLINE ../../images/graphs/reorder_routes_to_make_all_paths_lead_to_the_city_zero.jpeg

pub struct Solution;

impl Solution {
fn dfs(node: i32, visited: &mut Vec<bool>, res: &mut i32, graph: &Vec<Vec<(i32, bool)>>) {
visited[node as usize] = true;
for (next, is_out) in &graph[node as usize] {
if !visited[*next as usize] {
if *is_out {
*res += 1;
}
Self::dfs(*next, visited, res, graph);
}
}
}
pub fn min_reorder(n: i32, connections: Vec<Vec<i32>>) -> i32 {
let mut res = 0;
let mut visited = vec![false; n as usize];
let mut graph = vec![vec![]; n as usize];
for connection in connections {
graph[connection[0] as usize].push((connection[1], true));
graph[connection[1] as usize].push((connection[0], false));
}
Self::dfs(0, &mut visited, &mut res, &graph);
res
}
}
1 change: 1 addition & 0 deletions tests/graphs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ pub mod flower_planting_with_no_adjacent_test;
pub mod frog_position_after_t_seconds_test;
pub mod minimum_fuel_cost_to_report_to_the_capital_test;
pub mod minimize_the_total_price_of_the_trips_test;
pub mod reorder_routes_to_make_all_paths_lead_to_the_city_zero_test;
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use rust_practice::graphs::reorder_routes_to_make_all_paths_lead_to_the_city_zero::Solution;

#[test]
fn min_reorder() {
// 示例 1:
// 输入:n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
// 输出:3
// 解释:更改以红色显示的路线的方向,使每个城市都可以到达城市 0 。
let n = 6;
let connections = vec![vec![0, 1], vec![1, 3], vec![2, 3], vec![4, 0], vec![4, 5]];
assert_eq!(Solution::min_reorder(n, connections), 3);

// 示例 2:
// 输入:n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
// 输出:2
// 解释:更改以红色显示的路线的方向,使每个城市都可以到达城市 0 。
let n = 5;
let connections = vec![vec![1, 0], vec![1, 2], vec![3, 2], vec![3, 4]];
assert_eq!(Solution::min_reorder(n, connections), 2);

// 示例 3:
// 输入:n = 3, connections = [[1,0],[2,0]]
// 输出:0
let n = 3;
let connections = vec![vec![1, 0], vec![2, 0]];
assert_eq!(Solution::min_reorder(n, connections), 0);
}
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -377,10 +377,10 @@ lcov-parse@^1.0.0:
resolved "https://registry.yarnpkg.com/lcov-parse/-/lcov-parse-1.0.0.tgz#eb0d46b54111ebc561acb4c408ef9363bdc8f7e0"
integrity sha512-aprLII/vPzuQvYZnDRU78Fns9I2Ag3gi4Ipga/hxnVMCZC8DnR2nI7XBqrPoywGfxqIx/DgarGvDJZAD3YBTgQ==

leetcode-test-helper@^0.4.7:
version "0.4.7"
resolved "https://registry.yarnpkg.com/leetcode-test-helper/-/leetcode-test-helper-0.4.7.tgz#7798cdf1c322f1ed452b8b26d56b769a5c732aed"
integrity sha512-ULRnWlvIMhduzAvUdxAh1moyrraOLsm5CmA0chYXZYo7OSfpnp176B6JDca+lFGvrPvRLicDzB+3rmi1uHz0xg==
leetcode-test-helper@^0.4.8:
version "0.4.8"
resolved "https://registry.yarnpkg.com/leetcode-test-helper/-/leetcode-test-helper-0.4.8.tgz#150213bc6ec96cf84117d5256272b0c61dcd3dae"
integrity sha512-AQp5cNKW9QQ0SqroUQ/zAeajIEiIIEMlebTfLK2n1pusb65GLse8IkqPDuzvwerQPZgVgn/hHndrVhjZ34xNvw==
dependencies:
playwright "^1.38.0"
playwright-extra "^4.3.6"
Expand Down

0 comments on commit 2ef7d19

Please sign in to comment.