Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from typing import List


class Solution:
def pathExistenceQueries(
self, n: int, nums: List[int], maxDiff: int, queries: List[List[int]]
) -> List[bool]:
# nums is sorted non-decreasing. An edge exists between i and j when
# |nums[i] - nums[j]| <= maxDiff. Because values are sorted, any path
# must cross consecutive indices, so components are contiguous segments
# split wherever the gap between neighbors exceeds maxDiff.
component = [0] * n
cid = 0
for i in range(1, n):
if nums[i] - nums[i - 1] > maxDiff:
cid += 1
component[i] = cid

return [component[u] == component[v] for u, v in queries]