網頁

2025年8月7日 星期四

C++ DP速解難題leetcode 3363 Find the Maximum Number of Fruits Collected/Py3 code inclusive


C++ DP速解難題leetcode 3363  Find the Maximum Number of Fruits Collected
[Python解請進]

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日 星期日

二元搜尋sliding window C++ py3解Leetcode 2106難題Maximum Fruits Harvested After ...


二元搜尋sliding window cpp py3解Leetcode 2106難題Maximum Fruits Harvested After at Most K Steps
[C++ solution請進]

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年7月4日 星期五

Py3 C++位元計算bitset速解Leetcode難題3307 Find the K th Character in String Gam...

Py3 C++位元計算bitset速解Leetcode難題3307  Find the K th Character in String Game II
[Py3 一行解請進]

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月29日 星期四

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月17日 星期六

opencv C++使用卷積convolution/correlation進行影像處理

 
 opencv C++使用卷積convolution/correlation進行影像處理 

C/C++/Py3使用荷蘭旗演算3 pointer解Leetcode 75 Sort Colors

C C++ Py3使用荷蘭旗演算3 pointer解Leetcode 75 Sort Colors
一次走訪演算採用於荷蘭旗演算法。
[Py3解請進]

C++ Py3 Greedy 2 pointer速解(含1行解)Leetcode 2900 Longest Unequal Adjacent ...


C++ Py3 Greedy 2 pointer速解2900  Longest Unequal Adjacent Groups Subsequence I
C++ 會採用 2 -pointers主要的原因,就是不要額外的陣列,達到真正的O(1)空間解。
[Py3一行解請進]
---------

C++ Py3 Greedy速解Leetcode 2918 Minimum Equal Sum of Two Arrays After Rep...


C++ Py3 Greedy速解Leetcode 2918  Minimum Equal Sum of Two Arrays After Replacing Zeros
影片展示如何使用C++ STL編寫一個簡短的C++解答; Python程式碼也是按照類似的方式完成的。
[Py3 code請進]

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

python版教科書級RSA產生key

有些版次的車輪沒造好,就自己造,以下為教科書版能處理1024-bit RSA的python程式

C++ Py3 Sliding window速解Leetcode 2379 Minimum Recolors to Get K Consec...


C++ Py3  Sliding window速解2379  Minimum Recolors to Get K Consecutive Black Blocks
[Py3 解含過程輸出請進]

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月26日 星期三

C++ prefix sum Kadane Python一行解Leetcode 1749. Maximum Absolute Sum of An...



cpp prefix sum Kadane Python一行解Leetcode 1749. Maximum Absolute Sum of Any Subarray
[解答請進]

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解請進」