herrDeng網內搜尋

自訂搜尋

Ads

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請進 。
Related Posts Plugin for WordPress, Blogger...

熱門文章