Hash-Table
LeetCode 1001: Grid Illumination
·2 分鐘
LeetCode
Hard
Hash-Table
Array
LeetCode 1001 解題筆記:列、欄、兩條對角線各開一個計數器(r - c、r + c),加上亮著的燈的集合;關燈時四個計數器都要減
LeetCode 4067: Longest Subarray With Restricted Pair Sums
·2 分鐘
LeetCode
Medium
Sliding-Window
Hash-Table
Array
LeetCode 4067 解題筆記:合法視窗的子陣列也合法,可以滑動;加入 x 時只要檢查跟 x 有關的三元組:x 當加數(x + y 在視窗裡)或 x 當和(x - y 在視窗裡)
LeetCode 2561: Rearranging Fruits
·1 分鐘
LeetCode
Hard
Greedy
Hash-Table
Sorting
LeetCode 2561 解題筆記:每種值總數要是偶數;多出來的排序後,便宜的一半各自跟貴的交換,成本取 min(便宜的值, 2 × 全域最小值)
LeetCode 4019: Merge Close Characters II
·1 分鐘
LeetCode
Medium
Hash-Table
String
LeetCode 4019 解題筆記:留下來的字不會再變,記每種字元在結果裡最後的位置;新字元跟它的距離 <= k 就被合併掉
LeetCode 996: Number of Squareful Arrays
·2 分鐘
LeetCode
Hard
Dynamic-Programming
Bitmask
Backtracking
Math
Hash-Table
LeetCode 996 解題筆記:n 最多 12,用 bitmask DP:dp[mask][last] = 用了 mask 這些數、最後一個是 last 的排列數;相同的數規定依 index 順序使用來去重
LeetCode 2963: Count the Number of Good Partitions
·1 分鐘
LeetCode
Hard
Math
Combinatorics
Hash-Table
Array
LeetCode 2963 解題筆記:同一個數的所有出現位置必須在同一段,用最後出現位置把陣列切成 b 個不可再分的區塊;區塊之間可切可不切,答案是 2^(b-1)
LeetCode 3873: Maximum Points Activated with One Addition
·1 分鐘
LeetCode
Hard
Union-Find
Hash-Table
Array
LeetCode 3873 解題筆記:同 x 或同 y 的點用 Union-Find 合併成塊;新加的點最多接上兩塊,答案是最大的兩塊加 1
LeetCode 3548: Equal Sum Grid Partition II
·2 分鐘
LeetCode
Hard
Prefix-Sum
Hash-Table
Matrix
Enumeration
LeetCode 3548 解題筆記:枚舉橫切(直切用轉置),兩邊差值要等於較大那邊某一格的值;那一邊只有一列或一欄時只能去掉兩端,否則任何一格都可以
LeetCode 3713: Longest Balanced Substring I
·1 分鐘
LeetCode
Medium
Hash-Table
String
Counting
Enumeration
LeetCode 3713 解題筆記:固定起點往右擴;「每種字元次數都一樣」等於「最大次數 × 種類數 = 長度」,每一步 O(1) 判斷
LeetCode 3960: Frequency Balance Subarray
·1 分鐘
LeetCode
Medium
Hash-Table
Counting
Array
LeetCode 3960 解題筆記:n 只有 1000,固定左端點往右擴,同時維護「值 -> 次數」和「次數 -> 個數」;只有 f 和 2f 兩種次數就成立
LeetCode 3900: Longest Balanced Substring After One Swap
·2 分鐘
LeetCode
Medium
Prefix-Sum
Hash-Table
String
LeetCode 3900 解題筆記:前綴差值相同就平衡;差 2 的子字串只要外面有可以換進來的字元也行,每個差值記最早的兩個位置就夠
LeetCode 522: Longest Uncommon Subsequence II
·1 分鐘
LeetCode
Medium
Two-Pointers
String
Sorting
Array
Hash-Table
LeetCode 522 解題筆記:最長的特殊子序列一定是某個完整字串;依長度由長到短檢查,只要不是其他夠長字串的子序列就是答案