1046. Last Stone Weight - Easy 前往題目 想法 用priority queue 思路這題無需考慮位置,因為最終回傳的也是剩下的weight,而不是哪顆石頭,所以非常簡單 利用priority queue把stones存為descending order(降冪) 然後把前兩項相減後的結果再加入pq(兩數都要poll) 直到pq.size只剩1,直接回傳pq.poll() 題目說如果沒有石頭那就回傳0,而我沒判斷也AC,應該 2024-02-08 Leetcode > Easy #Leetcode #心得 #Array #Heap (Priority Queue)
703. Kth Largest Element in a Stream - Easy 前往題目 想法 用priority queue 思路 把初始array放進priority queue裡面 每次加入的時候都先加入新的數字,然後再把pq刪減到剩下k項(因為這題只有add方法所以只要維持k個就好,因為k值固定所以更小的數字永遠不會用到) Codeclass KthLargest { private int k; private PriorityQue 2024-02-08 Leetcode > Medium #Leetcode #心得 #Binary Search Tree #Binary Tree #Tree #Heap (Priority Queue) #Design #Data Stream
1448. Count Good Nodes in Binary Tree - Medium 前往題目 想法 搜尋的時候維護最大值 思路難得自己寫出來DFS DFS 搜尋時維護最大值 Codeclass Solution { private int res; // Result public int goodNodes(TreeNode root) { res = 0; // Start DFS d 2024-02-07 Leetcode > Medium #Leetcode #心得 #Binary Tree #Depth-First Search #Breadth-First Search #Tree
138. Copy List with Random Pointer - Medium 前往題目 想法 關鍵應該是random要怎麼接上,一個想法是先疊代整個list deepcopy出沒有random的list,過程中建立hashmap映射值和其對應的node,這樣時間上是O(2N),但空間需要O(N) 只想得到用map儲存node和值的對應關係,但是值不是唯一所以不能當作key,有了node的時候值又是多餘的。可能可以用array,但是最大的array會需要$10^4$的空間 2024-02-07 Leetcode > Medium #Leetcode #心得 #Hash Table #Linked List
875. Koko Eating Bananas - Medium 前往題目 思路 利用binary search(不是搜尋piles array,而是最小數與最大數的區間) 最少的可能是每小時1根香蕉,最大就是piles裡面最大的,因為piles.length <= h,所以最大值一定可以在時限內吃完 binary search搜尋,每次計算piles能不能只需要每小時mid根香蕉吃完,如果不行,小指針指向mid + 1,如果可以,那大指針就變為mid, 2024-02-06 Leetcode > Medium #Leetcode #心得 #Array #Binary Search
853. Car Fleet - Medium 前往題目 思路 排序好以後,從最後面開始往前看,如果前車比後車快就會catch up,然後位置靠左也就是前車會放慢速度跟後車並行,這時兩車視為一體了 繼續往前看,以此類推 Code 其實可以不用再弄個class的😂 class Solution { class Car{ private int position; privat 2024-02-06 Leetcode > Medium #Leetcode #心得 #Stack #Array #Monotonic Stack #Sorting
347. Top K Frequent Elements - Medium 前往題目 想法 利用hashmap儲存數字個數以便快速存取 使用priority queue來比較並儲存比較關係 思路自己的思路 hashmap儲存數字個數 priority queue儲存比較關係,由小到大,每當pq size超過k的時候就把頭去掉,因為頭是當前最小值,而最終需要的是前k大的 時間複雜度: O(nums.length + nums.length * log (size o 2024-02-05 Leetcode > Medium #Leetcode #心得 #Array #Hash Table #Sorting #Divide and Conquer #Quickselect #Heap (Priority Queue) #Bucket Sort #Counting
567. Permutation in String - Medium 前往題目 想法 用hashmap,以及sliding window 思路 用array存字母個數 疊代整個s2,如果window>=s1的大小的時候,就可以比較s1和s2 沒有的話把window的首字母數量-1,這樣才能保證window的大小和s1一致 Code class Solution { public boolean checkInclusion(Strin 2024-02-05 Leetcode > Medium #Leetcode #心得 #String #Two Pointers #Hash Table #Sliding Window
994. Rotting Oranges - Medium 前往題目 之前的文章 想法 BFS 思路實作的時候條件判斷卡住,一段時間沒寫生疏了 取得有幾個新鮮橘子(這樣才能知道要感染幾個),以及初始的爛橘子在哪 BFS只關心腐爛橘子,感染其上下左右的橘子,直到感染所有橘子,或是觸碰不到剩餘的橘子 如果還有剩餘新鮮橘子就回傳-1,反之回傳過了幾分鐘 Codeclass Solution { public int orangesRot 2024-02-03 Leetcode > Medium #Leetcode #心得 #Array #Matrix #Breadth-First Search
2024. Maximize the Confusion of an Exam - Medium 前往題目 思路1004題的進階 sliding window 重複兩遍循環,一次是把T當作基準,也就是翻轉成T,一次是把F當作基準 Code class Solution { public int maxConsecutiveAnswers(String answerKey, int k) { int res = 0; int i 2024-02-01 Leetcode > Medium #Leetcode #心得 #String #Binary Search #Sliding Window #Prefix Sum