網頁

2024年9月19日 星期四

C++採Catalan數的遞迴DP方式計算Leetcode 241 Different Ways to Add Parentheses


C++採Catalan數的遞迴DP方式計算Leetcode 241  Different Ways to Add Parentheses
每個數字都是葉節點,而+、-、*操作則不是。算術公式的 dfs 走訪等價於其對應的帶括號的 inOrder 算式。
-----

2024年9月15日 星期日

Python C++ bitmask解Leetcode 1371 Find the Longest Substring Containing ...


Python C++ bitmask解Leetcode 1371  Find the Longest Substring Containing Vowels in Even Counts
建構一個容器first_seen[32](5個母音考慮2**5=32),它表示第一次看到的32不同bmask的索引
[Python code請進]
-----

2024年9月5日 星期四

python C++速解Leetcode算術題2028 Find Missing Observations


python C++速解Leetcode算術題2028  Find Missing Observations
[Python code請進]

2024年9月3日 星期二

python C++速解Leetcode 1945 Sum of Digits of String After Convert


python C++速解Leetcode 1945  Sum of Digits of String After Convert
這是個簡單問題,有技巧。
第一次轉換不需要用bigINT來存,只是將數字與第1輪的數字相加即可。
由於s.length最多可能為100,因此使用bigInt或其他字串效率非常低。
[Python code請進]
---

2024年8月23日 星期五

C++ stringstream解分數加減Leetcode 592 Fraction Addition and Subtraction


C++ stringstream解分數加減Leetcode 592  Fraction Addition and Subtraction
程式設計分數加減很難嗎?計算容易,但處理字串費事,用stringstream就簡單了

2024年8月17日 星期六

C++ DP動態規劃解Leetcode 937 Maximum Number of Points with Cost


C++ DP動態規劃解Leetcode 937  Maximum Number of Points with Cost
有些標示medium要比標示hard的問題還要難,Leetcode 1937. Maximum Number of Points with Cost,DP動態規劃可解,但怎麼樣的遞迴關係才能降低時間複雜度?
[Python解請進]
----------

2024年8月15日 星期四

python C++ Greedy速解找零問題Leetcode 860 Lemonade Change


python C++ Greedy速解找零問題Leetcode 860  Lemonade Change
b=20時貪婪演算法保持檢查順序,先找十塊,不要改變檢查順序,否則不起作用
[python解請進]
-----

2024年8月10日 星期六

C++ EULER特徵數定理與UnionFind解Leetcode959 Regions Cut By Slashes


EULER特徵數定理與UnionFind解Leetcode959 Regions Cut By Slashes
EULER特徵數定理:平面連通圖有V-E+F
--------------

2024年8月9日 星期五

Python C++解魔方陣Leetcode 840 Magic Squares In Grid



Python C++解魔方陣Leetcode 840  Magic Squares In Grid.
影片一開始就是展示如何解一個3x3的魔方陣,5總是在中心
In the beginning of film is to show how to solve a 3x3 magic square, 5 is always in the center.

2024年8月2日 星期五

Python C++ sliding windows解Leetcode 2134 Minimum Swaps to Group All 1'...


Python C++  sliding windows解Leetcode 2134  Minimum Swaps to Group All 1's Together II
使用sliding window, 由於陣列是循環的,所以右邊的游標r應該一直走到n+n0或n+n1
[Python  code請進]
-----

2024年7月26日 星期五

C++Floyd Warshall解Leetcode 1334 Find the City With the Smallest Number ...


C++Floyd Warshall解Leetcode 1334  Find the City With the Smallest Number of Neighbors at a Threshold
由於每對 (i, j) 都有許多最短路徑需要計算,採用 Floyd-Warshall 演算法比較划算。
---

2024年7月24日 星期三

Python C++速解排序問題Leetcode 2191 Sort the Jumbled Numbers


今日Leetcode的排序問題2191. Sort the Jumbled Numbers,主要有兩個部份,一個是把數字轉成另一個數字,用int就行,採Least Significant Digit First。然後排序,不建議用自訂Lambda,跑能過但非常慢;更不建議自製車輪去排序。比較快的方式是sort ((mapping(x), index),然後再從排序後的資料中取得 x
[Python code請進]

2024年7月20日 星期六

Python C++採Greedy 2 pointers解Leetcode 1605 Find Valid Matrix Given Row ...


Python CPP採Greedy 2pointers解Leetcode 1605  Find Valid Matrix Given Row and Column Sums
將大小為 r*c 的陣列 arr 初始化為全 0
獨立的 i、j 是 2 個指標。使用迴圈繼續
設 x=min(rowSum[i], colSum[j])
設定 arr[i][j]=x & rowSum[i]-=x, colSum[j]-=x 根據 rowSum[i]==0 & colSum[j]==0 移動 i, j。
[Python解請進]
----

2024年7月2日 星期二

Python C++速解multiset問題Leetcode 350 Intersection of Two Arrays II


Python C++速解multiset問題Leetcode 350  Intersection of Two Arrays II
兩個多重集合(multiset)的交集是兩個多重集中公共元素的多重集合。
Python code請進

2024年6月26日 星期三

Python C++ InOrder與Greedy解Leetcode 1382 Balance a Binary Search Tree


Python C++ InOrder與Greedy解Leetcode 1382  Balance a Binary Search Tree
使用中序/逆中序走訪建立按升序/降序排序的 int 陣列。
貪婪演算法(分而治之)建構平衡的 BST。
Python code請進

2024年6月15日 星期六

Python C++ Sort,Priority Queue, Binary search解Leetcode難題502 IPO


Python C++ Sort Priority Queue Binary search解Leetcode難題502 IPO
建立一個容器 cp,裡面包含配對的資訊 (capital[i], profits[i]),其中 i 從 0 到 n-1。
將 cp 按照字典順序(預設排序)進行排序。
(Python code請進)

2024年6月14日 星期五

Python C++計數速解Leetcode 945 Minimum Increment to Make Array Unique


Python C++計數速解Leetcode 945  Minimum Increment to Make Array Unique
先計數每個x出現幾次,再從小而大,把多餘的x移動到下一個數字x+1
Python解答請進

2024年6月10日 星期一

Python C++計數排序與一行解Leetcode 1051 Height Checker


套用系統排序函數解當然簡單,自己造輪子就稍微進階點,觀察輸入資料限制,counting sort應該是可以自造又不失為簡單且線性時間的解答。

Python C++計數排序與一行解Leetcode 1051  Height Checker
Python 1 行解以及
計數排序的解,它使用計數排序並重複使用heights來存原始陣列和排序後的差異;然後使用 |heights|-count(heights, 0) 給出答案。 C++ 和 python 程式碼都已製作。
一行解請進

2024年6月9日 星期日

Python C++ prefix sum mod k陣列和公式解Leetcode 974 Subarray Sums Divisible...


Python C++ prefix  sum  mod k陣列和公式解Leetcode 974  Subarray Sums Divisible by K
昨天的問題523. Continuous Subarray Sum會解,今日Leetcode 974. Subarray Sums Divisible by K更沒問題,看一下constraints hash map就不用了,馬上就能解答
Python code請進
----------

2024年6月8日 星期六

Python C++使用prefix sum mod k + hash map解Leetcode523 Continuous Subarray...



Python C++使用prefix sum mod k + hash map解Leetcode523  Continuous Subarray Sum。模k,前綴和 (mod k) 總共有 k 個可能,為 0,1,...k-1。 對於這個條件 1 less eq nums.length less eq 10^5 O(n^2) 解可能會導致 TLE。 對於此條件 1 less leq k less eq 2^31 - 1,陣列版本解決方案可能會導致 MLE。 然而,注意使用前綴和 mod k 的hash映射是一個有效解法。
modulo k there are 0,1,...k-1 totally k possible for prefix sum (mod k).
For this constraint 1  less eq nums.length  less eq 10^5 an O(n^2) solution may lead to TLE.
For this constraint 1 less leq k less eq 2^31 - 1, an array version solution might lead to MLE. 
C++解請進

2024年6月2日 星期日

C++ Python C速解Leetcode 344 Reverse String


C++ Python C速解Leetcode 344  Reverse String
交換索引對 (i, n-1-i)。在C的實作中,交換並沒有使用額外的空間,只是透過xor!

2024年5月26日 星期日

python C++ DP動態規劃速解難題Leetcode 552 Student Attendance Record II


python CPP DP動態規劃速解難題Leetcode 552  Student Attendance Record II
Leetcode 552. Student Attendance Record II為什麼是難題?原來@cache這招會導致MLE。先用動態規劃解,因為參數只有一個,當然還有更快的Matrix power解法
-------

2024年5月25日 星期六

C++DP動態規劃Trie解Leetcode 難題140 wordbreak 2



DP動態規劃Trie解Leetcode 難題140 wordbreak 2 解Leetcode難題140. Word Break II 用DP+ Trie這樣解,應該算是難題 
----- 

2024年5月21日 星期二

Python C++ 回遡bit mask解Leetcode 78, 90 subsets 1, 2


Python C++ 回遡bit mask解Leetcode 78, 90 subsets 1, 2
Subset/Powerset question. 2 approaches :bit mask & backtracking
Python 請進

2024年5月18日 星期六

C++ python後序走訪解Leetcode 979 Distribute Coins in Binary Tree


C++ python後序走訪解Leetcode 979  Distribute Coins in Binary Tree
解Leetcode 979. Distribute Coins in Binary Tree。學過二元樹的一定會使用post-order走訪,重點不是coding有多容易或多困難,重點是二元樹分錢最好是從葉節點開始,孩子要孝順父母!Python code請進
--------

2024年5月12日 星期日

python C++速解MaxPooling Leetcode 2373 Largest Local Values in a Matrix


python CPP速解MaxPooling Leetcode 2373  Largest Local Values in a Matrix
算maxPooling stride=1就一般矩陣計算而已,找出子矩陣的極大,可以只使用額外空間O(1)就得解。C++解請進。
-----

2024年5月4日 星期六

C++ python貪婪排序2 pointer解Leetcode 881 Boats to Save People


C++ python貪婪排序2 pointer解Leetcode 881  Boats to Save People
Greedy是這類問題的關鍵,片中先用sort, counting sort來排序,左有兩游標大小一對看能不能一次載兩人,逐次移動游標
-------

2024年5月1日 星期三

如何將teachable Machine訓練好的模型在Google colab執行python opencv程式


如何將teachable Machine訓練好的模型在Google colab執行python opencv程式

2024年4月29日 星期一

C++/reroot DP動態規劃解Leetcode難題834 Sum of Distances in Tree


C++/reroot DP動態規劃解Leetcode難題834  Sum of Distances in Tree
關鍵字:reroot DP。 你會發現很多關於它的討論。
對於作為根的每個節點,進行 DFS,將獲得 O(n^2) 的解決方案,但可能 TLE。
使用一次遞迴 DFS 求 root=0 的樹中的距離和,計算每個節點 i 作為根的子樹中的節點數。

2024年4月24日 星期三

C++ UnionFind BFS DFS解圖論問題Leetcode 1971 Find if Path Exists in Graph


C++ UnionFind  BFS DFS解圖論問題Leetcode 1971 Find if Path Exists in Graph
使用UnionFind class實作容易,效能又好,這對無向圖特別有效。
為了比較,BFS、DFS也實作

Python C++速解Leetcode 1137 N th Tribonacci Number


Python CPP速解Leetcode 1137  N th Tribonacci Number
先用遞迴、再加上cache,然後轉成動態規劃,當然還可以用矩陣冪次方

2024年4月20日 星期六

C++ DFS BFS貪婪三法解Leetcode農地問題1992 Find All Groups of Farmland


C++ DFS BFS貪婪三法解Leetcode農地問題1992  Find All Groups of Farmland
採用三種方式解題,一是DFS, 二是BFS,前兩法都很標準。農地因為都是矩形,第三法採貪婪演算找出矩形右下角,簡單又快速。

2024年4月16日 星期二

C++ DFS BFS解二元樹Leetcode 623 Add One Row to Tree


C++ DFS BFS解二元樹Leetcode 623 Add One Row to Tree
要辨識一位程式設計師是否會使用常用的資料結構,binary  tree就是個鑑別率高的東西,許多程式自學者壓根沒見過。Leetcode 623. Add One Row to Tree是個好練習

2024年4月14日 星期日

C++ python速解左葉節點之和Leetcode 404 Sum of Left Leaves


C++ python速解左葉節點之和Leetcode 404  Sum of Left Leaves。影片中採最簡易解法preorder走訪遞迴,並設參數isLeft=0,其他解法詳見Leetcode連結。Python解請進,C++提供一行解。

2024年4月12日 星期五

python c++用monotonic stack解 Leetcode難題42 trapping rain water並附pyplot繪直方圖解說


Python c++用monotonic stack解 Leetcode難題42 trapping rain water並附pyplot繪直方圖解說
想法是使用索引 m 的單調堆疊來找到右牆height[r]和左牆height [l];計算此區域高於底部 height[m]的水量應為min(height[r]-height[m], height[l]-height[m])*(r-l-1) 

2024年4月9日 星期二

Python C++速解Leetcode 2073 Time Needed to Buy Tickets


Python CPP速解LLeetcode 2073  Time Needed to Buy Tickets
假設x=tickets[k]。 當i≤k時,person[i]最多只能買x張票; 當 i大於k時,person[i]最多只能購買 x-1 票。python code請進

2024年4月7日 星期日

python C++DP動態規劃解Leetcode 678 Valid Parenthesis String


python C++DP動態規劃解Leetcode 678  Valid Parenthesis String
使用2D DP來求解。 由於 n≤100,因此帶有 memo 的遞迴可以完成這項工作。 知道了遞迴那麼DP就一定可行!
第二種方法是迭代 DP 程式碼; 使用 &1 技巧減少空間至O(n)。Python code請進 。

2024年4月6日 星期六

python C++字串deque解Leetcode 1249 Minimum Remove to Make Valid Parentheses


python C++字串deque解Leetcode 1249  Minimum Remove to Make Valid Parentheses
連續幾天都是含括號的字串問題Leetcode 1249. Minimum Remove to Make Valid Parentheses,都提示用stack其實不用,走訪兩次就好,一次去掉多的 ')' ,第二反向去掉多的 '(' ;選用適當的容器,字串、deque就得解了,最快的C++解請進

2024年4月5日 星期五

python C++速解Leetcode 1544 make the string great


python C++速解Leetcode 1544 make  the string great
基本上算是stack解法,但實作的語言C, C++, python只需要善用個別字串的特性,無須使用stack,大幅加快計算速度,C語言解答請進,並加上最佳C++解

2024年3月29日 星期五

C++. Python Sliding window與組合公式解Leetcode 2962


python, CPP Sliding window與組合公式解Leetcode 2962. Count Subarrays Where Max Element Appears at Least K Times

2024年3月27日 星期三

python單一迴圈解答最佳股票的買賣時機LeetCode 121 Best Time to Buy and Sell Stock



先說明一下這是後知後覺的解答,所謂後知就是股票價格已知存在陣列(清單),當然就要用迴圈練習,雙迴圈暴力解需時O(n**2),當然不用,採python單一迴圈解答「最佳股票的買賣時機#LeetCode 121  Best Time to Buy and Sell Stock」,解法簡單快速O(n)時間。

2024年3月23日 星期六

C++ Folyd龜兔賽跑演算解Leetcode 234, 143 Reorder List

C++ Folyd龜兔賽跑演算解Leetcode 234, 143  Reorder List
今日Leetcode的問題,應該是連續三個相關Linked List問題的總結,基本上都先考慮純單向鏈結的解法,如加上其他容器如deque, stack, vector就會簡單許多。

2024年3月16日 星期六

Python dict C++陣列Prefix Sum解Leetcode 525 Contiguous Array


Python  dict C++陣列Prefix Sum解Leetcode 525 Contiguous Array
使用 Prefix sum 來計算 n0 中的 0 和 n1 中的 1。 使用容器記錄n1-n0的索引。
hash表 (C++ unordered_map, Python dict) 用於此任務。
第二種方法是從第一種方法修改而來的陣列版本,速度很快。python這有提供。

2024年3月9日 星期六

C++雙指標與二元搜尋解Leetcode 2540 Minimum Common Value


C++雙指標與二元搜尋解Leetcode 2540  Minimum Common Value
陣列已排序。 使用雙指標或使用二元搜尋找來解它
第二種方法使用二分搜索,當 n1 或 n2 非常大時另外一個數很小時,這種方法非常有效。

2024年3月8日 星期五

python C C++陣列迴圈速解Leetcode 3005 Count Elements With Maximum Frequency


python C C++陣列迴圈速解Leetcode 3005  Count Elements With Maximum Frequency
陣列迴圈就能解答的,何須用到hash table(C++ unordered_map)簡單的問題可用簡單的 資料結構解答,迴圈就是迴圈、陣列就是陣列(內有python code)

2024年3月3日 星期日

python C C++速解Leetcode 19 Remove Nth Node From End of List並考慮memory leaks問題


python C C++速解Leetcode 19 Remove Nth Node From End of List並考慮memory leaks問題
其實刷題不要只是貪快還是要考慮Memory Leaks,解老題目19 Remove Nth Node From End of List要特別別考慮,另別是C/C++使用者

2024年3月1日 星期五

C++ 2行Python 1行解Leetcode 2864 Maximum Odd Binary Number


Leetcode 2864. Maximum Odd Binary Number問題,要想寫出一行解也不是那麼容易,C++合理的二行解、python的一行解。要點先數幾個1再重填字串,另外還有使用兩個游標的解法,未在影片中。

2024年2月22日 星期四

python C++計算 indegree解Leetcode 997 Find the Town Judge


python C++計算 indegree解Leetcode 997  Find the Town Judge
使用圖形解決方案。
由於信任中的每一對 e=(a, b) 實際上表示一種信任關係,可以將其視為有向邊。C++程式請進
---

2024年2月21日 星期三

python C++ bit 計算O(1)時間解Leetcode 201 Bitwise AND of Numbers Range


python C++ bit 計算O(1)時間解Leetcode 201  Bitwise AND of Numbers Range
這就是用x&(x-1)技法。有人會說計算時間是O(log n),但32-bit整數,無論是迴圈或遞迴頂多32次。如果要熟透bit處理的訣竅,還有更厲害的技法,請進

2024年2月20日 星期二

python C++高斯小學公式與xor sum速解Leetcode 268 Missing Number


python C++高斯小學公式與xor sum速解Leetcode 268  Missing Number
片中講兩種解法,一是用Gauss的n(n+1)/2公式,二是用xor-sum
第三種解法片中未顯示,採radix sort再binary search

2024年2月17日 星期六

C++貪婪演算與Min Heap解Leetcode 1642 Furthest Building You Can Reach

Leetcode問題1642. Furthest Building You Can Reach也是用Greedy演算,有梯子、有磚塊,要訣就是高度差大的用梯子,高度差小的用磚塊,至於容器,C++可用priority_queue或用make_heap,當然也可用multiset,用heap的解答當然很快速,千萬不要誤入歧途採用DP動態規劃,先看constraints就知。

2024年2月15日 星期四

C++, python貪婪演算解多邊形問題Leetcode 2971 Find Polygon With the Largest Perimeter


C++, python貪婪演算解多邊形問題Leetcode 2971  Find Polygon With the Largest Perimeter
程式實作不難,用貪婪演算即可,有人知其然不知所以然,貪婪演算是否可用是需要數學證明的,下面有一個用make_heap的解答,保留在此

2024年2月13日 星期二

C, C++ ,python速解迴文Leetcode 2108 Find First Palindromic String in the Array


C C++ python速解迴文Leetcode 2108  Find First Palindromic String in the Array
這就是迴文「花蓮噴水池水噴蓮花」"racecar",Leetcode蒐集了一堆跟迴文有關的問題,應該是說科技公司面試常出迴文問題。想看C語言解答請進

2024年2月11日 星期日

C++ DP動態規劃解Leetcode難題1463 Cherry Pickup II


先用DP動態規劃來解,演算法應該是還能優化,但O(n*m^2)目前也就這樣吧

2024年2月7日 星期三

Python C++採陣列排序速解字串問題Leetcode 451 Sort Characters By Frequency


解Leetcode 451. Sort Characters By Frequency.兩個重點,一是可用陣列就不用hash table(C++ unordered_map)來計數,二是排序的元素很少,可以練習各種排序法,用sort,自製radix sort,用max heap(C++ priority_queue)

2024年2月6日 星期二

Python C++ hash table速解Leetcode 49 Group Anagrams


Python C++ hash table速解Leetcode 49  Group Anagrams
Leetcode今天是老題目49. Group Anagrams,解法很多種,不過弄清楚anagram就是排列,一個簡易又快速的解答就出爐了

2024年2月4日 星期日

C++ sliding window頻率計數陣列解Leetcode難題76 Minimum Window Substring打敗100趴


C++ sliding window頻率計數陣列解Leetcode難題76  Minimum Window Substring打敗100趴
難題Leetcode 76. Minimum Window Substring半年前就解出來了,不過解法不好,保持主要sliding window的架構,把unordered_map換成C int array,用點bitmask 就64個元素的陣列,先用C++,再用python於是100趴的code就成形了

2024年2月3日 星期六

Python C++ dp動態規劃解Leetcode 1043 Partition Array for Maximum Sum


Python C++ dp動態規劃解Leetcode 1043  Partition Array for Maximum Sum
如果你看一下遞迴公式
dp[i]=max(ans, dp[i-j]+j*maxA)  for j in  [1...min(i, j)]
它是 k+1 項遞迴,這意味著遞迴公式中最多有 k+1 個連續項。 所以為了節省空間,可以改為
dp[i%k]=max(ans, dp[(i-j)%k]+j*maxA) for j in  [1...min(i, j)]

2024年1月31日 星期三

C++ python用monotonic stack解Leetcode 739 daily temperatures

C++ python用monotonic stack解Leetcode 739 daily temperatures。Stack在程式設計、資料結構的課會教,但monotonic stack就不一定了。Leetcode 739 daily temperatures,題目問天氣要等幾天才會變暖,當然迴圈可用倒序,這個確定後,當然要確保堆疊的頂端所代表的溫度大於temperatures[i]...

2024年1月27日 星期六

C++ python Mahonian三角四項遞迴公式解Leetcode難題629 K Inverse Pairs Array

C++ python Mahonian三角四項遞迴公式解Leetcode難題629  K Inverse Pairs Array
Fact 1 is for better writing f(n, k)=\sum_{j=0}^{n-1}f(n-1, k-j)
Leetcode 629. K Inverse Pairs Array解這類的Leetcode難題,先導一導數學式,

2024年1月25日 星期四

python C++遞迴邁向dp動態規劃解Leetcode 1143 Longest Common Subsequence


python C++遞迴邁向dp動態規劃解Leetcode 1143  Longest Common Subsequence. LCS之類的問題其實跟 DNA 序列的比對問題密切關聯。非常經典的DP動態規劃問題,有的人會TLE,請注意不是只設cache就好,尤其是C++,字串不要call-by-value 

2024年1月24日 星期三

C++ python使用DFS與奇偶測試解Leetcode1457 Pseudo Palindromic Paths in a Binary ...


C++ python使用DFS與奇偶測試解Leetcode1457  Pseudo Palindromic Paths in a Binary Tree

2024年1月23日 星期二

C++遞迴試所有組合bitset解Leetcode 1239 Maximum Length of a Concatenated String ...


C++遞迴試所有組合bitset解Leetcode 1239  Maximum Length of a Concatenated String with Unique Characters。

2024年1月21日 星期日

2024年1月20日 星期六

C++ python DP動態規劃與monotonic stack單調堆疊解Leetcode 907 Sum of Subarray Mini...


解Leetcode 907. Sum of Subarray Minimums,用了DP動態規劃以及Monotonic stack,可以得到線性時間解,如果直接解,那可是O(n^3)時間,有好方法當然要會用

2024年1月14日 星期日

python C++解Leetcode字串問題1657 Determine if Two Strings Are Close


python C++解Leetcode字串問題1657  Determine if Two Strings Are Close

2024年1月13日 星期六

python C++速解Leetcode 1347 Minimum Number of Steps to Make Two Strings A...


python cpp速解Leetcode 1347  Minimum Number of Steps to Make Two Strings Anagram
No need for hash table. there are are just 26 alphabets.
s is anagram of t  ⟺ freq(s)==freq(t)
In other words, s is a permutation of t.

2024年1月8日 星期一

Python C++解二元搜尋樹問題Leetcode 938 Range Sum of BST


Python C++解二元搜尋樹問題Leetcode 938  Range Sum of BST。其實這是蠻典型二元收尋樹的問題,可以試著使用preorder, postorder & inorder等不同走訪的方式來解,二元收尋樹是資料結構的內容,會用它來寫程式跟不會用,就有顯著的區隔。

2024年1月3日 星期三

Python C++速解Leetcode 2125 Number of Laser Beams in a Bank

Python C++速解Leetcode 2125  Number of Laser Beams in a Bank