The end of November
operator is a reserved keyword in C++, created a pull
requestMEMO: 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;
}
}
}
};
Amazon VPC (Virtual Private Cloud)
Why so many americans use strange abbreviations. It is super confusing.
carryWhat 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;
}
};
If domain is not set:
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);
}
};
#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;
}
};
size_t is
always an unsigned type, meaning it can only hold non-negative
values.”#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);
}
};
#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;
}
};. for object
itself, -> for a pointer to access membersI didn’t feel well (cough), so I took some rest
std::invalid_argument,
brute-force, map, sorting + two pointersauto [identifier1, identifier2, ...] = expression;std::tuple and
std::pairemplace_back() forwards the arguments to the
constructor<utility> std::pair-O1,
i++ and ++i seem to result in the same
assembly. However, when dealing with objects like iterators,
postincrement adds overhead copying the previous value. preincrement is
always more efficient than postincrement, so it’s better to use
preincrement.
TODO: