1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| class Solution { public: int smallestDifference(vector<int>& a, vector<int>& b) { int m = a.size(), n = b.size(); sort(a.begin(), a.end()); sort(b.begin(), b.end()); int pa = 0, pb = 0; long ret = LONG_MAX; while(pa < m && pb < n) { ret = min(ret, abs((long)a[pa] - (long)b[pb])); if(a[pa] < b[pb]) ++pa; else ++pb; } return ret; } };
|