classSolution { public: intmaxDistance(vector<int>& nums1, vector<int>& nums2){ int ans = 0; for (int i = 0; i < nums1.size(); ++i) { auto it = upper_bound(nums2.begin() + i, nums2.end(), nums1[i], greater<>()); ans = max(ans, static_cast<int>(distance(nums2.begin() + i, prev(it)))); } return ans; } };
classSolution: deflargestPathValue(self, colors: str, edges: List[List[int]]) -> int: n = len(colors) m = len(edges) indegree = [0] * n graph = defaultdict(list) for e in edges: indegree[e[1]] += 1 graph[e[0]].append(e[1]) # topo sort defdfs(root): ans = [0] * 26 ans[ord(colors[root]) - ord('a')] += 1 for n in graph[root]: if n in path: returnNone x = dfs(n, path) if x isNone: returnNone for i inrange(26): ans[i] += x[i] return ans ans = -1 q = collections.deque() seen = 0 cnt = [[0] * 26for i inrange (n)] for i inrange(n): if indegree[i] == 0: cnt[i][ord(colors[i]) - ord('a')] += 1 q.append(i) seen += 1 ans = max(ans, max(cnt[i])) while q: front = q.popleft() for i in graph[front]: for x inrange(26): cnt[i][x] = max(cnt[i][x], cnt[front][x]) indegree[i] -= 1 if indegree[i] == 0: q.append(i) cnt[i][ord(colors[i]) - ord('a')] += 1 ans = max(ans, max(cnt[i])) seen += 1 if seen == n: return ans else: return -1