|
| 1 | +/** |
| 2 | + * [1923] Longest Common Subpath |
| 3 | + * |
| 4 | + * There is a country of n cities numbered from 0 to n - 1. In this country, there is a road connecting every pair of cities. |
| 5 | + * There are m friends numbered from 0 to m - 1 who are traveling through the country. Each one of them will take a path consisting of some cities. Each path is represented by an integer array that contains the visited cities in order. The path may contain a city more than once, but the same city will not be listed consecutively. |
| 6 | + * Given an integer n and a 2D integer array paths where paths[i] is an integer array representing the path of the i^th friend, return the length of the longest common subpath that is shared by every friend's path, or 0 if there is no common subpath at all. |
| 7 | + * A subpath of a path is a contiguous sequence of cities within that path. |
| 8 | + * |
| 9 | + * Example 1: |
| 10 | + * |
| 11 | + * Input: n = 5, paths = [[0,1,<u>2,3</u>,4], |
| 12 | + * [<u>2,3</u>,4], |
| 13 | + * [4,0,1,<u>2,3</u>]] |
| 14 | + * Output: 2 |
| 15 | + * Explanation: The longest common subpath is [2,3]. |
| 16 | + * |
| 17 | + * Example 2: |
| 18 | + * |
| 19 | + * Input: n = 3, paths = [[0],[1],[2]] |
| 20 | + * Output: 0 |
| 21 | + * Explanation: There is no common subpath shared by the three paths. |
| 22 | + * |
| 23 | + * Example 3: |
| 24 | + * |
| 25 | + * Input: n = 5, paths = [[<u>0</u>,1,2,3,4], |
| 26 | + * [4,3,2,1,<u>0</u>]] |
| 27 | + * Output: 1 |
| 28 | + * Explanation: The possible longest common subpaths are [0], [1], [2], [3], and [4]. All have a length of 1. |
| 29 | + * |
| 30 | + * Constraints: |
| 31 | + * |
| 32 | + * 1 <= n <= 10^5 |
| 33 | + * m == paths.length |
| 34 | + * 2 <= m <= 10^5 |
| 35 | + * sum(paths[i].length) <= 10^5 |
| 36 | + * 0 <= paths[i][j] < n |
| 37 | + * The same city is not listed multiple times consecutively in paths[i]. |
| 38 | + * |
| 39 | + */ |
| 40 | +pub struct Solution {} |
| 41 | + |
| 42 | +// problem: https://leetcode.com/problems/longest-common-subpath/ |
| 43 | +// discuss: https://leetcode.com/problems/longest-common-subpath/discuss/?currentPage=1&orderBy=most_votes&query= |
| 44 | + |
| 45 | +// submission codes start here |
| 46 | + |
| 47 | +impl Solution { |
| 48 | + pub fn longest_common_subpath(n: i32, paths: Vec<Vec<i32>>) -> i32 { |
| 49 | + 0 |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +// submission codes end |
| 54 | + |
| 55 | +#[cfg(test)] |
| 56 | +mod tests { |
| 57 | + use super::*; |
| 58 | + |
| 59 | + #[test] |
| 60 | + #[ignore] |
| 61 | + fn test_1923_example_1() { |
| 62 | + let n = 5; |
| 63 | + let paths = vec![vec![0, 1, 2, 3, 4], vec![2, 3, 4], vec![4, 0, 1, 2, 3]]; |
| 64 | + |
| 65 | + let result = 2; |
| 66 | + |
| 67 | + assert_eq!(Solution::longest_common_subpath(n, paths), result); |
| 68 | + } |
| 69 | + |
| 70 | + #[test] |
| 71 | + #[ignore] |
| 72 | + fn test_1923_example_2() { |
| 73 | + let n = 3; |
| 74 | + let paths = vec![vec![0], vec![1], vec![2]]; |
| 75 | + |
| 76 | + let result = 0; |
| 77 | + |
| 78 | + assert_eq!(Solution::longest_common_subpath(n, paths), result); |
| 79 | + } |
| 80 | + |
| 81 | + #[test] |
| 82 | + #[ignore] |
| 83 | + fn test_1923_example_3() { |
| 84 | + let n = 5; |
| 85 | + let paths = vec![vec![0, 1, 2, 3, 4], vec![4, 3, 2, 1]]; |
| 86 | + |
| 87 | + let result = 1; |
| 88 | + |
| 89 | + assert_eq!(Solution::longest_common_subpath(n, paths), result); |
| 90 | + } |
| 91 | +} |
0 commit comments