Skip to content

Commit 7a35747

Browse files
committed
feat: add solutions to lcci problem: No.16.06. Smallest Difference
1 parent 328b1fc commit 7a35747

File tree

6 files changed

+186
-34
lines changed

6 files changed

+186
-34
lines changed

lcci/16.06.Smallest Difference/README.md

+65-11
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@ class Solution:
3232
def smallestDifference(self, a: List[int], b: List[int]) -> int:
3333
a.sort()
3434
b.sort()
35-
i, j, res = 0, 0, 2147483647
36-
m, n = len(a), len(b)
37-
while i < m and j < n:
38-
if a[i] == b[j]: return 0
35+
i = j = 0
36+
res = float('inf')
37+
while i < len(a) and j < len(b):
3938
res = min(res, abs(a[i] - b[j]))
40-
if a[i] > b[j]: j += 1
41-
else: i += 1
39+
if a[i] > b[j]:
40+
j += 1
41+
else:
42+
i += 1
4243
return res
43-
4444
```
4545

4646
### **Java**
@@ -52,17 +52,71 @@ class Solution {
5252
public int smallestDifference(int[] a, int[] b) {
5353
Arrays.sort(a);
5454
Arrays.sort(b);
55-
int m = a.length, n = b.length;
5655
int i = 0, j = 0;
5756
long res = Long.MAX_VALUE;
58-
while (i < m && j < n) {
59-
if (a[i] == b[j]) return 0;
57+
while (i < a.length && j < b.length) {
6058
res = Math.min(res, Math.abs((long) a[i] - (long) b[j]));
59+
if (a[i] > b[j]) {
60+
++j;
61+
} else {
62+
++i;
63+
}
64+
}
65+
return (int) res;
66+
}
67+
}
68+
```
69+
70+
### **C++**
71+
72+
```cpp
73+
class Solution {
74+
public:
75+
int smallestDifference(vector<int>& a, vector<int>& b) {
76+
sort(a.begin(), a.end());
77+
sort(b.begin(), b.end());
78+
int i = 0, j = 0;
79+
long res = LONG_MAX;
80+
while (i < a.size() && j < b.size()) {
81+
res = min(res, abs((long)a[i] - (long)b[j]));
6182
if (a[i] > b[j]) ++j;
6283
else ++i;
6384
}
64-
return (int) res;
85+
return res;
6586
}
87+
};
88+
```
89+
90+
### **Go**
91+
92+
```go
93+
func smallestDifference(a []int, b []int) int {
94+
sort.Ints(a)
95+
sort.Ints(b)
96+
i, j, res := 0, 0, 2147483647
97+
for i < len(a) && j < len(b) {
98+
res = min(res, abs(a[i]-b[j]))
99+
if a[i] > b[j] {
100+
j++
101+
} else {
102+
i++
103+
}
104+
}
105+
return res
106+
}
107+
108+
func abs(a int) int {
109+
if a < 0 {
110+
return -a
111+
}
112+
return a
113+
}
114+
115+
func min(a, b int) int {
116+
if a < b {
117+
return a
118+
}
119+
return b
66120
}
67121
```
68122

lcci/16.06.Smallest Difference/README_EN.md

+65-11
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ class Solution:
3535
def smallestDifference(self, a: List[int], b: List[int]) -> int:
3636
a.sort()
3737
b.sort()
38-
i, j, res = 0, 0, 2147483647
39-
m, n = len(a), len(b)
40-
while i < m and j < n:
41-
if a[i] == b[j]: return 0
38+
i = j = 0
39+
res = float('inf')
40+
while i < len(a) and j < len(b):
4241
res = min(res, abs(a[i] - b[j]))
43-
if a[i] > b[j]: j += 1
44-
else: i += 1
42+
if a[i] > b[j]:
43+
j += 1
44+
else:
45+
i += 1
4546
return res
46-
4747
```
4848

4949
### **Java**
@@ -53,17 +53,71 @@ class Solution {
5353
public int smallestDifference(int[] a, int[] b) {
5454
Arrays.sort(a);
5555
Arrays.sort(b);
56-
int m = a.length, n = b.length;
5756
int i = 0, j = 0;
5857
long res = Long.MAX_VALUE;
59-
while (i < m && j < n) {
60-
if (a[i] == b[j]) return 0;
58+
while (i < a.length && j < b.length) {
6159
res = Math.min(res, Math.abs((long) a[i] - (long) b[j]));
60+
if (a[i] > b[j]) {
61+
++j;
62+
} else {
63+
++i;
64+
}
65+
}
66+
return (int) res;
67+
}
68+
}
69+
```
70+
71+
### **C++**
72+
73+
```cpp
74+
class Solution {
75+
public:
76+
int smallestDifference(vector<int>& a, vector<int>& b) {
77+
sort(a.begin(), a.end());
78+
sort(b.begin(), b.end());
79+
int i = 0, j = 0;
80+
long res = LONG_MAX;
81+
while (i < a.size() && j < b.size()) {
82+
res = min(res, abs((long)a[i] - (long)b[j]));
6283
if (a[i] > b[j]) ++j;
6384
else ++i;
6485
}
65-
return (int) res;
86+
return res;
6687
}
88+
};
89+
```
90+
91+
### **Go**
92+
93+
```go
94+
func smallestDifference(a []int, b []int) int {
95+
sort.Ints(a)
96+
sort.Ints(b)
97+
i, j, res := 0, 0, 2147483647
98+
for i < len(a) && j < len(b) {
99+
res = min(res, abs(a[i]-b[j]))
100+
if a[i] > b[j] {
101+
j++
102+
} else {
103+
i++
104+
}
105+
}
106+
return res
107+
}
108+
109+
func abs(a int) int {
110+
if a < 0 {
111+
return -a
112+
}
113+
return a
114+
}
115+
116+
func min(a, b int) int {
117+
if a < b {
118+
return a
119+
}
120+
return b
67121
}
68122
```
69123

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
int smallestDifference(vector<int>& a, vector<int>& b) {
4+
sort(a.begin(), a.end());
5+
sort(b.begin(), b.end());
6+
int i = 0, j = 0;
7+
long res = LONG_MAX;
8+
while (i < a.size() && j < b.size()) {
9+
res = min(res, abs((long)a[i] - (long)b[j]));
10+
if (a[i] > b[j]) ++j;
11+
else ++i;
12+
}
13+
return res;
14+
}
15+
};
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
func smallestDifference(a []int, b []int) int {
2+
sort.Ints(a)
3+
sort.Ints(b)
4+
i, j, res := 0, 0, 2147483647
5+
for i < len(a) && j < len(b) {
6+
res = min(res, abs(a[i]-b[j]))
7+
if a[i] > b[j] {
8+
j++
9+
} else {
10+
i++
11+
}
12+
}
13+
return res
14+
}
15+
16+
func abs(a int) int {
17+
if a < 0 {
18+
return -a
19+
}
20+
return a
21+
}
22+
23+
func min(a, b int) int {
24+
if a < b {
25+
return a
26+
}
27+
return b
28+
}

lcci/16.06.Smallest Difference/Solution.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ class Solution {
22
public int smallestDifference(int[] a, int[] b) {
33
Arrays.sort(a);
44
Arrays.sort(b);
5-
int m = a.length, n = b.length;
65
int i = 0, j = 0;
76
long res = Long.MAX_VALUE;
8-
while (i < m && j < n) {
9-
if (a[i] == b[j]) return 0;
7+
while (i < a.length && j < b.length) {
108
res = Math.min(res, Math.abs((long) a[i] - (long) b[j]));
11-
if (a[i] > b[j]) ++j;
12-
else ++i;
9+
if (a[i] > b[j]) {
10+
++j;
11+
} else {
12+
++i;
13+
}
1314
}
1415
return (int) res;
1516
}

lcci/16.06.Smallest Difference/Solution.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ class Solution:
22
def smallestDifference(self, a: List[int], b: List[int]) -> int:
33
a.sort()
44
b.sort()
5-
i, j, res = 0, 0, 2147483647
6-
m, n = len(a), len(b)
7-
while i < m and j < n:
8-
if a[i] == b[j]: return 0
5+
i = j = 0
6+
res = float('inf')
7+
while i < len(a) and j < len(b):
98
res = min(res, abs(a[i] - b[j]))
10-
if a[i] > b[j]: j += 1
11-
else: i += 1
9+
if a[i] > b[j]:
10+
j += 1
11+
else:
12+
i += 1
1213
return res
13-

0 commit comments

Comments
 (0)