Hard
LeetCode 1001: Grid Illumination
·2 分鐘
LeetCode
Hard
Hash-Table
Array
LeetCode 1001 解題筆記:列、欄、兩條對角線各開一個計數器(r - c、r + c),加上亮著的燈的集合;關燈時四個計數器都要減
LeetCode 1842: Next Palindrome Using Same Digits
·2 分鐘
LeetCode
Hard
Two-Pointers
String
LeetCode 1842 解題筆記:回文由左半邊決定,中間的數字固定不動;對左半邊做 next permutation,再鏡像回去
LeetCode 3313: Find the Last Marked Nodes in Tree
·1 分鐘
LeetCode
Hard
Tree
Bfs
Graph
Tree-Diameter
LeetCode 3313 解題筆記:最後被標記的就是最遠的點,而最遠點一定能取直徑端點
LeetCode 2608: Shortest Cycle in a Graph
·2 分鐘
LeetCode
Hard
Bfs
Graph
LeetCode 2608 解題筆記:從每個點 BFS,遇到已拜訪而且不是 parent 的鄰居,就找到一個長度 dist[u] + dist[v] + 1 的環
LeetCode 2561: Rearranging Fruits
·1 分鐘
LeetCode
Hard
Greedy
Hash-Table
Sorting
LeetCode 2561 解題筆記:每種值總數要是偶數;多出來的排序後,便宜的一半各自跟貴的交換,成本取 min(便宜的值, 2 × 全域最小值)
LeetCode 716: Max Stack
·2 分鐘
LeetCode
Hard
Stack
Heap
Design
Lazy-Deletion
LeetCode 716 解題筆記:stack 和 heap 都存唯一 id,刪除只記 id,查詢時再丟掉已刪除的;用值記錄會分不清重複的元素
LeetCode 2218: Maximum Value of K Coins From Piles
·1 分鐘
LeetCode
Hard
Dynamic-Programming
Knapsack
Prefix-Sum
Array
LeetCode 2218 解題筆記:每一堆只能從上面拿,拿 x 個的價值是前綴和;分組背包,dp[j] = 前幾堆總共拿 j 個的最大價值,每一堆枚舉拿幾個
LeetCode 2416: Sum of Prefix Scores of Strings
·1 分鐘
LeetCode
Hard
Trie
String
Counting
LeetCode 2416 解題筆記:Trie 每個節點記錄經過的單字數,單字的答案就是沿路計數的總和
LeetCode 987: Vertical Order Traversal of a Binary Tree
·1 分鐘
LeetCode
Hard
Tree
Binary-Tree
Bfs
Sorting
LeetCode 987 解題筆記:收集 (欄, 列, 值) 三元組,排序後依欄分組
LeetCode 940: Distinct Subsequences II
·1 分鐘
LeetCode
Hard
Dynamic-Programming
String
LeetCode 940 解題筆記:依最後一個字元分類;以 c 結尾的不同子序列 = 目前所有不同子序列後面接 c,再加上 c 本身,直接覆蓋舊的值
LeetCode 2973: Find Number of Coins to Place in Tree Nodes
·2 分鐘
LeetCode
Hard
Dynamic-Programming
Tree-Dp
Depth-First-Search
Sorting
Heap
Tree
LeetCode 2973 解題筆記:三個數的最大乘積只可能是「最大的三個」或「最小的兩個 × 最大的一個」;每個子樹只回傳最小 2 個和最大 3 個,後序合併
LeetCode 1246: Palindrome Removal
·1 分鐘
LeetCode
Hard
Dynamic-Programming
Interval-Dp
Array
LeetCode 1246 解題筆記:區間 DP;arr[i] 自己刪、兩端相同時跟裡面一起刪,或切成兩段,三種取最小