Problem Description
Given two arrays of unique digits nums1 and nums2, return the smallest number that contains at least one digit from each array. If a digit exists in both arrays, it alone qualifies as the smallest possible number.
Key Insights
- Check for a common digit between the two arrays.
- If a common digit exists, the smallest common digit is the answer.
- If no common digit is found, construct the smallest possible two-digit number using the smallest digit from each array.
- With each array containing at most 9 unique digits, iterating over possible digits is efficient.
Space and Time Complexity
Time Complexity: O(1) (operations are bounded by a constant, as the maximum digits are 9 in each array) Space Complexity: O(1) (only a few additional variables are used)
Solution
First, convert both arrays to sets and check for any common digits. If a common digit exists, return the smallest one from the intersection. Otherwise, find the smallest digit from each array, and combine them in increasing order (i.e., if min1 < min2, combine as min1 followed by min2; otherwise, use the reverse) to form the smallest two-digit number. This approach leverages simple set operations and comparison logic.