thảo luận Leetcode + Codeforces, Competitive programming contest. Đường tới Guardian + Candidate Master.

  • Người tạo chủ đề Người tạo chủ đề freedom.9
  • Ngày bắt đầu Ngày bắt đầu
1760196257444.png

phế điên =((
 
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
d5YSmCu.png
 
bài Q4 phải biết kiểu bipartite graph với binary search on value mới giải được nhỉ, mình học chưa tới nên đành chịu :beat_plaster:
 
:love: share để tham khảo đc ko bác
Python:
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 search
Ý 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.
 
Python:
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 search
Ý 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.
:sweet_kiss: thanks bác, à h mới biết đc bipartiate là gì
 
Thif 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
Câu 3 bsearch khá hiển nhiên còn câu 4 thì do nó là tìm max trong các min(hoặc min trong các max) thì cũng thường sẽ bsearch dựa vào giá trị hoy.
 

Thống kê chủ đề

Ngày tạo
freedom.9,
Người trả lời cuối
deple20k,
Trả lời
1.686
Lượt xem
107.188
Quay lại
Lên đầu trang