Date: 2026-06-06
Time: 17:54
This file solves LeetCode 2540 — Minimum Common Value. It owns a single responsibility: given two sorted integer arrays, find the smallest integer that appears in both. It returns -1 if no common element exists.
mincommonnumber(nums1, nums2) -> intThe sole public function. Contract:
-1 if the intersection is empty.Two-pointer merge scan. This is the canonical pattern for finding common elements in two sorted sequences without extra space. Two indices advance through the arrays in lockstep: the pointer on the smaller value advances, because anything behind it can never match anything ahead in the other array. On equality, we return immediately — the first match is guaranteed to be the minimum because both arrays are scanned left to right.
This is the same merge logic used in merge sort's merge step, but short-circuited on the first match instead of producing a full merged output.
Imports: None — the solution is self-contained, using only built-in list and int.
Imported by: The testsolution.py in the same directory. The long "Imported By" list in the prompt is misleading — those are unrelated test files across the repo that happen to share a common test harness, not direct consumers of mincommon_number.
1. Initialize two pointers i = 0, j = 0.
2. While both pointers are in bounds:
nums1[i] == nums2[j] → return that value (first common, guaranteed minimum).nums1[i] < nums2[j] → increment i to catch up.j to catch up.3. If either pointer runs past the end, no common value exists → return -1.
Each iteration advances at least one pointer, so the loop terminates in at most len(nums1) + len(nums2) steps.
None. The function assumes valid input (two sorted lists of integers). Empty lists are handled correctly — the while condition fails immediately and -1 is returned. There are no exceptions raised or caught.
minimum-common-value/test_solution.py — See which edge cases are tested (empty arrays, no overlap, single-element arrays, duplicates)minimum-common-value/plan.md — Check whether alternative approaches (binary search, set intersection) were considered and why two-pointer was chosenintersection-of-two-arrays/solution.py — Compare with the set-based approach for unsorted arrays to see when two-pointer vs hashing is preferredtwo-pointer-on-sorted-arrays — The same merge-scan pattern appears in problems like merge sorted lists, intersection of sorted arrays, and median of two sorted arraystwo-pointer-linear-time — mincommonnumber runs in O(n + m) time and O(1) space where n and m are the input lengthssorted-precondition-not-validated — The function assumes both inputs are sorted but does not check or enforce this; unsorted input produces silently wrong resultsearly-return-guarantees-minimum — The first equality found by the left-to-right scan is necessarily the smallest common value, so the function returns immediately without scanning the restempty-input-returns-negative-one — Passing one or both empty lists correctly returns -1 via the while-loop guard, not via a special case