LeetCode 2605: Form Smallest Number From Two Digit Arrays Solution
Master LeetCode problem 2605 (Form Smallest Number From Two Digit Arrays), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
2605. Form Smallest Number From Two Digit Arrays
Problem Explanation
Explanation
To form the smallest number that contains at least one digit from each array, we need to select the smallest digit from both arrays. We can achieve this by sorting the digits in ascending order and then concatenating the smallest digit from each array.
- Sort both arrays in ascending order.
- Select the smallest digit from the sorted arrays.
- Concatenate the smallest digits to form the smallest number.
Time Complexity: O(n log n) where n is the total number of digits in both arrays, due to sorting.
Space Complexity: O(n) for storing the sorted arrays.
Solution Code
class Solution {
public String formSmallestNumber(int[] nums1, int[] nums2) {
Arrays.sort(nums1);
Arrays.sort(nums2);
return String.valueOf(Math.min(nums1[0], nums2[0])) + Math.max(nums1[0], nums2[0]);
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 2605 (Form Smallest Number From Two Digit Arrays)?
This page provides optimized solutions for LeetCode problem 2605 (Form Smallest Number From Two Digit Arrays) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 2605 (Form Smallest Number From Two Digit Arrays)?
The time complexity for LeetCode 2605 (Form Smallest Number From Two Digit Arrays) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 2605 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2605 in Java, C++, or Python.