có khi xuống nữa, đọc dc vài a ấn xài AI giải ko thèm đổi biến rXem tệp đính kèm 3280987
mới update, lên 4x hehe
.Còn chả thèm xóa cmtq4 nhìn tưởng dễ xơi mà khoai nhỉ,
có khi xuống nữa, đọc dc vài a ấn xài AI giải ko thèm đổi biến r![]()

Daily task simulating mà bác :>dạng bài design data structure có vẻ hot ở mấy web OA tầm vài tháng trước, h leetcode update vào liên tục
share để tham khảo đc ko bácQ4 n khá nhỏ nên dùng graph để bipartiate theo distance, đm 4 bugs rank 1k4 cmnr![]()
chư làm kiểu gen hết dist ra bỏ vào min heap bóc ra chia vào 2 group cho tới khi tìm thấy 1 pair đã nằm cùng trong 1 group.tưởng như v là max của min dist r nhưng mà ko, con gpt nó giải thêm 30 dòng nữa mới rashare để tham khảo đc ko bác

share để tham khảo đc ko bác
class Solution:
def isBipartite(self, n: int, adj: dict) -> bool:
colors = [0] * n
for startNode in range(n):
if colors[startNode] == 0:
stack = [(startNode, 1)]
colors[startNode] = 1
while stack:
u, uColor = stack.pop()
for v in adj.get(u, []):
if colors[v] == 0:
colors[v] = -uColor
stack.append((v, -uColor))
elif colors[v] == uColor:
return False
return True
def maxPartitionFactor(self, points: List[List[int]]) -> int:
n = len(points)
if n <= 2:
return 0
allDistances = []
for i in range(n):
xi, yi = points[i]
for j in range(i + 1, n):
xj, yj = points[j]
distance = abs(xi - xj) + abs(yi - yj)
allDistances.append((distance, i, j))
allDistances.sort(key=lambda x: x[0])
uniqueDistances = sorted(list(set(d[0] for d in allDistances)))
maxFactor = uniqueDistances[0]
for i in range(1, len(uniqueDistances)):
thresholdD = uniqueDistances[i]
adj = defaultdict(list)
for dist, u, v in allDistances:
if dist < thresholdD:
adj[u].append(v)
adj[v].append(u)
else:
pass
if self.isBipartite(n, adj):
maxFactor = thresholdD
else:
break
return maxFactor
Lúc đầu ngu quá viết code greedy bằng cách dùng 1 line hoặc 1 point để split thành 2 groups nên ko ăn, sau mới nghĩ ra dùng bipartiate graph kiểm tra từng point, cuối giờ viết code ko kịp chôm code chỗ bipartiate có sẵn. Nhanh hơn thì phải dùng binary searchPython:class Solution: def isBipartite(self, n: int, adj: dict) -> bool: colors = [0] * n for startNode in range(n): if colors[startNode] == 0: stack = [(startNode, 1)] colors[startNode] = 1 while stack: u, uColor = stack.pop() for v in adj.get(u, []): if colors[v] == 0: colors[v] = -uColor stack.append((v, -uColor)) elif colors[v] == uColor: return False return True def maxPartitionFactor(self, points: List[List[int]]) -> int: n = len(points) if n <= 2: return 0 allDistances = [] for i in range(n): xi, yi = points[i] for j in range(i + 1, n): xj, yj = points[j] distance = abs(xi - xj) + abs(yi - yj) allDistances.append((distance, i, j)) allDistances.sort(key=lambda x: x[0]) uniqueDistances = sorted(list(set(d[0] for d in allDistances))) maxFactor = uniqueDistances[0] for i in range(1, len(uniqueDistances)): thresholdD = uniqueDistances[i] adj = defaultdict(list) for dist, u, v in allDistances: if dist < thresholdD: adj[u].append(v) adj[v].append(u) else: pass if self.isBipartite(n, adj): maxFactor = thresholdD else: break return maxFactor
Ý tưởng là vì min của 2 groups phải > 1 cái threshold nào đấy nên chỉ cần kiểm tra xem với đống distance mà < threshold nó bắt buộc phải bipartiate ở 2 group khác nhau là ok. Nếu nghĩ theo hướng binary search on the answer thì code đơn giản hơn nhiều rồi.
thanks bác, à h mới biết đc bipartiate là gìỪ q cuối phải reverse thinking mà nhìn thấy cái trick này thì dễ là đúng rồi. Khó nhất là cái trick này thôiThif e bảo đề nay dễ mà, hai câu array cơ bản 2 câu binarysearch, câu 3 thêm prefixsum còn câu 4 thêm tô màu graph (dfs/bfs) là xong
Chắc do có đợt em spam toàn làm binary searchỪ q cuối phải reverse thinking mà nhìn thấy cái trick này thì dễ là đúng rồi. Khó nhất là cái trick này thôi
))) nên dễ xử lý hơnbí kĩ bi search bác @anoldvozer1710.v2 đưa e năm ngoái có chia quyển hạ quyển thượng sao mà bi search này áp dụng lạ quáChắc do có đợt em spam toàn làm binary search))) nên dễ xử lý hơn