/* 自定義代碼塊樣式 */

herrDeng網內搜尋

自訂搜尋

Ads

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解法
-------
Related Posts Plugin for WordPress, Blogger...

熱門文章