↓快轉到主要內容

Medium

LeetCode 633: Sum of Square Numbers
·2 分鐘
LeetCode Medium Two-Pointers Math Binary-Search
LeetCode 633 解題筆記:a 從 0、b 從 isqrt(c) 開始對撞;平方和太大 b 減一,太小 a 加一;注意 0 也算、a 可以等於 b、浮點數開根號的誤差
LeetCode 3820: Pythagorean Distance Nodes in a Tree
·1 分鐘
LeetCode Medium Tree Bfs Graph
LeetCode 3820 解題筆記:三次 BFS 算出每個點到三個目標的距離
LeetCode 3819: Rotate Non Negative Elements
·1 分鐘
LeetCode Medium Array Simulation
LeetCode 3819 解題筆記:把非負數抽出來左旋 k 格,再依序放回原本非負數的位置;一個非負數都沒有時不能對 0 取模
LeetCode 3818: Minimum Prefix Removal to Make Array Strictly Increasing
·1 分鐘
LeetCode Medium Array
LeetCode 3818 解題筆記:從右往左找最長的嚴格遞增後綴,它前面的都要刪
LeetCode 2563: Count the Number of Fair Pairs
·2 分鐘
LeetCode Medium Two-Pointers Sorting Binary-Search Array
LeetCode 2563 解題筆記:把「和落在 [lower, upper]」拆成「和 <= upper」減「和 <= lower - 1」,每個都用排序後的對撞指針 O(n) 數
LeetCode 1574: Shortest Subarray to be Removed to Make Array Sorted
·1 分鐘
LeetCode Medium Two-Pointers Binary-Search Array Monotonic-Stack
LeetCode 1574 解題筆記:留下來的一定是「非遞減前綴 + 非遞減後綴」;先找出最長前綴和後綴,再用雙指針把兩段接起來
LeetCode 1471: The k Strongest Values in an Array
·1 分鐘
LeetCode Medium Two-Pointers Sorting Array
LeetCode 1471 解題筆記:排序後離中位數最遠的一定在兩端,對撞指針每次從兩端挑比較強的那個,挑 k 次
LeetCode 948: Bag of Tokens
·1 分鐘
LeetCode Medium Two-Pointers Greedy Sorting Array
LeetCode 948 解題筆記:排序後,用最小的 token 換分數、用最大的 token 換能量;只有分數 > 0 才能換能量,換不了就停
LeetCode 923: 3Sum With Multiplicity
·3 分鐘
LeetCode Medium Two-Pointers Sorting Hash-Table Counting Array
LeetCode 923 解題筆記:排序後固定第一個數、對撞指針找另外兩個;兩端值相同時用組合數一次算完,不同時把兩端各自的重複數相乘
LeetCode 3775: Reverse Words With Same Vowel Count
·1 分鐘
LeetCode Medium String Simulation Two-Pointers
LeetCode 3775 解題筆記:先數第一個字的母音數,後面母音數一樣的字就反轉
LeetCode 2105: Watering Plants II
·1 分鐘
LeetCode Medium Two-Pointers Simulation Array
LeetCode 2105 解題筆記:Alice 從左、Bob 從右,對撞指針模擬澆水;相遇在同一株時由水比較多的人澆
LeetCode 1877: Minimize Maximum Pair Sum in Array
·1 分鐘
LeetCode Medium Two-Pointers Greedy Sorting Array
LeetCode 1877 解題筆記:排序後最小配最大、次小配次大,對撞指針取配對和的最大值