↓快轉到主要內容

Counting

LeetCode 3839: Number of Prefix Connected Groups
·1 分鐘
LeetCode Medium Hash-Table String Counting
LeetCode 3839 解題筆記:前 k 個字元相同的字互相連通,用 word[:k] 當 key 計數;長度不到 k 的字要排除
LeetCode 923: 3Sum With Multiplicity
·3 分鐘
LeetCode Medium Two-Pointers Sorting Hash-Table Counting Array
LeetCode 923 解題筆記:排序後固定第一個數、對撞指針找另外兩個;兩端值相同時用組合數一次算完,不同時把兩端各自的重複數相乘
LeetCode 1782: Count Pairs Of Nodes
·2 分鐘
LeetCode Hard Two-Pointers Graph Sorting Hash-Table Counting
LeetCode 1782 解題筆記:先用排序後的度數 + 對撞指針數 deg[a] + deg[b] > q 的配對,再對有共用邊的配對修正多算的部分
LeetCode 992: Subarrays with K Different Integers
·1 分鐘
LeetCode Hard Sliding-Window Hash-Table Counting Array
LeetCode 992 解題筆記:「剛好 k 種」=「最多 k 種」−「最多 k - 1 種」,「最多」型的計數視窗每個右端加 right - left + 1
LeetCode 3805: Count Caesar Cipher Pairs
·1 分鐘
LeetCode Medium Math Hash-Table String Counting
LeetCode 3805 解題筆記:把每個字串整串平移到第一個字母是 a,凱薩等價的字串會變成一樣;再數每種正規化結果的配對數
LeetCode 3679: Minimum Discards to Balance Inventory
·1 分鐘
LeetCode Medium Sliding-Window Hash-Table Simulation Counting Array
LeetCode 3679 解題筆記:規則已經決定哪些要丟,直接模擬;長度 w 的視窗只計算「留下來」的到貨,被丟掉的離開視窗時不能再扣
LeetCode 1512: Number of Good Pairs
·1 分鐘
LeetCode Easy Math Hash-Table Counting Array
LeetCode 1512 解題筆記:邊掃邊數,每個數可以跟前面每個相同的數配一對,答案加上目前的出現次數
LeetCode 3712: Sum of Elements With Frequency Divisible by K
·1 分鐘
LeetCode Easy Hash-Table Counting Array
LeetCode 3712 解題筆記:先數每個值的次數,次數能被 k 整除的就加上「值 × 次數」
LeetCode 2131: Longest Palindrome by Concatenating Two Letter Words
·1 分鐘
LeetCode Medium Greedy Hash-Table String Counting
LeetCode 2131 解題筆記:每個字跟它的反轉配成一對放兩邊;本身是回文的字也能兩兩配對,只有剩下的一個能放正中間
LeetCode 1497: Check If Array Pairs Are Divisible by k
·1 分鐘
LeetCode Daily Medium Array Hash-Map Complement Counting Math Modulo
餘數配對與雜湊表;特別處理 0 與 k/2 的餘數。
LeetCode 3639: Minimum Time to Activate String
·2 分鐘
LeetCode Medium Binary-Search Counting String
LeetCode 3639 解題筆記:每個新星號貢獻 (左距離) × (右距離) 個新子字串;或對時間二分
LeetCode 3545: Minimum Deletions for At Most K Distinct Characters
·1 分鐘
LeetCode Easy Greedy Hash-Table String Sorting Counting
LeetCode 3545 解題筆記:要減少種類就得整種刪掉,從出現次數最少的開始刪