Skip to content

Commit 8b61eb2

Browse files
authored
feat: add solutions to lcci problem: No.16.06 (#1770)
No.16.06.Smallest Difference
1 parent 4e75425 commit 8b61eb2

File tree

7 files changed

+428
-102
lines changed

7 files changed

+428
-102
lines changed

lcci/16.06.Smallest Difference/README.md

+179-24
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
## 题目描述
66

77
<!-- 这里写题目描述 -->
8+
89
<p>给定两个整数数组<code>a</code>和<code>b</code>,计算具有最小差绝对值的一对数值(每个数组中取一个值),并返回该对数值的差</p>
910
<p><strong>示例:</strong></p>
1011
<pre><strong>输入:</strong>{1, 3, 15, 11, 2}, {23, 127, 235, 19, 8}
@@ -21,90 +22,195 @@
2122

2223
<!-- 这里可写通用的实现逻辑 -->
2324

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+
2437
<!-- tabs:start -->
2538

2639
### **Python3**
2740

2841
<!-- 这里可写当前语言的特殊实现逻辑 -->
2942

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+
3058
```python
3159
class Solution:
3260
def smallestDifference(self, a: List[int], b: List[int]) -> int:
3361
a.sort()
3462
b.sort()
3563
i = j = 0
36-
res = inf
64+
ans = inf
3765
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]:
4268
i += 1
43-
return res
69+
else:
70+
j += 1
71+
return ans
4472
```
4573

4674
### **Java**
4775

4876
<!-- 这里可写当前语言的特殊实现逻辑 -->
4977

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+
50110
```java
51111
class Solution {
52112
public int smallestDifference(int[] a, int[] b) {
53113
Arrays.sort(a);
54114
Arrays.sort(b);
55115
int i = 0, j = 0;
56-
long res = Long.MAX_VALUE;
116+
long ans = Long.MAX_VALUE;
57117
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]) {
62120
++i;
121+
} else {
122+
++j;
63123
}
64124
}
65-
return (int) res;
125+
return (int) ans;
66126
}
67127
}
68128
```
69129

70130
### **C++**
71131

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+
72152
```cpp
73153
class Solution {
74154
public:
75155
int smallestDifference(vector<int>& a, vector<int>& b) {
76156
sort(a.begin(), a.end());
77157
sort(b.begin(), b.end());
78158
int i = 0, j = 0;
79-
long res = LONG_MAX;
159+
long long ans = LONG_LONG_MAX;
80160
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]) {
85163
++i;
164+
} else {
165+
++j;
166+
}
86167
}
87-
return res;
168+
return ans;
88169
}
89170
};
90171
```
91172

92173
### **Go**
93174

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+
94199
```go
95200
func smallestDifference(a []int, b []int) int {
96201
sort.Ints(a)
97202
sort.Ints(b)
98-
i, j, res := 0, 0, 2147483647
203+
i, j := 0, 0
204+
var ans int = 1e18
99205
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] {
104208
i++
209+
} else {
210+
j++
105211
}
106212
}
107-
return res
213+
return ans
108214
}
109215

110216
func abs(a int) int {
@@ -122,6 +228,55 @@ func min(a, b int) int {
122228
}
123229
```
124230

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+
125280
### **...**
126281

127282
```

0 commit comments

Comments
 (0)