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.
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.
Given an integer n
and a 2D integer array paths
where paths[i]
is an integer array representing the path of the ith
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.
A subpath of a path is a contiguous sequence of cities within that path.
Example 1:
Input: n = 5, paths = [[0,1,2,3,4], [2,3,4], [4,0,1,2,3]] Output: 2 Explanation: The longest common subpath is [2,3].
Example 2:
Input: n = 3, paths = [[0],[1],[2]] Output: 0 Explanation: There is no common subpath shared by the three paths.
Example 3:
Input: n = 5, paths = [[0,1,2,3,4], [4,3,2,1,0]] Output: 1 Explanation: The possible longest common subpaths are [0], [1], [2], [3], and [4]. All have a length of 1.
Constraints:
1 <= n <= 105
m == paths.length
2 <= m <= 105
sum(paths[i].length) <= 105
0 <= paths[i][j] < n
- The same city is not listed multiple times consecutively in
paths[i]
.
Similar Questions:
// OJ: https://leetcode.com/problems/longest-common-subpath/
// Author: github.com/lzl124631x
// Time: O(N + logM * P) where P is the total length of all paths.
// Space: O(M)
class Solution {
bool valid(vector<vector<int>> &A, int len) {
unordered_map<unsigned long long, int> m;
unordered_set<unsigned long long> s; // Note that we must use `unsigned long long` to reduce the chance of hash conflict
unsigned d = 16777619;
for (auto &v : A) {
s.clear();
unsigned long long h = 0, p = 1;
for (int i = 0; i < v.size(); ++i) {
h = h * d + v[i];
if (i >= len) h -= v[i - len] * p;
else p *= d;
if (i >= len - 1) s.insert(h);
}
for (auto x : s) {
if (++m[x] == A.size()) return true;
}
}
return false;
}
public:
int longestCommonSubpath(int n, vector<vector<int>>& A) {
int minLen = INT_MAX;
for (auto &v : A) minLen = min(minLen, (int)v.size());
int L = 0, R = minLen;
while (L < R) {
int M = (L + R + 1) / 2;
if (valid(A, M)) L = M;
else R = M - 1;
}
return L;
}
};
Or
// OJ: https://leetcode.com/problems/longest-common-subpath/
// Author: github.com/lzl124631x
// Time: O(N + logM * P) where P is the total length of all paths.
// Space: O(M)
class Solution {
bool valid(vector<vector<int>> &A, int len) {
unordered_set<unsigned long long> s, tmp;
for (int i = 0; i < A.size() && (i == 0 || s.size()); ++i) {
unsigned long long d = 1099511628211, h = 0, p = 1;
tmp.clear();
swap(s, tmp);
for (int j = 0; j < A[i].size(); ++j) {
h = h * d + A[i][j];
if (j < len) p *= d;
else h -= A[i][j - len] * p;
if (j >= len - 1 && (i == 0 || tmp.count(h))) s.insert(h);
}
}
return s.size();
}
public:
int longestCommonSubpath(int n, vector<vector<int>>& A) {
int L = 0, R = min_element(begin(A), end(A), [](auto &a, auto &b) { return a.size() < b.size(); })->size();
while (L < R) {
int M = (L + R + 1) / 2;
if (valid(A, M)) L = M;
else R = M - 1;
}
return L;
}
};