Python 2605.py
class Solution:
def minNumber(self, nums1: List[int], nums2: List[int]) -> int:
# single digit
both = set(nums1) & set(nums2)
if both:
return min(both)
# two digits
a = min(nums1)
b = min(nums2)
if a < b:
return a * 10 + b
else:
return b * 10 + a
Difficulty
and has an acceptance rate of about 55% (90k+ submissions).
Typical patterns to solve this problem:
- Array
- Enumeration
- Hash Table
AI Disclaimer
However, two years later I set up this website and used various AI tools to generate most of the walkthrough / explanations below.
See this dedicated page for more details.
Code Walkthrough
Overview
The provided Solution class implements a method minNumber that receives two integer lists, nums1 and nums2. Its purpose is to compute the smallest possible positive integer that can be formed using digits taken from the two lists, under the constraint that at most two digits are used. The algorithm first checks for a common digit that can serve as a single-digit answer; if none exists, it constructs the smallest two-digit number by arranging the minimal elements from each list.
Key Steps
Detecting a shared digit
both = set(nums1) & set(nums2)
if both:
return min(both)
The method converts both input lists into sets and computes their intersection. If the intersection is non-empty, the smallest element of that intersection is returned. This handles the case where a single digit appears in both lists, which is the minimal possible result because any two-digit number would be larger than a single digit.
Selecting the minimal elements from each list
a = min(nums1)
b = min(nums2)
When no common digit exists, the algorithm extracts the smallest value from each list. These two values are the only candidates needed to form the minimal two-digit number because any larger digit from either list would increase the resulting value.
Forming the two-digit number
if a < b:
return a * 10 + b
else:
return b * 10 + a
The two minimal digits are placed in the tens and units positions to produce the smallest possible two-digit integer. The conditional ensures that the smaller digit occupies the tens place, guaranteeing the minimal numeric value.
Edge Cases Handled
When the input lists contain a digit that appears in both, the intersection step captures this scenario and returns that digit directly, avoiding unnecessary two-digit construction.
If one list consists solely of larger digits while the other contains a single small digit, the min calls still correctly identify the smallest element from each list, and the conditional placement guarantees the correct ordering regardless of which list provides the smaller digit.
The algorithm also works when the lists contain duplicate values; converting to sets for the intersection eliminates redundancy without affecting correctness.
If either list is empty, the min function would raise an exception, but the problem constraints guarantee non-empty input, so the code safely assumes valid data.
Performance Optimizations
The solution leverages set intersection to detect a common digit in linear time relative to the combined size of the input lists, avoiding nested loops that would increase the operation count.
By using the built-in min function, the code benefits from highly optimized native implementations for finding the smallest element, which is faster than manual iteration.
The conditional that decides the digit order performs only a single comparison, ensuring that the final numeric construction is executed with minimal overhead.
Overall, the method executes a constant amount of work after the initial set creation and two min calls, leading to an efficient solution for the problem size.
ELI5 - Simplified Explanation
Imagine there are two baskets, each filled with different numbered blocks. The goal is to make the smallest number possible using one block from each basket.
First, look to see if any number is inside both baskets at the same time. If there is a matching number, the smallest matching one is the winner because it can be used alone to make a tiny number.
If there are no matching numbers, pick the smallest block from the first basket and the smallest block from the second basket. To build a new number, put the smaller of these two blocks in the tens place and the other block in the ones place. For example, if there is a two and a five, putting them together makes twenty-five, which is smaller than fifty-two. Following this simple rule always creates the smallest possible combination.
Code Quality
Complexity Analysis
As a rule of thumb, the maximum input size for one second execution time is
- factorial: up to 10 elements
- exponential: up to 20 elements
- polynomial: a few thousand elements
- loglinear: up to 100,000 elements
- linear: a few million elements
- logarithmic: a billion elements
- constant: practically unlimited
Other Solutions
Obviously, I did that after I solved the problem myself.
In general, AlgoMonster and WalkCCC solutions are high-quality and always worth a look.
I haven't checked most of the other solutions, but they might be helpful as well.
And of course, the LeetCode forum is always a good place (or even the best) to look for alternative approaches.
(click on a column header to sort the table)
| author | ||||||||
|---|---|---|---|---|---|---|---|---|
| author | solutions | |||||||
| www | AlgoMonster | Java | Python | C++ | TS | |||
| WalkCCC | Java | Python | C++ | |||||
| leetcode.ca | Java | Python | C++ | Go | Rust | TS | ||
| Progiez | Java | Python | C++ | |||||
| github | charles-wangkai | Java | ||||||
| fsq | C++ | |||||||
| JoshCrozier | JS | |||||||
| lkwq007 | Python | |||||||
| varunu28 | Java | |||||||
| AlgoMonster | C++, Python, Java, TypeScript | |||||||
| WalkCCC | C++, Python, Java | |||||||
| leetcode.ca | C++, Go, Java, Python, Rust, TypeScript | |||||||
| Progiez | C++, Java, Python | |||||||
| charles-wangkai | Java | |||||||
| fsq | C++ | |||||||
| JoshCrozier | JavaScript | |||||||
| lkwq007 | Python | |||||||
| varunu28 | Java |
Videos
Solved On The Same Day
- problem 1317: Convert Integer to the Sum of Two No-Zero Integers
- problem 1417: Reformat The String
- problem 1556: Thousand Separator
- problem 1790: Check if One String Swap Can Make Strings Equal
- problem 2259: Remove Digit From Number to Maximize Result
- problem 2582: Pass the Pillow
- problem 2729: Check if The Number is Fascinating
- problem 881: Boats to Save People
- problem 1503: Last Moment Before All Ants Fall Out of a Plank
- problem 1578: Minimum Time to Make Rope Colorful
- problem 1685: Sum of Absolute Differences in a Sorted Array
- problem 1701: Average Waiting Time
Extra
- Tech Stack - my development environment and tools
- AI - how AI helped me create this website (NOT solving the problems)
- Progress - progression over time
- Quality - code quality