↓快轉到主要內容

LeetCode

LeetCode 3800: Minimum Cost to Make Two Binary Strings Equal
·1 分鐘
LeetCode Medium Greedy String
LeetCode 3800 解題筆記:不相等的位置分兩類;不同類的兩兩用 swap,同類的兩兩用 cross + swap,剩一個只能 flip,每種都跟兩次 flip 比
LeetCode 3799: Word Squares II
·1 分鐘
LeetCode Medium Backtracking Enumeration String
LeetCode 3799 解題筆記:四層枚舉,每一層先用角落字母剪枝,排序後輸出自然就是字典序
LeetCode 3798: Largest Even Number
·1 分鐘
LeetCode Easy String
LeetCode 3798 解題筆記:偶數只看最後一位,把結尾的 1 刪掉就好;保留越多位數越大
LeetCode 2953: Count Complete Substrings
·2 分鐘
LeetCode Hard Sliding-Window Hash-Table String
LeetCode 2953 解題筆記:先在相鄰字元差 > 2 的地方切段;每段枚舉有幾種字元 d,視窗長度固定是 d × k,維護「剛好出現 k 次的字元數」
LeetCode 2269: Find the K-Beauty of a Number
·1 分鐘
LeetCode Easy Sliding-Window Math String
LeetCode 2269 解題筆記:把數字轉成字串,枚舉每個長度 k 的子字串;0 不能當除數
LeetCode 2200: Find All K-Distant Indices in an Array
·1 分鐘
LeetCode Easy Two-Pointers Array
LeetCode 2200 解題筆記:每個 key 往左右各延伸 k 格,用一個指針記住已經輸出到哪裡,避免重疊的區間重複輸出
LeetCode 2156: Find Substring With Given Hash Value
·1 分鐘
LeetCode Hard Sliding-Window Rolling-Hash String
LeetCode 2156 解題筆記:雜湊的第一個字元乘 p^0,從右往左滑動時只要乘 p 再加新字元、扣掉 p^k 倍的出去字元,不需要除法;modulo 不一定是質數,不能用模反元素
LeetCode 1016: Binary String With Substrings Representing 1 To N
·2 分鐘
LeetCode Medium Sliding-Window String Bit-Manipulation Hash-Table
LeetCode 1016 解題筆記:s 最多 1000 個字元,長度固定時的子字串數量有限,n 太大一定是 False;從 n 往下檢查會很快失敗,或從每個起點往右最多延伸 30 位收集所有 <= n 的值
LeetCode 3796: Find Maximum Value in a Constrained Sequence
·1 分鐘
LeetCode Medium Greedy Array
LeetCode 3796 解題筆記:每個位置的上界是自己的限制、左邊上界 + 差值、右邊上界 + 差值三者取最小;左右各掃一次
LeetCode 3795: Minimum Subarray Length With Distinct Sum At Least K
·1 分鐘
LeetCode Medium Sliding-Window Hash-Table Array
LeetCode 3795 解題筆記:求最短的可變視窗;同一個值只算一次,記錄每個值最後出現的位置,左邊界移出的是最後一次出現才扣掉
LeetCode 3794: Reverse String Prefix
·1 分鐘
LeetCode Easy Two-Pointers String
LeetCode 3794 解題筆記:切片一行解決;手寫雙指針時要先把字串轉成 list
LeetCode 2653: Sliding Subarray Beauty
·1 分鐘
LeetCode Medium Sliding-Window Hash-Table Array
LeetCode 2653 解題筆記:值域只有 -50 到 50,固定視窗用計數陣列維護,每個視窗從 -50 往上累加找第 x 小的負數