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

herrDeng網內搜尋

自訂搜尋

Ads

2025年9月6日 星期六

C++C py3 log4 prefix sum, bits解Leetcode難題3495 Minimum Operations to Mak...


C++C py3 log4 prefix sum/bits解Leetcode難題3495  Minimum Operations to Make Array Elements Zero
ceil(log4(x)) 可以透過 clz 計算;將 expSum 定義為部分和來計算。
[Py3 code請進]

2025年8月31日 星期日

C++回遡與bitmask解Leetcode 37數獨Sudoku


C++回遡與bitmask解Leetcode 37數獨Sudoku
有些經典問題值得一解再解,雖然不是最佳解,也要比Leetcode Editorial的解有效許多。解數獨,回遡乃基本款,但加上bitmask就能篩掉許多無用的測試,就更快許多
-----
Some classic problems are worth solving again and again. Although they are not the best solutions, they are much more effective than Leetcode Editorial's solutions. Solve Sudoku by using backtracking is the basic version, but add bitmask to filter out many useless tests can make it much faster.
[其他採回溯未用bitmask解法]https://www.youtube.com/watch?v=wviWrbbOdYE

2025年8月28日 星期四

Py3 C++速解Leetcode 3446 Sort Matrix by Diagonals/with Py3 code


Py3 C++速解Leetcode 3446  Sort Matrix by Diagonals
矩陣問題都是很好的迴圈、陣列練習,又加上排序那就是很好的練習題
[Py3 code請進]

2025年8月27日 星期三

C++ dp動態規劃解Leetcode 3459 Length of Longest V Shaped Diagonal Segment


C++ dp動態規劃解Leetcode 3459  Length of Longest V Shaped Diagonal Segment
採DP動態規劃迭代版本解難題,需要定義V-shaped distance
------
Using the DP dynamic programming iterative version, it is necessary to define the V-shaped distance

2025年8月24日 星期日

Py3 C++ sliding window速解Leetcode1493 Longest Subarray of 1's After Dele...


Py3 C++ sliding window速解Leetcode1493  Longest Subarray of 1's After Deleting One Element
示範如何從一般  sliding window解答簡化成單迴圈解答解無if
[Py3解在此,請進]

2025年8月23日 星期六

C++bitmask陣列切割解Leetcode難題3197 Find the Minimum Area to Cover All Ones II


C++bitmask陣列切割解Leetcode難題3197  Find the Minimum Area to Cover All Ones II
這是個立方時間的解答,主要採bitmask陣列法表達矩陣,應該沒人這樣做,最快時間已達8ms擊敗100%的C++參與者。
---------------
This is a cubic time solution, which mainly uses the bitmask array method to express the matrix. It is unlikely that anyone would do this. The fastest time has reached 8ms, beating 100% of the C++ participants.

2025年8月16日 星期六

Py3 C++陣列迴圈速解Leetcode 1323 Maximum 69 Number


Py3 C++陣列迴圈速解Leetcode 1323  Maximum 69 Number
學程式的第一式,就是要會活用陣列迴圈,足以解Leetcode 1323. Maximum 69 Number 
[Py3解請進]

2025年8月13日 星期三

C++一行解Leetcode 326 Power of Three


C++一行解Leetcode 326  Power of Three
利用3的羃次方的因數也是3的羃次方,導出一行C++程式碼
[解答請進]

2025年8月11日 星期一

C++快速模指數計算與prefix sum解Leetcode 2438 Range Product Queries of Powers


C++快速模指數計算與prefix sum解Leetcode 2438  Range Product Queries of Powers
先用fast modular exp解Leetcode 2438. Range Product Queries of Powers,這不會殺雞用牛刀?
-----
First, use fast modular exp to solve Leetcode 2438. Range Product Queries of Powers. This will not be an overkill?

2025年8月5日 星期二

segment tree C++解Leetcode 3479 Fruits Into Baskets III


segment tree C++解Leetcode 3479  Fruits Into Baskets III
線段樹(segment tree)是一種進階的資料結構,可以用來解決一些 LeetCode 難題。
線段樹本質上是一棵二元樹;它可以用二元樹來實現,但更常見的是使用陣列表示。這題需要用到有最大值的線段樹;以及一種尋找第一個不小於target的basket然後更新的方法。
------
Segment tree is an advance data struture which can solve several Leetcode questions.
Segment tree is basically a binary tree, ; it can be implemented by using binary tree, but most frequently its array representation way. This question needs the segment tree with max; and a method which  searchs the 1st basket not less than target & then updates it.

2025年8月3日 星期日

2025年7月16日 星期三

Py3 C++奇偶交錯奇偶子數列解Leecode 3201 Find the Maximum Length of Valid Subsequ...


Py3  C++奇偶交錯奇偶子數列解Leecode 3201  Find the Maximum Length of Valid Subsequence I
求最長有效子序列,使得 (sub[0] + sub[1]) % 2 == (sub[1] + sub[2]) % 2 == ... == (sub[x - 2] + sub[x - 1]) % 2,有 3 種可能的情況
-全為偶數
-全為奇數
-或奇偶交錯子序列
[Py3解請進]

2025年7月15日 星期二

Py3 C++母音 vowel mask速解Leetcode 3136 Valid Word

Py3 C++母音 vowel mask速解Leetcode 3136  Valid Word
Leetcode有許多判斷母音的問題,採用bitmask方式,迅速又方便
-----
Leetcode has many problems in determining vowels. Using the bitmask method is fast and convenient.

2025年7月6日 星期日

C++ Py3 hashmap與binary search解Leetcode 1865 Finding Pairs With a Certain...



C++ Py3 hashmap與binary search解Leetcode 1865 Finding Pairs With a Certain Sum
基本上用 hashmap。解釋為什麼要使用排序和二元搜尋找來優化程式碼
-----
Basically use hashmap. Explain why  one should use sorting and binary search to optimize the code.

2025年6月28日 星期六

Py3排序 C++nth_element速解Leetcode 2099 Find Subsequence of Length K With t...


Py3排序 C++nth_element速解Leetcode 2099  Find Subsequence of Length K With the Largest Sum
C++的nth_element很好用,基本上是做partition,在這個問題可取代sort
----
C++'s nth_element is very useful. It basically does partitioning and can replace sort in this problem.

2025年6月19日 星期四

Py3 C++計數排序counting sort解Leetcode 2294 Partition Array Such That Maximu...

Py3 C++計數排序counting sort解Leetcode 2294  Partition Array Such That Maximum Difference Is K
基本上使用Greedy sort,而排序就用counting sort
[Python C++計數排序與一行解Leetcode 1051 Height Checker]https://www.youtube.com/watch?v=dybiq3pg-Os

2025年6月1日 星期日

C++Py3重複組合排容原理解Leetcode2929 Distribute Candies Among Children II

C++Py3重複組合排容原理解Leetcode2929  Distribute Candies Among Children II
Use combinatorial number with repetition & Inclusion-Exclusion Prinple to solve
[Py3解請進]

2025年5月25日 星期日

C++ Py3計數解Leetcode 2131 Longest Palindrome by Concatenating Two Letter ...


C++ Py3計數解Leetcode 2131  Longest Palindrome by Concatenating Two Letter Words
回文問題常被問到。這題基本上可以用計數的方法來解決。重點是兩個字母的字串 xy 和 yx 可以配對。
-----
Palindrome question is quite often asked. This question can be solved basically by using counting. It's important to know the 2-letter strings xy & yx can be paired.
[C, C++ ,python速解迴文Leetcode 2108 Find First Palindromic String in the Array]https://www.youtube.com/watch?v=eXhdv8jDpao

2025年5月15日 星期四

2025年4月20日 星期日

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)時間。

C++ Py3計數解Leetcode 781 Rabbits in Forest


C++ Py3計數解Leetcode 781 Rabbits in Forest
[Python C++計數速解Leetcode 945 Minimum Increment to Make Array Unique]https://www.youtube.com/watch?v=tS-kunvQwcc

2025年3月5日 星期三

Py3 C++算術數列算面積一行解Leetcode 2579 Count Total Number of Colored Cells


Py3 C++算術數列算面積一行解Leetcode 2579  Count Total Number of Colored Cells
利用算術數列面積公式一行解Leetcode 257
[Py3解請進]


2025年2月19日 星期三

C++py3排列組合與binary表達解Leetcode 1415 The k th Lexicographical String of Al...


C++py3排列組合與binary表達解Leetcode 1415  The k th Lexicographical String of All Happy Strings of Length n
總共有3*2^(n-1)個happy strings要求第k個。先算(k-1)除以2^(n-1)的商q與餘數r,s[0]由q決定,s[1:n]由r決定,化r為二進位,然後簡單處理一下就得解
[Py3解請進]

2025年2月14日 星期五

C++ OOP Prefix Product解Leetcode 1352 Product of the Last K Numbers


C++ OOP Prefix Product解Leetcode 1352  Product of the Last K Numbers
Leetcode這題1352. Product of the Last K Numbers,是很好的OOP題目,除了談談class之外,還能練習一下prefix product的概念(與prefix sum類似但要小心0)
[C++解請進]

2025年2月7日 星期五

Python C++ hashmap解Leetcode 3160 Find the Number of Distinct Colors Am...


Python C++  hashmap解Leetcode 3160  Find the Number of Distinct Colors Among the Balls
嘗試2個hashmap來解決。由於實際運行,可以說,Python dict 比 C++ unordered_map 快得多
[Py3 code請進]

2025年2月6日 星期四

排列組合Python解Leetcode 1726 Tuple with Same Product


本片展示如何理解這題排列組合實質內容,以及如何完成 1 行Python程式碼。
[Py3 code請進]

2025年2月3日 星期一

Python C++單迴圈速解Leetcode 3105 Longest Strictly Increasing or Strictly De...


Python cpp單迴圈速解Leetcode 3105  Longest Strictly Increasing or Strictly Decreasing Subarray
用單迴圈就可解,甚至也沒有用到if, switch, tenary
[Py3解請進]

2025年1月31日 星期五

C++ UnionFind解難題Leetcdoe 827 Making A Large Island(含Py3解)


C++ UnionFind解難題Leetcdoe 827  Making A Large Island
嘗試UnionFind解決。由於該題目透過最多翻轉 1 個額外的 0 單元來詢問最大島嶼的大小;使用Size優化而不是rank。
[Python解請進]

2025年1月19日 星期日

Python C++BFS /heap解Leetcode難題407 Trapping Rain Water II


Py3 cpp BFS /heap解Leetcode難題407 Trapping Rain Water II
解題要訣:boundary由外而內,容器採min heap,BFS走訪
[Py3解請進]

2025年1月11日 星期六

Python C++速解Leetcode迴文1400 Construct K Palindrome Strings



Python C++速解Leetcode迴文1400  Construct K Palindrome Strings
關鍵:偶數長度的回文字串中所有字母都出現偶數,奇數長度的回文字串中除了一個例外其他所有字母都出現偶數。
----------
The key observation: palindrome strings of even lengths have all letters even occurrences, palindrome strings of odd lengths have all letters even occurrences but except one letter with odd occurrence.
[Py3解答請進]

2025年1月3日 星期五

C++ python 用Prefix Sum速解Leetcode 2270 Number of Ways to Split Array


Prefix sum這個概念,只要數學會用Sigma的,問題都不大,基本上就是partial sum。程式要寫的好,老實講該有數學概念真不能少,演算法的東西畢竟是真刀真槍的。

C++ python 用Prefix Sum速解Leetcode 2270  Number of Ways to Split Array
求分割陣列 nums 的方式數量,使得 sum(nums[0...i]) \geq sum(nums[i+1...])。
如何?計算總和sum=sum(nums)
使用loop判斷i是否滿足sum(nums[0..i])\geq sum(nums[i..])=sum-sum(nums[0..i])

「Py3解請進」
Related Posts Plugin for WordPress, Blogger...

熱門文章