Open3

Get started Leet Code

KenjiKenji

Purpose

I’m using LeetCode to practice thinking about code in English and to strengthen my problem-solving skills.

30-Day Goals

Objective

Establish a consistent English-first coding habit.

Key Results

  • ≥ 20 sessions (5×/week, 15–25 min each)
  • ≥ 16 ACs (≈ 12 Easy, 4 Medium)
  • For every session, write a 3-line English note: Restate / Idea / Complexity
  • Be able to explain 3 core patterns: Two Pointers, Sliding Window, Binary Search
  • Metrics: median time-to-first-AC ≤ 20 min; editorial reliance ≤ 30%; 15+ new terms added to a personal glossary
KenjiKenji

Convert Integer to the Sum of Two No-Zero Integers

No-Zero integer is a positive integer that does not contain any 0 in its decimal representation.
Given an integer n, return a list of two integers [a, b] where:

  • a and b are No-Zero integers.
  • a + b = n
    The test cases are generated so that there is at least one valid solution. If there are many valid solutions, you can return any of them.
impl Solution {
    pub fn get_no_zero_integers(n: i32) -> Vec<i32> {
        // Helper: returns true if x has no '0' digit in decimal.
        fn no_zero(mut x: i32) -> bool {
            // Scan digits from least significant to most.
            while x > 0 {
                // If any digit is 0, it's invalid.
                if x % 10 == 0 { return false; }
                // Drop the last digit and continue.
                x /= 10;
            }
            // No zeros found → valid.
            true
        }

        // Try all a from 1 up to n-1 (Rust range 1..n is half-open).
        for a in 1..n {
            // Compute b so that a + b = n.
            let b = n - a;
            // Both numbers must be No-Zero integers.
            if no_zero(a) && no_zero(b) {
                // Return the first valid pair we find.
                return vec![a, b];
            }
        }
        // Problem guarantees a solution exists, so this is unreachable.
        unreachable!();
    }
}
KenjiKenji

Memoize

Given a function fn, return a memoized version of that function.

A memoized function is a function that will never be called twice with the same inputs. Instead it will return a cached value.

You can assume there are 3 possible input functions: sum, fib, and factorial.

  • sum accepts two integers a and b and returns a + b. Assume that if a value has already been cached for the arguments (b, a) where a != b, it cannot be used for the arguments (a, b). For example, if the arguments are (3, 2) and (2, 3), two separate calls should be made.
  • fib accepts a single integer n and returns 1 if n <= 1 or fib(n - 1) + fib(n - 2) otherwise.
  • factorial accepts a single integer n and returns 1 if n <= 1 or factorial(n - 1) * n otherwise.
function memoize(fn) {
  const cache = new Map(); // key: 引数配列のJSON文字列, value: 計算結果

  return function(...args) {
    const key = JSON.stringify(args); // [2,3] と [3,2] は別キー
    if (cache.has(key)) return cache.get(key);

    const result = fn(...args);
    cache.set(key, result);
    return result;
  };
}

memoized(メモ化された)関数とは、同じ入力で呼ばれたときに計算をやり直さず、前に計算した結果(キャッシュ)を返す関数のこと