Skip to content

Commit f1c34bc

Browse files
committed
feat: add solutions to lc/lcci problems
* lc No.0401.Binary Watch * lcci No.17.11.Find Closest
1 parent fdeb627 commit f1c34bc

File tree

14 files changed

+504
-96
lines changed

14 files changed

+504
-96
lines changed

lcci/17.11.Find Closest/README.md

Lines changed: 116 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,31 @@
3131
```python
3232
class Solution:
3333
def findClosest(self, words: List[str], word1: str, word2: str) -> int:
34-
idx1, idx2, ans = 10**5, -10**5, 10**5
35-
for i, word in enumerate(words):
34+
i, j, ans = 1e5, -1e5, 1e5
35+
for k, word in enumerate(words):
3636
if word == word1:
37-
idx1 = i
37+
i = k
3838
elif word == word2:
39-
idx2 = i
40-
ans = min(ans, abs(idx1 - idx2))
39+
j = k
40+
ans = min(ans, abs(i - j))
41+
return ans
42+
```
43+
44+
```python
45+
class Solution:
46+
def findClosest(self, words: List[str], word1: str, word2: str) -> int:
47+
d = defaultdict(list)
48+
for i, w in enumerate(words):
49+
d[w].append(i)
50+
ans = 1e5
51+
idx1, idx2 = d[word1], d[word2]
52+
i, j, m, n = 0, 0, len(idx1), len(idx2)
53+
while i < m and j < n:
54+
ans = min(ans, abs(idx1[i] - idx2[j]))
55+
if idx1[i] < idx2[j]:
56+
i += 1
57+
else:
58+
j += 1
4159
return ans
4260
```
4361

@@ -48,17 +66,39 @@ class Solution:
4866
```java
4967
class Solution {
5068
public int findClosest(String[] words, String word1, String word2) {
51-
int idx1 = 100000;
52-
int idx2 = -100000;
53-
int ans = 100000;
54-
for (int i = 0; i < words.length; ++i) {
55-
String word = words[i];
69+
int i = 100000, j = -100000, ans = 100000;
70+
for (int k = 0; k < words.length; ++k) {
71+
String word = words[k];
5672
if (word.equals(word1)) {
57-
idx1 = i;
73+
i = k;
5874
} else if (word.equals(word2)) {
59-
idx2 = i;
75+
j = k;
76+
}
77+
ans = Math.min(ans, Math.abs(i - j));
78+
}
79+
return ans;
80+
}
81+
}
82+
```
83+
84+
```java
85+
class Solution {
86+
public int findClosest(String[] words, String word1, String word2) {
87+
Map<String, List<Integer>> d = new HashMap<>();
88+
for (int i = 0; i < words.length; ++i) {
89+
d.computeIfAbsent(words[i], k -> new ArrayList<>()).add(i);
90+
}
91+
List<Integer> idx1 = d.get(word1), idx2 = d.get(word2);
92+
int i = 0, j = 0, m = idx1.size(), n = idx2.size();
93+
int ans = 100000;
94+
while (i < m && j < n) {
95+
int t = Math.abs(idx1.get(i) - idx2.get(j));
96+
ans = Math.min(ans, t);
97+
if (idx1.get(i) < idx2.get(j)) {
98+
++i;
99+
} else {
100+
++j;
60101
}
61-
ans = Math.min(ans, Math.abs(idx1 - idx2));
62102
}
63103
return ans;
64104
}
@@ -92,13 +132,34 @@ function findClosest(words: string[], word1: string, word2: string): number {
92132
class Solution {
93133
public:
94134
int findClosest(vector<string>& words, string word1, string word2) {
95-
int idx1 = 1e5, idx2 = -1e5, ans = 1e5;
96-
for (int i = 0; i < words.size(); ++i)
135+
int i = 1e5, j = -1e5, ans = 1e5;
136+
for (int k = 0; k < words.size(); ++k)
97137
{
98-
string word = words[i];
99-
if (word == word1) idx1 = i;
100-
else if (word == word2) idx2 = i;
101-
ans = min(ans, abs(idx1 - idx2));
138+
string word = words[k];
139+
if (word == word1) i = k;
140+
else if (word == word2) j = k;
141+
ans = min(ans, abs(i - j));
142+
}
143+
return ans;
144+
}
145+
};
146+
```
147+
148+
```cpp
149+
class Solution {
150+
public:
151+
int findClosest(vector<string>& words, string word1, string word2) {
152+
unordered_map<string, vector<int>> d;
153+
for (int i = 0; i < words.size(); ++i) d[words[i]].push_back(i);
154+
vector<int> idx1 = d[word1], idx2 = d[word2];
155+
int i = 0, j = 0, m = idx1.size(), n = idx2.size();
156+
int ans = 1e5;
157+
while (i < m && j < n)
158+
{
159+
int t = abs(idx1[i] - idx2[j]);
160+
ans = min(ans, t);
161+
if (idx1[i] < idx2[j]) ++i;
162+
else ++j;
102163
}
103164
return ans;
104165
}
@@ -109,14 +170,14 @@ public:
109170

110171
```go
111172
func findClosest(words []string, word1 string, word2 string) int {
112-
idx1, idx2, ans := 100000, -100000, 100000
113-
for i, word := range words {
173+
i, j, ans := 100000, -100000, 100000
174+
for k, word := range words {
114175
if word == word1 {
115-
idx1 = i
176+
i = k
116177
} else if word == word2 {
117-
idx2 = i
178+
j = k
118179
}
119-
ans = min(ans, abs(idx1-idx2))
180+
ans = min(ans, abs(i-j))
120181
}
121182
return ans
122183
}
@@ -136,6 +197,37 @@ func abs(x int) int {
136197
}
137198
```
138199

200+
```go
201+
func findClosest(words []string, word1 string, word2 string) int {
202+
d := map[string][]int{}
203+
for i, w := range words {
204+
d[w] = append(d[w], i)
205+
}
206+
idx1, idx2 := d[word1], d[word2]
207+
i, j, m, n := 0, 0, len(idx1), len(idx2)
208+
ans := 100000
209+
for i < m && j < n {
210+
t := abs(idx1[i] - idx2[j])
211+
if t < ans {
212+
ans = t
213+
}
214+
if idx1[i] < idx2[j] {
215+
i++
216+
} else {
217+
j++
218+
}
219+
}
220+
return ans
221+
}
222+
223+
func abs(x int) int {
224+
if x < 0 {
225+
return -x
226+
}
227+
return x
228+
}
229+
```
230+
139231
### **Rust**
140232

141233
```rust

lcci/17.11.Find Closest/README_EN.md

Lines changed: 116 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,31 @@
2929
```python
3030
class Solution:
3131
def findClosest(self, words: List[str], word1: str, word2: str) -> int:
32-
idx1, idx2, ans = 10**5, -10**5, 10**5
33-
for i, word in enumerate(words):
32+
i, j, ans = 1e5, -1e5, 1e5
33+
for k, word in enumerate(words):
3434
if word == word1:
35-
idx1 = i
35+
i = k
3636
elif word == word2:
37-
idx2 = i
38-
ans = min(ans, abs(idx1 - idx2))
37+
j = k
38+
ans = min(ans, abs(i - j))
39+
return ans
40+
```
41+
42+
```python
43+
class Solution:
44+
def findClosest(self, words: List[str], word1: str, word2: str) -> int:
45+
d = defaultdict(list)
46+
for i, w in enumerate(words):
47+
d[w].append(i)
48+
ans = 1e5
49+
idx1, idx2 = d[word1], d[word2]
50+
i, j, m, n = 0, 0, len(idx1), len(idx2)
51+
while i < m and j < n:
52+
ans = min(ans, abs(idx1[i] - idx2[j]))
53+
if idx1[i] < idx2[j]:
54+
i += 1
55+
else:
56+
j += 1
3957
return ans
4058
```
4159

@@ -44,17 +62,39 @@ class Solution:
4462
```java
4563
class Solution {
4664
public int findClosest(String[] words, String word1, String word2) {
47-
int idx1 = 100000;
48-
int idx2 = -100000;
49-
int ans = 100000;
50-
for (int i = 0; i < words.length; ++i) {
51-
String word = words[i];
65+
int i = 100000, j = -100000, ans = 100000;
66+
for (int k = 0; k < words.length; ++k) {
67+
String word = words[k];
5268
if (word.equals(word1)) {
53-
idx1 = i;
69+
i = k;
5470
} else if (word.equals(word2)) {
55-
idx2 = i;
71+
j = k;
72+
}
73+
ans = Math.min(ans, Math.abs(i - j));
74+
}
75+
return ans;
76+
}
77+
}
78+
```
79+
80+
```java
81+
class Solution {
82+
public int findClosest(String[] words, String word1, String word2) {
83+
Map<String, List<Integer>> d = new HashMap<>();
84+
for (int i = 0; i < words.length; ++i) {
85+
d.computeIfAbsent(words[i], k -> new ArrayList<>()).add(i);
86+
}
87+
List<Integer> idx1 = d.get(word1), idx2 = d.get(word2);
88+
int i = 0, j = 0, m = idx1.size(), n = idx2.size();
89+
int ans = 100000;
90+
while (i < m && j < n) {
91+
int t = Math.abs(idx1.get(i) - idx2.get(j));
92+
ans = Math.min(ans, t);
93+
if (idx1.get(i) < idx2.get(j)) {
94+
++i;
95+
} else {
96+
++j;
5697
}
57-
ans = Math.min(ans, Math.abs(idx1 - idx2));
5898
}
5999
return ans;
60100
}
@@ -88,13 +128,34 @@ function findClosest(words: string[], word1: string, word2: string): number {
88128
class Solution {
89129
public:
90130
int findClosest(vector<string>& words, string word1, string word2) {
91-
int idx1 = 1e5, idx2 = -1e5, ans = 1e5;
92-
for (int i = 0; i < words.size(); ++i)
131+
int i = 1e5, j = -1e5, ans = 1e5;
132+
for (int k = 0; k < words.size(); ++k)
93133
{
94-
string word = words[i];
95-
if (word == word1) idx1 = i;
96-
else if (word == word2) idx2 = i;
97-
ans = min(ans, abs(idx1 - idx2));
134+
string word = words[k];
135+
if (word == word1) i = k;
136+
else if (word == word2) j = k;
137+
ans = min(ans, abs(i - j));
138+
}
139+
return ans;
140+
}
141+
};
142+
```
143+
144+
```cpp
145+
class Solution {
146+
public:
147+
int findClosest(vector<string>& words, string word1, string word2) {
148+
unordered_map<string, vector<int>> d;
149+
for (int i = 0; i < words.size(); ++i) d[words[i]].push_back(i);
150+
vector<int> idx1 = d[word1], idx2 = d[word2];
151+
int i = 0, j = 0, m = idx1.size(), n = idx2.size();
152+
int ans = 1e5;
153+
while (i < m && j < n)
154+
{
155+
int t = abs(idx1[i] - idx2[j]);
156+
ans = min(ans, t);
157+
if (idx1[i] < idx2[j]) ++i;
158+
else ++j;
98159
}
99160
return ans;
100161
}
@@ -105,14 +166,14 @@ public:
105166

106167
```go
107168
func findClosest(words []string, word1 string, word2 string) int {
108-
idx1, idx2, ans := 100000, -100000, 100000
109-
for i, word := range words {
169+
i, j, ans := 100000, -100000, 100000
170+
for k, word := range words {
110171
if word == word1 {
111-
idx1 = i
172+
i = k
112173
} else if word == word2 {
113-
idx2 = i
174+
j = k
114175
}
115-
ans = min(ans, abs(idx1-idx2))
176+
ans = min(ans, abs(i-j))
116177
}
117178
return ans
118179
}
@@ -132,6 +193,37 @@ func abs(x int) int {
132193
}
133194
```
134195

196+
```go
197+
func findClosest(words []string, word1 string, word2 string) int {
198+
d := map[string][]int{}
199+
for i, w := range words {
200+
d[w] = append(d[w], i)
201+
}
202+
idx1, idx2 := d[word1], d[word2]
203+
i, j, m, n := 0, 0, len(idx1), len(idx2)
204+
ans := 100000
205+
for i < m && j < n {
206+
t := abs(idx1[i] - idx2[j])
207+
if t < ans {
208+
ans = t
209+
}
210+
if idx1[i] < idx2[j] {
211+
i++
212+
} else {
213+
j++
214+
}
215+
}
216+
return ans
217+
}
218+
219+
func abs(x int) int {
220+
if x < 0 {
221+
return -x
222+
}
223+
return x
224+
}
225+
```
226+
135227
### **Rust**
136228

137229
```rust

lcci/17.11.Find Closest/Solution.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
class Solution {
22
public:
33
int findClosest(vector<string>& words, string word1, string word2) {
4-
int idx1 = 1e5, idx2 = -1e5, ans = 1e5;
5-
for (int i = 0; i < words.size(); ++i)
4+
int i = 1e5, j = -1e5, ans = 1e5;
5+
for (int k = 0; k < words.size(); ++k)
66
{
7-
string word = words[i];
8-
if (word == word1) idx1 = i;
9-
else if (word == word2) idx2 = i;
10-
ans = min(ans, abs(idx1 - idx2));
7+
string word = words[k];
8+
if (word == word1) i = k;
9+
else if (word == word2) j = k;
10+
ans = min(ans, abs(i - j));
1111
}
1212
return ans;
1313
}

0 commit comments

Comments
 (0)