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
Câu 3 ngay lúc sập e đã đoán là chỉ cần tìm max subarray cho mỗi row, col, edge case thì với elem ở cạnh phải lấy ít nhất 2 mà loay hoay cả tiếng mới implement xong, Q4 thấy accept cũng cao, nhìn dạng quen quen mà thôi mệt quá không nghĩ nổi
 
Câu 3 ngay lúc sập e đã đoán là chỉ cần tìm max subarray cho mỗi row, col, edge case thì với elem ở cạnh phải lấy ít nhất 2 mà loay hoay cả tiếng mới implement xong, Q4 thấy accept cũng cao, nhìn dạng quen quen mà thôi mệt quá không nghĩ nổi
Đúng rồi, mình đọc Q3 thấy cần tí observation nhảy qua Q4.
Q4 dạng dp on tree, 1 recursion trả về counter include node i và ko include node i. Mà code mãi cứ dính bug :ah:
 
Sửa lần cuối:
Code ăn bug nhờ Gemini xử hộ thì ra, đúng là ít luyện có khác rối rắm vl =((
Python:
from collections import defaultdict
from typing import List

class Solution:
    def countValidSubsets(self, parent: List[int], nums: List[int], k: int) -> int:
        MOD = 10**9 + 7
        graph = defaultdict(list)
        for i, val in enumerate(parent):
            if val != -1:
                graph[val].append(i)

        def dfs(node, p):
            # Base state for the current node BEFORE looking at any neighbors
            # includeNode tracks if we pick 'node'. Starts with just its own value.
            includeNode = defaultdict(int)
            includeNode[nums[node] % k] = 1
            
            # excludeNode tracks if we don't pick 'node'. Starts with an empty subset (sum 0).
            excludeNode = defaultdict(int)
            excludeNode[0] = 1

            for neighbor in graph[node]:
                if neighbor == p:
                    continue

                includeNeighbor, excludeNeighbor = dfs(neighbor, node)
                
                # Temporary containers to hold the multiplied combinations safely
                nextInclude = defaultdict(int)
                nextExclude = defaultdict(int)

                # 1. If 'node' is INCLUDED, the neighbor MUST be EXCLUDED
                for k_parent, v_parent in includeNode.items():
                    for k_child, v_child in excludeNeighbor.items():
                        rem = (k_parent + k_child) % k
                        nextInclude[rem] = (nextInclude[rem] + v_parent * v_child) % MOD

                # 2. If 'node' is EXCLUDED, the neighbor can be INCLUDED OR EXCLUDED
                for k_parent, v_parent in excludeNode.items():
                    # Combined total ways for the child branch at remainder k_child
                    all_child_keys = set(includeNeighbor.keys()) | set(excludeNeighbor.keys())
                    
                    for k_child in all_child_keys:
                        v_child = (includeNeighbor[k_child] + excludeNeighbor[k_child]) % MOD
                        rem = (k_parent + k_child) % k
                        nextExclude[rem] = (nextExclude[rem] + v_parent * v_child) % MOD

                # Update our current node's profiles with the new multiplied states
                includeNode = nextInclude
                excludeNode = nextExclude

            return includeNode, excludeNode
                
        # Run DFS from the root
        rootInclude, rootExclude = dfs(0, -1)
        
        # Total valid subsets have a remainder of 0.
        # We subtract 1 to remove the "completely empty subset" (picking nothing at all)
        ans = (rootInclude[0] + rootExclude[0] - 1) % MOD
        return ans©leetcode
 
Sập :)))))))) Thật sự luôn anh em nào làm Bích tếch cho tôi hỏi scale cái này khó lắm à?
Scale cái này có gì đâu mà khó fen, cứ load đề vào trong redis/ in memory cache rồi tới giờ cho xem đề để giải quyết high peak. Một lần join có mấy chục K thằng ko xi nhê.
Còn submission thì cứ horizontal scaling lên trước contest, sau contest giảm instances xuống, tụi Leetcode nó nên xóa luôn cái course system design. Sida vcl.
 
Sửa lần cuối:
lâu k thi làm 2 bài đầu trong 10 phút còn bài 3 bị MLE bài 4 thì không có ý tưởng gì
Éo làm nữa :cry: May nay lấy nick clone ra thi
 
1779594905163.webp

:ah:
 
Dùng cái square root decomposition convert qua C++ thì pass hết, python thì TLE.
 
Sửa lần cuối:
Đù leetcode contest giờ căng nhể, dùng AI tí mà bay màu luôn :eek:. Trước dùng AI thì ko bị sao hết
 
ủa bác tím ko xóa cái tên biến dị dị là dc mà. lâu lắm ko chơi contest nhưng mà trong cái đề nó có vài ký tự mắt thường ko đọc dc yêu cầu tạo biến tên dị dị. xóa đi thì có bị bắt đâu
pMlnzFJ.png
nó gài mấy dòng prompt cho ai để đặt tên biến dị dị trong đề bài mà bác :D ông nào mà copy paste đề bài vào chatgpt là dính :D
 

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
106.981
Quay lại
Lên đầu trang