TODO:

20261031

20261030

20261029

20261028

20261027

22061026

class Solution:
    def calculateScore(self, a: int, b: int) -> int:
        def count_set_bits(n: int) -> int:
            n_str = bin(n)[2:]
            return sum(int(c) for c in n_str)

        s1 = count_set_bits(a)
        s2 = count_set_bits(b)
        if s2 <= s1:
            return s1 * s2
        return s2 // s1
class Solution:
    def calculateScore(self, a: int, b: int) -> int:
        s1 = a.bit_count()
        s2 = b.bit_count()
        if s2 <= s1:
            return s1 * s2
        return s2 // s1
class Solution:
    def maximizeSum(self, nums: list[int], k: int) -> int:
        improvements = []

        for num in nums:
            max_num = int("".join(sorted(str(num), reverse=True)))
            improvements.append(max_num - num)

        improvements.sort(reverse=True)
        return sum(nums) + sum(improvements[:k])
import heapq


class Solution:
    def maximizeSum(self, nums: list[int], k: int) -> int:
        improvements = []

        for num in nums:
            max_num = int("".join(sorted(str(num), reverse=True)))
            improvements.append(max_num - num)

        best_improvements = heapq.nlargest(k, improvements)
        return sum(nums) + sum(best_improvements)

20261025

20261024

20261023

20261022

20261021

20261020

20261019

20261018

class Solution:
    def findColoredRoot(self, edges: list[list[int]], colors: str) -> int:
        next_color = {"R": "B", "B": "W", "W": "R"}
        in_degree = [0] * len(colors)
        out_degree = [0] * len(colors)

        for u, v in edges:
            if next_color[colors[u]] == colors[v]:
                parent, child = u, v
            elif next_color[colors[v]] == colors[u]:
                parent, child = v, u
            else:  # same color on both ends
                return -1

            in_degree[child] += 1
            out_degree[parent] += 1
            if in_degree[child] > 1 or out_degree[parent] > 2:
                return -1

        return in_degree.index(0)

20261017

I’ve been mulling over what ot get myself for my birthday. I was thinking of getting Garmin Fenix 9 Pro Solar, Fitbit Air, or Fitbit Edge, but I decided to get some new keyboards instead.

20261016

20261015

20261014

#include <cctype>
#include <cstddef>
#include <string>

class Solution {
public:
    bool isPalindrome(std::string s) {
        std::size_t left = 0;
        std::size_t right = s.size() - 1;
        while (left < right) {
            if (!std::isalnum(s[left])) {
                ++left;
                continue;
            }
            if (!std::isalnum(s[right])) {
                --right;
                continue;
            }

            if (std::tolower(s[left]) != std::tolower(s[right])) {
                return false;
            }
            ++left;
            --right;
        }
        return true;
    }
};

20261013

I’m cold… sleepy…

20261012

class Solution {
public:
    bool isPalindrome(int x) {
        int original = x;
        long long reversed = 0;
        while (0 < x) {
            reversed = reversed * 10 + (x % 10);
            x /= 10;
        }

        return reversed == original;
    }
};
class Solution {
public:
    bool isPalindrome(int x) {
        if (x < 0) { return false; }

        int num_digits = 0;
        int x_copy = x;
        while (0 < x_copy) {
            x_copy /= 10;
            ++num_digits;
        }

        int reversed_last = 0;
        for (int i = 0; i < num_digits / 2; ++i) {
            reversed_last = reversed_last * 10 + (x % 10);
            x /= 10;
        }

        if (num_digits % 2 != 0) {
            x /= 10;
        }
        return reversed_last == x;
    }
};
class Solution {
public:
    bool isPalindrome(int x) {
        if (x < 0) { return false; }
        // Without this, leading zeros will be discarded incorrectly
        // in the following operations
        if (x != 0 && x % 10 == 0) { return false; }

        int reversed_last = 0;
        while (reversed_last < x) {
            reversed_last = reversed_last * 10 + (x % 10);
            x /= 10;
        }
        return reversed_last == x || reversed_last / 10 == x;
    }
};

20261011

#include <cstddef>
#include <string>

class Solution {
public:
    bool isPalindrome(int x) {
        std::string s = std::to_string(x);
        std::size_t left = 0;
        std::size_t right = s.length() - 1;
        while (left < right) {
            if (s[left] != s[right]) { return false; }
            ++left;
            --right;
        }

        return true;
    }
};

20261010

#include <algorithm>
#include <vector>

class Solution {
public:
    int missingNumber(std::vector<int>& nums) {
        std::sort(nums.begin(), nums.end());
        for (int i = 0; i < nums.size(); ++i) {
            if (nums[i] != i) { return i; }
        }
        return static_cast<int>(nums.size());
    }
};
#include <vector>
#include <numeric>

class Solution {
public:
    int missingNumber(std::vector<int>& nums) {
        int total = std::accumulate(nums.begin(), nums.end(), 0);
        int n = static_cast<int>(nums.size());
        int expected = (n * (n + 1)) / 2;
        return expected - total;
    }
};

20261009

I went on a date with my wife at Valley Fair Mall

#include <string>

class Solution {
public:
    int lengthOfLastWord(std::string s) {
        int i = static_cast<int>(s.size() - 1);
        while (0 <= i && s[i] == ' ') { --i; }
        int length = 0;
        while (0 <= i && s[i] != ' ') {
            --i;
            ++length;
        }
        return length;
    }
};

20261008

My wife passed all 4 exams of CPA! Congratulations! Now I’ll be a stay-at-home-husband…

20261007

20261006

I felt a little envious when someone else in my study group got a job at a Big Tech company, but on second thought, health is what matters most, and I have a lovely wife.

20261005

20261004

20261003

Today was the most relaxing day I’ve had in a while.

20261002

I ate too much in the last 3 days. will try to go back to the normal state

Today’s Stretches

Daily Reps

class Solution {
public:
    bool isPowerOfFour(int n) {
        if (n <= 0) { return false; }
        if (n == 1) { return true; }
        if (n % 4 != 0) { return false; }
        return isPowerOfFour(n / 4);
    }
};

20261001

Today’s Stretches

Daily Reps

class Solution {
public:
    bool isPowerOfTwo(int n) {
        if (n <= 0) { return false; }

        while (1 < n) {
            if (n % 2 == 1) { return false; }
            n = n / 2;  // n /= 2;
        }

        return true;
    }
};
class Solution {
public:
    bool isPowerOfTwo(int n) {
        if (n <= 0) { return false; }
        return (n & (n - 1))  == 0;
    }
};
class Solution {
public:
    bool isPowerOfFour(int n) {
        const int MASK = 0x55555555;

        if (n <= 0) { return false; }
        if ((n & MASK) != n) { return false; }
        return (n & (n - 1)) == 0;
    }
};

index 202609 202611