// 演算法與 C 版相同：用 %10 / /10 拆數字，維護 max1（最大）與 max2（次大），
// 回傳 max1 * max2。這裡用 std::max 讓程式更簡潔。
// Same algorithm as the C version: peel digits with %10 and /10, keep max1 and max2,
// return max1 * max2. Uses std::max for a cleaner style.
class Solution {
public:
    int maxProduct(int n) {
        int max1 = 0, max2 = 0;      // 最大、次大數字 / largest and second-largest digit
        while (n > 0) {              // 逐位處理，直到 n 變 0 / process each digit until n hits 0
            int d = n % 10;          // 取出個位數 / extract the units digit
            if (d > max1) {          // d 超越目前最大 / d is a new maximum
                max2 = max1;         // 原最大退為次大 / demote old max to second place
                max1 = d;            // d 成為新最大 / d becomes the new max
            } else {                 // 否則只可能更新次大 / otherwise it can only affect second place
                max2 = std::max(max2, d);  // 取 max2 與 d 的較大者 / keep the larger of the two
            }
            n /= 10;                 // 移除已處理的個位 / chop off the processed digit
        }
        return max1 * max2;          // 頂端兩個數字的乘積 / product of the top two digits
    }
};
