20260719

20260718

20260717

20260716

class Solution:
    def isPowerOfThree(self, n: int) -> bool:
        if n <= 0:
            return False

        return 3 ** 19 % n == 0

20260715

20260714

class Solution:
    def isPowerOfFour(self, n: int) -> bool:
        # 4 ≡ 1 (mod 3)
        # 4^k ≡ 1^k ≡ 1 (mod 3)

        if n <= 0:
            return False

        if n & (n - 1) != 0:
            return False

        return n % 3 == 1

20260713

class Solution:
    def isPowerOfFour(self, n: int) -> bool:
        # 1 -> 1
        # 4 -> 100
        # 16 -> 10000

        if n <= 0:
            return False

        if n & (n - 1) != 0:
            return False

        EVEN_BIT_MASK = 0x55555555
        return n & EVEN_BIT_MASK != 0

20260712

CPU Core
   │
Registers    (~1 KB, fastest)
   │
L1 Cache     (~32–64 KB)
   │
L2 Cache     (~256 KB–2 MB)
   │
L3 Cache     (~8–64 MB)
   │
RAM          (GBs)
   │
SSD / HDD    (TBs)

The number of cache levels in a CPU is a design choice that balances speed, capacity, power, and cost.
class Solution:
    def isPowerOfFour(self, n: int) -> bool:
        return (
            n > 0  # 4 ^ x cannot be negative
            and n & (n - 1) == 0  # exactly one bit is set
            and n & 0x55555555 == n  # 1 bit at even position - 4 ^ x
            # and (n & 0x55555555) != 0  # alternative
        )

20260711

# I know this solution does not pass the test cases
# This is just for my reference
import collections


class Solution:
    def findMinHeightTrees(self, n: int, edges: list[list[int]]) -> list[int]:
        node_to_neighbors = collections.defaultdict(list)
        for node1, node2 in edges:
            node_to_neighbors[node1].append(node2)
            node_to_neighbors[node2].append(node1)

        def get_height(node: int, parent: int) -> int:
            return max(
                (
                    get_height(neighbor, node)
                    for neighbor in node_to_neighbors[node]
                    if neighbor != parent
                ),
                default=0
            ) + 1

        min_height = float("inf")
        min_roots = []
        for node in range(n):
            height = get_height(node, None)
            if height > min_height:
                continue
            elif height < min_height:
                min_height = height
                min_roots = [node]
            else:  # height == min_height
                min_roots.append(node)

        return min_roots

20260710

20250709

20260708

20260707

20250706

20260621-20260705


index 202606 202608