LC 2905 - Find Indices With Index and Value Difference II

[translate by ChatGPT] Although my algorithm was already O(n), I realized I was doing a lot of unnecessary work during the process. I didn’t actually need to compute the maximum and minimum for every previous position. As soon as I find a valid answer, I can return immediately, which saves a lot of time. I don’t need to store the maximum number itself — I can just keep track of the index and look it up later. I suspect using a vector to store extra values introduces some overhead. ...

March 7, 2025 · 2 min

LC 2429 - Minimize XOR

[translate by ChatGPT] At first I didn’t fully understand the problem, but later I figured it out. We need to count how many 1’s are in the binary representation of num2, then find a number ans that has the same number of 1’s and minimizes the XOR value between num1 and ans. My initial idea was: after counting the number of set bits, match all the 1-bits in num1 first, and then fill the remaining bits starting from the smallest positions. ...

January 15, 2025 · 2 min

LC 2657 - Find the Prefix Common Array of Two Arrays

[translate by ChatGPT] Just maintain two caches, count if the character B[i] is in cache of A, and do the same thing reversely. Code class Solution { public: vector<int> findThePrefixCommonArray(vector<int>& A, vector<int>& B) { vector<bool> cacheA(50, false); vector<bool> cacheB(50, false); int ans = 0; vector<int> ansAry; for(int i = 0 ; i < A.size() ; ++i) { if(cacheA[B[i]]) ++ans; if(cacheB[A[i]]) ++ans; if(A[i] == B[i]) ++ans; cacheA[A[i]] = true; cacheB[B[i]] = true; ansAry.push_back(ans); } return ansAry; } };

January 15, 2025 · 1 min

LC 1769 - Counting Words With a Given Prefix

[translate by ChatGPT] This was practice using a lambda functionm abd count_if, and accumulate. Code class Solution { public: vector<int> minOperations(string boxes) { // ans(i) = sum of |j-i|, for each boxes(j) = 1 // |j-i| = (j-i) if (j>i) else (i-j) // ans(0) = sum of j, for each boxes(j) = 1 int cntL = 0; int cntR = std::count_if(boxes.begin(), boxes.end(), [](char value) {return value =='1';}); int sum = std::accumulate(boxes.begin(), boxes.end(), 0, [&, index = 0](int acc, char value) mutable { int result = acc; if (value == '1') { result += index; } ++index; // Increment the index return result; }); vector<int> ans; for(int i = 0 ; i < boxes.size() ; ++i){ ans.push_back(sum); if(boxes[i] == '1') { ++cntL; --cntR; } sum -= cntR; sum += cntL; } return ans; } };

January 9, 2025 · 1 min

LC 1930 - Unique Length-3 Palindromic Subsequences

Approach [Translate by ChatGPT] The task is to find all palindromic subsequences of length 3. My first intuition was that we only need to count possibilities, because with length 3 the first and last characters must be the same. Initially, I thought about scanning twice and maintaining counts for the prefix (0 → i) and suffix (i → end). However, that would require a 26 × n array. Later I realized I only needed to maintain one side, and update the other side while computing the answer, which reduces the space to just 26. This actually feels similar to the time–space trade-off mindset in hardware design. ...

January 9, 2025 · 2 min

LC 1408 - String Matching in an Array

[translate by ChatGPT] Here we need to check pairs to find how many strings are substrings of others, so I compared them pairwise. However, I noticed that some people first sort the strings by length, so they only need to compare in one direction. Code class Solution { public: vector<string> stringMatching(vector<string>& words) { const int n = words.size(); unordered_set<int> ansIdx; for(int i = 0 ; i < n ; ++i) { for(int j = i+1 ; j < n ; ++j) { string& s1 = words[i]; string& s2 = words[j]; if (isSubString(s2, s1)) ansIdx.insert(i); if (isSubString(s1, s2)) ansIdx.insert(j); } } vector<string> ans; for(int i = 0 ; i < n ; ++i) { if (ansIdx.find(i) != ansIdx.end()) { ans.push_back(words[i]); } } return ans; } bool isSubString(string s1, string s2) { return s1.find(s2) != string::npos; } };

January 7, 2025 · 1 min

LC 3411 - Maximum Subarray With Equal Products

[translate by ChatGPT] I finally joined the weekly contest again today. Previously, I think I only managed to solve all four problems once. This time I thought I should at least be able to solve two within the time limit, but maybe because I hadn’t practiced for a while and wasn’t as familiar, I ended up only getting the second problem correct. Actually, my ideas for the first and third problems were correct. I just didn’t pay enough attention to some details, or I didn’t handle them carefully, which caused me to waste a lot of time. ...

January 5, 2025 · 2 min

LC 3412 - Find Mirror Score of a String

[translate by ChatGPT] This problem was fairly easy and intuitive. I maintained a deque of indices for each character, and while scanning the string, I searched for the most recent occurrence of its mirror character. During the process, I made a small mistake: I accidentally wrote pop_front instead of pop_back. But I caught that bug quickly. Code class Solution { public: long long calculateScore(string s) { vector<deque<int>> front(26,deque<int>()); // the last index for a char long long score = 0; for(int i = 0 ; i < s.size() ; ++i) { const char c = s[i]; deque<int>& lastMirror = front[25-int(c-'a')]; if (lastMirror.size() != 0) { int nearest = lastMirror.back(); score += (i - nearest); lastMirror.pop_back(); } else { front[int(c-'a')].push_back(i); } } return score; } }; Code: Rewrite in 2026-02-08 class Solution { public: long long calculateScore(string s) { vector<stack<int>> v(26); // char(int) -> idx int n = s.size(); long long score = 0; for(int i = 0 ; i < n ; ++i) { int id = s[i] - 'a'; int rId = 25 - id; if(v[rId].size() != 0) { int prevId = v[rId].top(); v[rId].pop(); score += (i - prevId); } else { v[id].push(i); } } return score; } };

January 5, 2025 · 1 min

LC 983 - Minimum Cost For Tickets

Approach [translate by ChatGPT] This problem gives a list of travel days (days) and the costs of 1-day, 7-day, and 30-day passes. We need the minimum cost to cover all travel days. This clearly suggests a DP solution. My first idea was to maintain an ansAry where each entry stores the optimal cost up to day i. For each day, we check day-1, day-7, and day-30 and take the minimum. ...

January 4, 2025 · 3 min