[LeetCode] Form Smallest Number From Two Digit Arrays

2605. Form Smallest Number From Two Digit Arrays

Given two arrays of unique digits nums1 and nums2, return the smallest number that contains at least one digit from each array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public:
int minNumber(vector<int>& nums1, vector<int>& nums2) {
unordered_set<int> us1(begin(nums1), end(nums1)), us2(begin(nums2), end(nums2));
int res = 100;
for(int i = 1; i <= 9; i++) {
if(!us1.count(i)) continue;
for(int j = 1; j <= 9; j++) {
if(!us2.count(j)) continue;
if(i == j) res = min(res, j);
else res = min({res, i * 10 + j, j * 10 + i});
}
}
return res;
}
};

Author: Song Hayoung
Link: https://songhayoung.github.io/2023/04/02/PS/LeetCode/form-smallest-number-from-two-digit-arrays/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.