20251130

The end of November

MEMO: LeetCode 88. Merge Sorted Array

#include <vector>

class Solution {
public:
    void merge(std::vector<int>& nums1, int m, std::vector<int>& nums2, int n) {
        int insert_index = m + n - 1;
        int index1 = m - 1;
        int index2 = n - 1;
        while (0 <= index2) {
            if (index1 < 0 || nums1[index1] < nums2[index2]) {
                nums1[insert_index] = nums2[index2];
                --insert_index;
                --index2;
            } else {
                nums1[insert_index] = nums1[index1];
                --insert_index;
                --index1;
            }
        }
    }
};

20251129

20251128

20251127

20251126

Amazon VPC (Virtual Private Cloud)

Why so many americans use strange abbreviations. It is super confusing.

20251125

20251124

What is ETL? - Extract Transform Load Explained - AWS

Extract, transform, and load (ETL) is the process of combining data from multiple sources into a large, central repository called a data warehouse. ETL uses a set of business rules to clean and organize raw data and prepare it for storage, data analytics, and machine learning (ML).

MEMO: LeetCode 94. Binary Tree Inorder Traversal

#include <vector>

class Solution {
private:
    void InorderTraversalHelper(const TreeNode* node, std::vector<int>& result) {
        if (node == nullptr) {
            return;
        }
        InorderTraversalHelper(node->left, result);
        result.push_back(node->val);
        InorderTraversalHelper(node->right, result);
    }

public:
    std::vector<int> inorderTraversal(const TreeNode* root) {
        std::vector<int> result;
        InorderTraversalHelper(root, result);
        return result;
    }
};

MEMO: LeetCode 290. Word Pattern

class Solution:
    def wordPattern(self, pattern: str, s: str) -> bool:
        letters: list[str] = list(pattern)
        words: list[str] = s.split(" ")
        if len(letters) != len(words):
            return False

        letter_to_word: dict[str, str] = {}
        word_to_letter: dict[str, str] = {}
        for letter, word in zip(letters, words):
            if letter not in letter_to_word and word not in word_to_letter:
                letter_to_word[letter] = word
                word_to_letter[word] = letter
                continue

            if letter_to_word.get(letter) == word and word_to_letter.get(word) == letter:
                continue

            return False

        return True
#include <ranges>
#include <map>
#include <string>

class Solution {
public:
    bool wordPattern(std::string pattern, std::string s) {
        std::vector<char> letters;
        for (const auto& letter : pattern) {
            letters.push_back(letter);
        }
        std::vector<std::string> words;
        auto word_ranges = std::views::split(s, ' ');
        for (const auto& word_range : word_ranges) {
            words.push_back(std::string(word_range.begin(), word_range.end()));
        }

        if (letters.size() != words.size()) {
            return false;
        }

        std::map<char, std::string> letter_to_word;
        std::map<std::string, char> word_to_letter;
        for (int i = 0; i < letters.size(); ++i) {
            char letter = letters[i];
            std::string word = words[i];
            if (!letter_to_word.contains(letter) && !word_to_letter.contains(word)) {
                letter_to_word[letter] = word;
                word_to_letter[word] = letter;
                continue;
            }
            if (
                letter_to_word.contains(letter)
                && letter_to_word[letter] == word
                && word_to_letter.contains(word)
                && word_to_letter[word] == letter
            ) {
                continue;
            }
            return false;
        }
        return true;
    }
};

20251122 (My wife’s birthday) - 20251123

20251121

20251120

If domain is not set:

20251119

LeetCode 14. Longest Common Prefix

I could simplify the while loop in yesterday’s solution, but the name check_index is confusing. I will try to come up with a better name tomorrow.

The problem is that i used the name check_index to mean that [:check_index] has already been checked, and [check_index + 1:] is still pending. But the meaning breaks around incremenet/return operations.

#include <string>
#include <vector>

class Solution {
public:
    string longestCommonPrefix(std::vector<std::string>& strs) {
        size_t check_index = 0;
        bool matched = true;
        while (matched) {
            char matching_char = '\0';
            for (std::string str : strs) {
                if (str.size() <= check_index) {
                    matched = false;
                    break;
                }
                if (matching_char == '\0') {
                    matching_char = str[check_index];
                    continue;
                }
                if (str[check_index] != matching_char) {
                    matched = false;
                    break;
                }
            }
            ++check_index;
        }
        return strs[0].substr(0, check_index - 1);
    }
};

LeetCode 58. Length of Last Word

#include <string>

class Solution {
public:
    int lengthOfLastWord(std::string s) {
        int non_space_count = 0;
        for (int i = 0; i < s.size(); ++i) {
            if (s[i] == ' ') {
                if (i + 1 < s.size() && s[i] != s[i + 1] ) {
                    non_space_count = 0;
                }
                continue;
            }

            ++non_space_count;
        }

        return non_space_count;
    }
};

20251118

#include <iostream>
#include <string>
#include <vector>

class Solution {
public:
    std::string longestCommonPrefix(std::vector<std::string>& strs) {
        size_t check_index = 0;
        while (true) {
            char matching_char = '\0';
            bool match = true;
            for (std::string str : strs) {
                if (str.size() <= check_index) {
                    match = false;
                    break;
                }
                if (matching_char == '\0') {
                    matching_char = str[check_index];
                    continue;
                }
                if (str[check_index] != matching_char) {
                    match = false;
                    break;
                }
            }
            ++check_index;
            if (!match) {
                break;
            }
        }

        return strs[0].substr(0, check_index - 1);
    }
};

20251117

#include <utility>

class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        int unique_index = 0;
        int i = 0;
        while (i < nums.size()) {
            while (i + 1 < nums.size() && nums[i] == nums[i + 1]) {
                ++i;
            }
            std::swap(nums[unique_index], nums[i]);
            ++i;
            ++unique_index;
        }
        return unique_index;
    }
};

20251116

20251111-20251115

20251110

20251109

I didn’t feel well (cough), so I took some rest

20251108

20251107

20251106

20251105

20251104

20251103

20251101-20251102


TODO:


index 202510 202512