5
5
## 题目描述
6
6
7
7
<!-- 这里写题目描述 -->
8
+
8
9
<p >给定两个整数数组<code >a</code >和<code >b</code >,计算具有最小差绝对值的一对数值(每个数组中取一个值),并返回该对数值的差</p >
9
10
<p ><strong >示例:</strong ></p >
10
11
<pre ><strong >输入:</strong >{1, 3, 15, 11, 2}, {23, 127, 235, 19, 8}
21
22
22
23
<!-- 这里可写通用的实现逻辑 -->
23
24
25
+ ** 方法一:排序 + 二分查找**
26
+
27
+ 我们可以对数组 $b$ 进行排序,并对数组 $a$ 中的每个元素 $x$ 在数组 $b$ 中进行二分查找,找到最接近 $x$ 的元素 $y$,那么 $x$ 和 $y$ 的差的绝对值就是 $x$ 和 $b$ 中最接近 $x$ 的元素的差的绝对值。
28
+
29
+ 时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 是数组 $b$ 的长度。
30
+
31
+ ** 方法二:排序 + 双指针**
32
+
33
+ 我们可以对数组 $a$ 和 $b$ 分别进行排序,然后使用双指针的方法,维护两个指针 $i$ 和 $j$,初始时分别指向数组 $a$ 和 $b$ 的起始位置。每一次,我们计算 $a[ i] $ 和 $b[ j] $ 的差的绝对值,并且更新答案。如果 $a[ i] $ 和 $b[ j] $ 指向的两个元素中的一个元素比另一个元素要小,则将指向较小元素的指针向前移动一步。当至少有一个指针超出数组范围时,遍历结束。
34
+
35
+ 时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 是数组 $a$ 和 $b$ 的长度。
36
+
24
37
<!-- tabs:start -->
25
38
26
39
### ** Python3**
27
40
28
41
<!-- 这里可写当前语言的特殊实现逻辑 -->
29
42
43
+ ``` python
44
+ class Solution :
45
+ def smallestDifference (self , a : List[int ], b : List[int ]) -> int :
46
+ b.sort()
47
+ ans = inf
48
+ n = len (b)
49
+ for x in a:
50
+ j = bisect_left(b, x)
51
+ if j < n:
52
+ ans = min (ans, b[j] - x)
53
+ if j:
54
+ ans = min (ans, x - b[j - 1 ])
55
+ return ans
56
+ ```
57
+
30
58
``` python
31
59
class Solution :
32
60
def smallestDifference (self , a : List[int ], b : List[int ]) -> int :
33
61
a.sort()
34
62
b.sort()
35
63
i = j = 0
36
- res = inf
64
+ ans = inf
37
65
while i < len (a) and j < len (b):
38
- res = min (res, abs (a[i] - b[j]))
39
- if a[i] > b[j]:
40
- j += 1
41
- else :
66
+ ans = min (ans, abs (a[i] - b[j]))
67
+ if a[i] < b[j]:
42
68
i += 1
43
- return res
69
+ else :
70
+ j += 1
71
+ return ans
44
72
```
45
73
46
74
### ** Java**
47
75
48
76
<!-- 这里可写当前语言的特殊实现逻辑 -->
49
77
78
+ ``` java
79
+ class Solution {
80
+ public int smallestDifference (int [] a , int [] b ) {
81
+ Arrays . sort(b);
82
+ long ans = Long . MAX_VALUE ;
83
+ for (int x : a) {
84
+ int j = search(b, x);
85
+ if (j < b. length) {
86
+ ans = Math . min(ans, (long ) b[j] - x);
87
+ }
88
+ if (j > 0 ) {
89
+ ans = Math . min(ans, (long ) x - b[j - 1 ]);
90
+ }
91
+ }
92
+ return (int ) ans;
93
+ }
94
+
95
+ private int search (int [] nums , int x ) {
96
+ int l = 0 , r = nums. length;
97
+ while (l < r) {
98
+ int mid = (l + r) >> 1 ;
99
+ if (nums[mid] >= x) {
100
+ r = mid;
101
+ } else {
102
+ l = mid + 1 ;
103
+ }
104
+ }
105
+ return l;
106
+ }
107
+ }
108
+ ```
109
+
50
110
``` java
51
111
class Solution {
52
112
public int smallestDifference (int [] a , int [] b ) {
53
113
Arrays . sort(a);
54
114
Arrays . sort(b);
55
115
int i = 0 , j = 0 ;
56
- long res = Long . MAX_VALUE ;
116
+ long ans = Long . MAX_VALUE ;
57
117
while (i < a. length && j < b. length) {
58
- res = Math . min(res, Math . abs((long ) a[i] - (long ) b[j]));
59
- if (a[i] > b[j]) {
60
- ++ j;
61
- } else {
118
+ ans = Math . min(ans, Math . abs((long ) a[i] - (long ) b[j]));
119
+ if (a[i] < b[j]) {
62
120
++ i;
121
+ } else {
122
+ ++ j;
63
123
}
64
124
}
65
- return (int ) res ;
125
+ return (int ) ans ;
66
126
}
67
127
}
68
128
```
69
129
70
130
### ** C++**
71
131
132
+ ``` cpp
133
+ class Solution {
134
+ public:
135
+ int smallestDifference(vector<int >& a, vector<int >& b) {
136
+ sort(b.begin(), b.end());
137
+ long long ans = LONG_LONG_MAX;
138
+ for (int x : a) {
139
+ auto it = lower_bound(b.begin(), b.end(), x);
140
+ if (it != b.end()) {
141
+ ans = min(ans, (long long) * it - x);
142
+ }
143
+ if (it != b.begin()) {
144
+ ans = min(ans, x - (long long) * prev(it));
145
+ }
146
+ }
147
+ return ans;
148
+ }
149
+ };
150
+ ```
151
+
72
152
```cpp
73
153
class Solution {
74
154
public:
75
155
int smallestDifference(vector<int>& a, vector<int>& b) {
76
156
sort(a.begin(), a.end());
77
157
sort(b.begin(), b.end());
78
158
int i = 0, j = 0;
79
- long res = LONG_MAX ;
159
+ long long ans = LONG_LONG_MAX ;
80
160
while (i < a.size() && j < b.size()) {
81
- res = min(res, abs((long) a[ i] - (long) b[ j] ));
82
- if (a[ i] > b[ j] )
83
- ++j;
84
- else
161
+ ans = min(ans, abs(1LL * a[i] - 1LL * b[j]));
162
+ if (a[i] < b[j]) {
85
163
++i;
164
+ } else {
165
+ ++j;
166
+ }
86
167
}
87
- return res ;
168
+ return ans ;
88
169
}
89
170
};
90
171
```
91
172
92
173
### ** Go**
93
174
175
+ ``` go
176
+ func smallestDifference (a []int , b []int ) int {
177
+ sort.Ints (b)
178
+ var ans int = 1e18
179
+ for _ , x := range a {
180
+ i := sort.SearchInts (b, x)
181
+ if i < len (b) {
182
+ ans = min (ans, b[i]-x)
183
+ }
184
+ if i > 0 {
185
+ ans = min (ans, x-b[i-1 ])
186
+ }
187
+ }
188
+ return ans
189
+ }
190
+
191
+ func min (a , b int ) int {
192
+ if a < b {
193
+ return a
194
+ }
195
+ return b
196
+ }
197
+ ```
198
+
94
199
``` go
95
200
func smallestDifference (a []int , b []int ) int {
96
201
sort.Ints (a)
97
202
sort.Ints (b)
98
- i, j, res := 0, 0, 2147483647
203
+ i , j := 0 , 0
204
+ var ans int = 1e18
99
205
for i < len (a) && j < len (b) {
100
- res = min(res, abs(a[i]-b[j]))
101
- if a[i] > b[j] {
102
- j++
103
- } else {
206
+ ans = min (ans, abs (a[i]-b[j]))
207
+ if a[i] < b[j] {
104
208
i++
209
+ } else {
210
+ j++
105
211
}
106
212
}
107
- return res
213
+ return ans
108
214
}
109
215
110
216
func abs (a int ) int {
@@ -122,6 +228,55 @@ func min(a, b int) int {
122
228
}
123
229
```
124
230
231
+ ### ** TypeScript**
232
+
233
+ ``` ts
234
+ function smallestDifference(a : number [], b : number []): number {
235
+ b .sort ((a , b ) => a - b );
236
+ let ans = Infinity ;
237
+ const search = (nums : number [], x : number ): number => {
238
+ let [l, r] = [0 , nums .length ];
239
+ while (l < r ) {
240
+ const mid = (l + r ) >> 1 ;
241
+ if (nums [mid ] >= x ) {
242
+ r = mid ;
243
+ } else {
244
+ l = mid + 1 ;
245
+ }
246
+ }
247
+ return l ;
248
+ };
249
+ for (const x of a ) {
250
+ const j = search (b , x );
251
+ if (j < b .length ) {
252
+ ans = Math .min (ans , b [j ] - x );
253
+ }
254
+ if (j > 0 ) {
255
+ ans = Math .min (ans , x - b [j - 1 ]);
256
+ }
257
+ }
258
+ return ans ;
259
+ }
260
+ ```
261
+
262
+ ``` ts
263
+ function smallestDifference(a : number [], b : number []): number {
264
+ a .sort ((a , b ) => a - b );
265
+ b .sort ((a , b ) => a - b );
266
+ let [i, j] = [0 , 0 ];
267
+ let ans = Infinity ;
268
+ while (i < a .length && j < b .length ) {
269
+ ans = Math .min (ans , Math .abs (a [i ] - b [j ]));
270
+ if (a [i ] < b [j ]) {
271
+ ++ i ;
272
+ } else {
273
+ ++ j ;
274
+ }
275
+ }
276
+ return ans ;
277
+ }
278
+ ```
279
+
125
280
### ** ...**
126
281
127
282
```
0 commit comments