23.04.29 Today’s Leetcode
1697. Checking Existence of Edge Length Limited Paths (hard)
Union and Find Approach, it is weird to me.
class DSU:
def __init__(self, n):
self.parent = list(range(n))
self.rank = [0] * n
def find(self, x):
if self.parent[x] == x:
return x
self.parent[x] = self.find(self.parent[x])
return self.parent[x]
def union(self, x, y):
xset, yset = self.find(x), self.find(y)
if xset != yset:
if self.rank[xset] < self.rank[yset]:
self.parent[xset] = yset
else:
self.parent[yset] = xset
if self.rank[xset] == self.rank[yset]:
self.rank[xset] += 1
return True
return False
class Solution:
def distanceLimitedPathsExist(self, n: int, edgeList: List[List[int]], queries: List[List[int]]) -> List[bool]:
dsu = DSU(n)
for i, q in enumerate(queries):
queries[i].append(i)
queries.sort(key=lambda q: q[2])
edgeList.sort(key=lambda e: e[2])
i = 0
res = [False] * len(queries)
for q in queries:
while i < len(edgeList) and edgeList[i][2] < q[2]:
dsu.union(edgeList[i][0], edgeList[i][1])
i += 1
if dsu.find(q[0]) == dsu.find(q[1]):
res[q[3]] = True
return res
1738. Find Kth Largest XOR Coordinate Value (medium)
class Solution:
def kthLargestValue(self, matrix: List[List[int]], k: int) -> int:
m, n = len(matrix), len(matrix[0])
arr = []
for i in range(m):
for j in range(n):
if i > 0:
matrix[i][j] ^= matrix[i - 1][j]
if j > 0:
matrix[i][j] ^= matrix[i][j - 1];
if i > 0 and j > 0:
matrix[i][j] ^= matrix[i - 1][j - 1];
heappush(arr, -matrix[i][j])
res = 0
for i in range(k):
res = -heappop(arr)
return res