/*
 * Algorithm: Two pointers on two sorted vectors.
 * 演算法：對兩個已排序的 vector 使用雙指針，O(n+m) 時間、O(1) 空間。
 * Identical logic to the C version, written in idiomatic C++.
 */

class Solution {
public:
    // vector<int>& 是「對 vector 的參考」，避免複製整個陣列（更快）
    // vector<int>& is a reference to the vector — avoids copying the whole array
    int getCommon(vector<int>& nums1, vector<int>& nums2) {
        // size_t 是 vector::size() 的回傳型別（非負整數）
        // size_t is the unsigned integer type returned by vector::size()
        size_t i = 0, j = 0;

        // .size() 回傳元素個數；只要兩邊都還有元素就繼續
        // .size() returns element count; continue while both have elements left
        while (i < nums1.size() && j < nums2.size()) {
            if (nums1[i] == nums2[j]) {
                // 找到第一個相等值，因為從小到大掃描，這就是最小共同值
                // First equal pair is the minimum common value (ascending scan)
                return nums1[i];
            }
            // 三元判斷哪一邊比較小，將該邊的指針前進
            // Advance the pointer that points to the smaller value
            if (nums1[i] < nums2[j]) {
                ++i;   // 前綴 ++ 比後綴稍微高效（對 int 沒差，但是好習慣）
                       // Pre-increment is a stylistic habit in C++
            } else {
                ++j;
            }
        }

        // 沒找到共同值 / no common value found
        return -1;
    }
};
