Skip to content

2605. Form Smallest Number From Two Digit Arrays 👍

  • Time: $O(81) = O(1)$
  • Space: $O(1)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution {
 public:
  int minNumber(vector<int>& nums1, vector<int>& nums2) {
    int ans = 89;  // the largest num we can have
    for (const int a : nums1)
      for (const int b : nums2)
        ans = min(ans, a == b ? a : min(a, b) * 10 + max(a, b));
    return ans;
  }
};
1
2
3
4
5
6
7
8
9
class Solution {
  public int minNumber(int[] nums1, int[] nums2) {
    int ans = 89; // the largest num we can have
    for (final int a : nums1)
      for (final int b : nums2)
        ans = Math.min(ans, a == b ? a : Math.min(a, b) * 10 + Math.max(a, b));
    return ans;
  }
}
1
2
3
4
5
class Solution:
  def minNumber(self, nums1: list[int], nums2: list[int]) -> int:
    return min(a if a == b else min(a, b) * 10 + max(a, b)
               for a in nums1
               for b in nums2)