Simon Lai's Blog
  • 首頁
  • 歸檔
  • 分類
  • 標籤
  • 關於

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

1004. Max Consecutive Ones III - Medium

前往題目 思路 看到subarray基本上是用sliding window 看到可以翻轉k個,基本上可以用dp sliding window 如果遇到1,就紀錄長度 如果遇到0,就紀錄當前幾個0,超過k的話把左邊指針右移直到0的個數小於等於k,然後再紀錄一下長度 Code官神影片同時講了dp和sliding window,dp在這題不太可行是因為需要用到二維陣列,這樣nums.length
2024-02-01
Leetcode > Medium
#Leetcode #心得 #Array #Binary Search #Sliding Window #Prefix Sum

3. Longest Substring Without Repeating Characters - Medium

前往題目 搬運以前的文章 想法 這題也沒寫出來,有嘗試用2 pointers和hashmap做,但失敗了 解法也看了半小時才懂,卡在為甚麼要remove那個character,當發現有同樣的時候 原因是因為那整個substring都不能要了,也記錄了長度,所以會逐漸remove 思路 Set儲存substring Sliding window char沒在set裡面的話就加進去,然後看看有沒有
2024-01-31
Leetcode > Medium
#Leetcode #心得 #String #Hash Table #Sliding Window

121. Best Time to Buy and Sell Stock - Easy

前往題目 之前寫的文章 思路 sliding window high指針一直往右,遇到low指針的值比high還大就把low直接移過去high,代表找到更小值了 Codeclass Solution { public int maxProfit(int[] prices) { int low = 0, high = 0; int re
2024-01-31
Leetcode > Easy
#Leetcode #心得 #Array #Dynamic Programming
1…2122232425…36

搜尋

Hexo Fluid