如果问最短,最少,BFS 如果问连通性,静态就是 DFS,BFS,动态就 UF 如果问依赖性就 topo sort DAG 的问题就 dfs+memo 矩阵和 Array 通常都是 DP 问数量的通常都是 DP 问是否可以,也很有可能 DP 求所有解的,基本 backtracking 排序总是可以想一想的 万事总可以想HashMap 找规律试试Stack

Link 1 Link 2

def adjust(salaries: list[float], target: float) -> list[float]:
    n = len(salaries)
    salaries.sort()
    adjustment = salaries[0]
    for i, salary in enumerate(salaries):
        if (n - i) * salary > target:
            adjustment = target / (n - i)
            break
        else:
            adjustment = salary
            target -= salary
    
    return adjustment

adjust([100, 300, 200, 400], 800)
adjust([100, 300, 200, 400], 320)

Link 1

def _strip(s):
    n = len(s)
    l = 0
    while l < n and s[l] == " ": l += 1
    r = n - 1
    while r >= 0 and s[r] == " ": r -= 1
    return s[l:r+1] if r >= l else ""

def split(s):
    result = []
    l = 0
    quote = 0
    s = _strip(s)
    for r, char in enumerate(s + " "):
        if char == " ":
            if quote: continue
            section = _strip(s[l:r])
            l = r
            if section: result.append(section)
        elif char == "'":
            quote ^= 1
    
    if quote:
        raise Exception("unmatched single quote.")
    
    return result
        
split("   Hello   World! 'Beauti'ful world! ")

Link 1 Link 2

Consider 2 x 2 - 2 ** 4 = 16 cases Only viaable configs are: 00 11 00 11 01 10 01 10 00 11 11 00 01 10 10 01 All others 8 cases are bad

Move to 3 x 3 - 2 ** 9 = 512 case, inpossible to enumerate, try reduce to 2 x 2 case Consider config in following which we can archive by enforcing first row and first column 000 0xx 0xx

these three must from above 6 0a 00 ax 0x ax xx

leaving the ax xx only two options 11 00 11 00

and 11 11 is not valid actually.

000 010 011 100 011 -> 001 -> 000 -> 000 -> not possible 011 001 000 000

==> once we make first row and first column to be all 0, the reminder has to be all 0