Hi, welcome to my space

我是 Richard Lai,這裡只是些廢廢的文章,有空看看吧~

Leetcode 2840 - Check if Strings Can Be Made Equal With Operations II

Approach Use hash table to record the frequency of each character of s1 and s2. Note that only the position with same parity can be swapped, if two strings’ sets of odd-position charaters are the same, they must can be permuted using finite number of transpositions. Code class Solution { public: bool checkStrings(string s1, string s2) { int n = s1.length(); vector<int> s1_odd(26, 0), s1_even(26, 0), s2_odd(26, 0), s2_even(26, 0); for (int i = 0 ; i < n; i++) { if (i & 1) { s1_odd[s1[i]-'a']++; s2_odd[s2[i]-'a']++; } else { s1_even[s1[i]-'a']++; s2_even[s2[i]-'a']++; } } for (int i = 0; i < 26; i++) { if (s1_odd[i] != s2_odd[i] || s1_even[i] != s2_even[i]) return false; } return true; } };

March 30, 2026

Leetcode 3546 - Equal Sum Grid Partition I

解法 基本上就是對於 row 跟 column 存 prefix sum,然後看當前累積的和有沒有機會等於全部元素的和除以 2。這是一題應該被放在簡單類別的中等題。 class Solution { public: bool canPartitionGrid(vector<vector<int>>& grid) { int m = grid.size(), n = grid[0].size(); long long sum = 0; for (auto i:grid) { for (auto j:i) sum += j; } if (sum & 1) return false; sum >>= 1; long long cur_hor = 0; for (int i = 0; i < m-1; i++) { for (int j = 0; j < n; j++) cur_hor += grid[i][j]; if (cur_hor == sum) return true; } long long cur_ver = 0; for (int j = 0; j < n-1; j++) { for (int i = 0; i < m; i++) cur_ver += grid[i][j]; if (cur_ver == sum) return true; } return false; } }; 不知道標題要取什麼 之後想養成跑步的習慣,天氣跟時間允許的話就跑,code跟我至少要有一個是跑得動的~XD 感覺自己還在因為某些事造成的創傷所苦,希望趕快好起來!不然有的時候我真的好怕自己失去面對一切的動力,或許有時候我還是該跟人類說話,我不知道(?)

March 25, 2026

Leetcode 2906 - Construct Product Matrix

Solution 對每個位置存 prefix product 跟 suffix product (這個位置以前所有元素的乘積,和這個位置以後所有元素的 product),最後把這兩個數字乘起來(記得模12345)就好了:))) class Solution { public: vector<vector<int>> constructProductMatrix(vector<vector<int>>& grid) { const int mod = 12345; int n = grid.size(), m = grid[0].size(); vector<vector<int>> prefixP(n, vector<int>(m, 1)); vector<vector<int>> suffixP(n, vector<int>(m, 1)); long long cur = 1; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (i == j && i == 0) { cur = (cur * grid[i][j]) % mod; continue; } prefixP[i][j] = cur % mod; cur = (cur * grid[i][j]) % mod; } } cur = 1; for (int i = n-1; i >= 0; i--) { for (int j = m-1; j >= 0; j--) { if (i == n-1 && j == m-1) { cur = (cur * grid[i][j]) % mod; continue; } suffixP[i][j] = cur % mod; cur = (cur * grid[i][j]) % mod; } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { grid[i][j] = (prefixP[i][j] * suffixP[i][j]) % mod; } } return grid; } };

March 24, 2026

Leetcode 1886 - Determine Whether Matrix Can Be Obtained by Rotation

Problem Link https://leetcode.com/problems/determine-whether-matrix-can-be-obtained-by-rotation/ Solution class Solution { public: bool equal(vector<vector<int>>& a, vector<vector<int>>& b) { int n = a.size(); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (a[i][j] != b[i][j]) return false; } } return true; } void rotate(vector<vector<int>>& matrix) { int n = matrix.size(); reverse(matrix.begin(), matrix.end()); for (int i = 0; i < n; i++) { for (int j = i+1; j < n; j++) { swap(matrix[i][j], matrix[j][i]); } } } bool findRotation(vector<vector<int>>& mat, vector<vector<int>>& target) { for (int i = 0; i < 4; i++) { if (equal(mat, target)) return true; rotate(mat); } return false; } }; untitled 很久沒有更新了,主要原因還是因為自己的惰性。這次的旋轉可以透過多開一個陣列存的方式實現。但如果要省空間,也可以 in-place 地做,一種方式是透過對稱性,先把整個矩陣上下翻轉,再沿著主對角線翻轉 (這是順時針,如果要逆時針就是先左右翻轉,再延主對角線翻轉) ...

March 22, 2026

大二下開學 - 簡記

20260223 數值方法看起來學的東西挺硬,不過學點理論的東西也不錯,第一堂課在教 Floating point system 以及一些定理的證明,上課形式是純粹播放幾年前的影片@@。然後教授英文很破 QQ 智慧型汽車導論跟 DSDL 跟我預期的差不多,不過我喜歡林忠緯的上課步調 (雖然是也看影片,不過影片錄的比林智仁好多ㄌ,講解條理分明) 20260224 作業系統還沒真正上課,暫時不評論。想到今年導生宴巫教授又會覺得我很眼熟,但我卻沒什麼理她就覺得挺尷尬的 QQ 我會努力在 OS 拿到 A+ 的,一定會。 高等電腦視覺我退掉了,我不能接受由學生上課,而且教授第一堂課就播了非常多搞笑影片,或許是為了讓上課變有趣,不過影片裡面甚至包括女性露點的畫面 = =。整個印象降到谷底。 20260225 去試著簽簽看 Web 程式設計,張傑帆的課整體很令人放鬆,儘管團隊合作依然令人不悅,我主要希望自己能累積一些實作經驗,而且自己以前蠻喜歡前端的,但學習之路皆半途而廢。 這禮拜沒有 Sato 的代數導論II 太難過了 20260226 重量訓練老師是梁澤敬,前田徑國手,感覺是個很有料的老師。 洪一平機率還沒開始上課,希望以他的步調能把 syllabus 的內容都講完。然後他又提到 Gaussian splatting 跟 inverse filtering ,影像處理的 PTSD 直接竄上來 @@ 別搞事了一平。 llm 不知道是不是這類文章真的變多了,還是我有意去滑,感覺有明顯 AI 生成痕跡的文章隨處可見,有夠噁心,說真的看到就想吐,難道大部分的受眾都不會感到反感嗎? 然後我也很常看到軟體工程師是黃昏職業,什麼世界上90%的工作都會消失blablabla。說實話,我並不是不相信這些說詞,只是我並不喜歡人們做的那些事 (包括寫作、開發),被貶低成一文不值、沒有任何意義。如果今天每個知識形成的過程都被忽略,學習舊知識、舊技能也被視為是迂腐不堪,或是應當被社會淘汰掉的事,這社會將會是災難性地枯燥乏味,時至今日我們依舊沒有看清 LLM 是一個極度擅長文字接龍的工具,忽略幻覺等缺陷,它並不需要為自己的錯誤負責,因此完全信任真的是件很可怕的事。 alone 反正因為某些因素,我有點不想再跟任何認識的人說話。我不喜歡自己的行為舉止、聲音,一切都是。這週我在學校基本上是沉默的,感覺其他人的聲音在自己的耳中都變得刺耳無比。我這學期無論如何都想拿到書卷獎,為此我一定要比系上所有人都還要努力,再也沒有什麼事情比前途、課業、工作、家人與金錢重要了,包括自己的健康、快樂、生活品質,人與人不過是互相利用罷了,為此我只要去建立利益為基礎的關係就好了。 荒謬、瘋狂、自大,我也懷疑自己是否真的這麼想,我知道我說出來的可能是錯的,但我卻沒辦法說服自己那是錯的,因為或許這樣做自己才能真正擁有成果? 真正成功? 成功是什麼?我是誰? 一切疑問都如同許久之前的夢一樣,在熟悉的環境中漾入模糊當中,與焦慮一同折磨著我的每條神經。為了不再悲傷、生氣,我或許該繼續麻痺自己的感受嗎?我不知道,我真的會有好過來的一天嗎?我越來越不期待那一天的到來了,因為自己從未改變,一樣是那個醜惡、懶惰而自我中心的小丑。 nothing changes 不會再有人過來幫我了,儘管我希望有。

February 26, 2026

Leetcode 1356 - Sort Integers by the Number of 1 Bits

Approach 1 搭車時用手機打的w class Solution { public: vector<int> sortByBits(vector<int>& arr) { int n = arr.size(); vector<pair<int,int>> v(n); for (int i = 0; i < n; i++) { v[i] = std::make_pair(__builtin_popcount(arr[i]), arr[i]); } sort(v.begin(), v.end()); for (int i = 0; i < n; i++) { arr[i] = v[i].second; } return arr; } }; Approach 2 使用 Brian Kernighan’s algorithm 來更有效率的找出一個數的 set bit 有幾個 (即 hamming weight)。 class Solution { public: static int findWeight(int x) { int res = 0; while (x) { res++; x &= (x-1); } return res; } static bool cmp(int a, int b) { if (findWeight(a) == findWeight(b)) { return a < b; } return findWeight(a) < findWeight(b); } vector<int> sortByBits(vector<int>& arr) { sort(arr.begin(), arr.end(), cmp); return arr; } };

February 25, 2026

Leetcode 1022 - Sum of Root to Leaf Binary Numbers

今天用Python寫 :p Code # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: ans = 0 def traverse(self, root, cur): if not root: return if not root.left and not root.right: cur = cur<<1 | root.val self.ans += cur return self.traverse(root.left, cur<<1 | root.val) self.traverse(root.right, cur<<1 | root.val) def sumRootToLeaf(self, root: Optional[TreeNode]) -> int: self.traverse(root, 0) return self.ans

February 24, 2026

LeetCode 693 - Binary Number With Alternating Bits

Approach 1 (brute force) class Solution { public: bool hasAlternatingBits(int n) { int cur = n & 1; for (int i = 1; i < 31; i++) { if ( (n >> i) == 0 ) break; int nxt = (n >> i) & 1; if (nxt == cur) return false; else cur = nxt; } return true; } }; Approach 2 (XOR trick) class Solution { public: bool hasAlternatingBits(int n) { long long x = n ^ (n>>1); return ((x & (x+1)) == 0); } }; Approach 3 (XOR trick too:)) class Solution { public: bool hasAlternatingBits(int n) { unsigned int x = n ^ (n >> 2); return ((x & (x-1)) == 0); } };

February 18, 2026

Leetcode 401 - Binary Watch

Approach 1 - Classic Enumeration 把所有小時、分鐘的組合列出來,然後看他們的 set bit 的數量加起來是不是 turnedOn。這是我一開始想到的作法,此外似乎 C++ 20 就開始有 std::format() 可以用了,還蠻酷的:) 我第一次使用 AC Code class Solution { public: vector<string> readBinaryWatch(int turnedOn) { vector<string> ans; for (int h = 0; h < 12; h++) { for (int m = 0; m < 60; m++) { int t1 = __builtin_popcount(h); int t2 = __builtin_popcount(m); if (t1+t2 == turnedOn) { string time = std::format("{}:{:02}", h, m); ans.push_back(time); } } } return ans; } }; Approach 2 - Binary Enumeration 這是官解提供的作法之一,因為小時跟分鐘加起來有 10 個 bits,所以有 2^10=1024 種可能性,我們可以透過枚舉 0~1024 的 i,i 從 MSB 開始數四位是小時,剩下六位是分鐘,透過基本的位元運算以及 Approach 1 的一些觀念就可以做出來了。anyway 這題非常簡單,依然是不用動腦的 Easy 類別。 ...

February 17, 2026

Leetcode 67 - Add Binary

Solution class Solution { public: string addBinary(string a, string b) { int n = a.length(), m = b.length(); int k = max(n, m); if (n < m) { for (int i = 0; i < m-n; i++) a = "0" + a; } else { for (int i = 0; i < n-m; i++) b = "0" + b; } string res = ""; int carry = 0; for (int i = k-1; i >= 0; i--) { int x = a[i]-'0', y = b[i]-'0'; int sum = x + y + carry; if (sum == 0) res = "0" + res, carry = 0; else if (sum == 1) res = "1" + res, carry = 0; else if (sum == 2) res = "0" + res, carry = 1; else if (sum == 3) res = "1" + res, carry = 1; } if (carry) res = "1" + res; return res; } }; ...

February 15, 2026