Skip to content

Commit

Permalink
feat: add solutions to lc/lcci problems
Browse files Browse the repository at this point in the history
* lc No.0401.Binary Watch
* lcci No.17.11.Find Closest
  • Loading branch information
yanglbme committed May 27, 2022
1 parent fdeb627 commit f1c34bc
Show file tree
Hide file tree
Showing 14 changed files with 504 additions and 96 deletions.
140 changes: 116 additions & 24 deletions lcci/17.11.Find Closest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,31 @@
```python
class Solution:
def findClosest(self, words: List[str], word1: str, word2: str) -> int:
idx1, idx2, ans = 10**5, -10**5, 10**5
for i, word in enumerate(words):
i, j, ans = 1e5, -1e5, 1e5
for k, word in enumerate(words):
if word == word1:
idx1 = i
i = k
elif word == word2:
idx2 = i
ans = min(ans, abs(idx1 - idx2))
j = k
ans = min(ans, abs(i - j))
return ans
```

```python
class Solution:
def findClosest(self, words: List[str], word1: str, word2: str) -> int:
d = defaultdict(list)
for i, w in enumerate(words):
d[w].append(i)
ans = 1e5
idx1, idx2 = d[word1], d[word2]
i, j, m, n = 0, 0, len(idx1), len(idx2)
while i < m and j < n:
ans = min(ans, abs(idx1[i] - idx2[j]))
if idx1[i] < idx2[j]:
i += 1
else:
j += 1
return ans
```

Expand All @@ -48,17 +66,39 @@ class Solution:
```java
class Solution {
public int findClosest(String[] words, String word1, String word2) {
int idx1 = 100000;
int idx2 = -100000;
int ans = 100000;
for (int i = 0; i < words.length; ++i) {
String word = words[i];
int i = 100000, j = -100000, ans = 100000;
for (int k = 0; k < words.length; ++k) {
String word = words[k];
if (word.equals(word1)) {
idx1 = i;
i = k;
} else if (word.equals(word2)) {
idx2 = i;
j = k;
}
ans = Math.min(ans, Math.abs(i - j));
}
return ans;
}
}
```

```java
class Solution {
public int findClosest(String[] words, String word1, String word2) {
Map<String, List<Integer>> d = new HashMap<>();
for (int i = 0; i < words.length; ++i) {
d.computeIfAbsent(words[i], k -> new ArrayList<>()).add(i);
}
List<Integer> idx1 = d.get(word1), idx2 = d.get(word2);
int i = 0, j = 0, m = idx1.size(), n = idx2.size();
int ans = 100000;
while (i < m && j < n) {
int t = Math.abs(idx1.get(i) - idx2.get(j));
ans = Math.min(ans, t);
if (idx1.get(i) < idx2.get(j)) {
++i;
} else {
++j;
}
ans = Math.min(ans, Math.abs(idx1 - idx2));
}
return ans;
}
Expand Down Expand Up @@ -92,13 +132,34 @@ function findClosest(words: string[], word1: string, word2: string): number {
class Solution {
public:
int findClosest(vector<string>& words, string word1, string word2) {
int idx1 = 1e5, idx2 = -1e5, ans = 1e5;
for (int i = 0; i < words.size(); ++i)
int i = 1e5, j = -1e5, ans = 1e5;
for (int k = 0; k < words.size(); ++k)
{
string word = words[i];
if (word == word1) idx1 = i;
else if (word == word2) idx2 = i;
ans = min(ans, abs(idx1 - idx2));
string word = words[k];
if (word == word1) i = k;
else if (word == word2) j = k;
ans = min(ans, abs(i - j));
}
return ans;
}
};
```
```cpp
class Solution {
public:
int findClosest(vector<string>& words, string word1, string word2) {
unordered_map<string, vector<int>> d;
for (int i = 0; i < words.size(); ++i) d[words[i]].push_back(i);
vector<int> idx1 = d[word1], idx2 = d[word2];
int i = 0, j = 0, m = idx1.size(), n = idx2.size();
int ans = 1e5;
while (i < m && j < n)
{
int t = abs(idx1[i] - idx2[j]);
ans = min(ans, t);
if (idx1[i] < idx2[j]) ++i;
else ++j;
}
return ans;
}
Expand All @@ -109,14 +170,14 @@ public:

```go
func findClosest(words []string, word1 string, word2 string) int {
idx1, idx2, ans := 100000, -100000, 100000
for i, word := range words {
i, j, ans := 100000, -100000, 100000
for k, word := range words {
if word == word1 {
idx1 = i
i = k
} else if word == word2 {
idx2 = i
j = k
}
ans = min(ans, abs(idx1-idx2))
ans = min(ans, abs(i-j))
}
return ans
}
Expand All @@ -136,6 +197,37 @@ func abs(x int) int {
}
```

```go
func findClosest(words []string, word1 string, word2 string) int {
d := map[string][]int{}
for i, w := range words {
d[w] = append(d[w], i)
}
idx1, idx2 := d[word1], d[word2]
i, j, m, n := 0, 0, len(idx1), len(idx2)
ans := 100000
for i < m && j < n {
t := abs(idx1[i] - idx2[j])
if t < ans {
ans = t
}
if idx1[i] < idx2[j] {
i++
} else {
j++
}
}
return ans
}

func abs(x int) int {
if x < 0 {
return -x
}
return x
}
```

### **Rust**

```rust
Expand Down
140 changes: 116 additions & 24 deletions lcci/17.11.Find Closest/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,31 @@
```python
class Solution:
def findClosest(self, words: List[str], word1: str, word2: str) -> int:
idx1, idx2, ans = 10**5, -10**5, 10**5
for i, word in enumerate(words):
i, j, ans = 1e5, -1e5, 1e5
for k, word in enumerate(words):
if word == word1:
idx1 = i
i = k
elif word == word2:
idx2 = i
ans = min(ans, abs(idx1 - idx2))
j = k
ans = min(ans, abs(i - j))
return ans
```

```python
class Solution:
def findClosest(self, words: List[str], word1: str, word2: str) -> int:
d = defaultdict(list)
for i, w in enumerate(words):
d[w].append(i)
ans = 1e5
idx1, idx2 = d[word1], d[word2]
i, j, m, n = 0, 0, len(idx1), len(idx2)
while i < m and j < n:
ans = min(ans, abs(idx1[i] - idx2[j]))
if idx1[i] < idx2[j]:
i += 1
else:
j += 1
return ans
```

Expand All @@ -44,17 +62,39 @@ class Solution:
```java
class Solution {
public int findClosest(String[] words, String word1, String word2) {
int idx1 = 100000;
int idx2 = -100000;
int ans = 100000;
for (int i = 0; i < words.length; ++i) {
String word = words[i];
int i = 100000, j = -100000, ans = 100000;
for (int k = 0; k < words.length; ++k) {
String word = words[k];
if (word.equals(word1)) {
idx1 = i;
i = k;
} else if (word.equals(word2)) {
idx2 = i;
j = k;
}
ans = Math.min(ans, Math.abs(i - j));
}
return ans;
}
}
```

```java
class Solution {
public int findClosest(String[] words, String word1, String word2) {
Map<String, List<Integer>> d = new HashMap<>();
for (int i = 0; i < words.length; ++i) {
d.computeIfAbsent(words[i], k -> new ArrayList<>()).add(i);
}
List<Integer> idx1 = d.get(word1), idx2 = d.get(word2);
int i = 0, j = 0, m = idx1.size(), n = idx2.size();
int ans = 100000;
while (i < m && j < n) {
int t = Math.abs(idx1.get(i) - idx2.get(j));
ans = Math.min(ans, t);
if (idx1.get(i) < idx2.get(j)) {
++i;
} else {
++j;
}
ans = Math.min(ans, Math.abs(idx1 - idx2));
}
return ans;
}
Expand Down Expand Up @@ -88,13 +128,34 @@ function findClosest(words: string[], word1: string, word2: string): number {
class Solution {
public:
int findClosest(vector<string>& words, string word1, string word2) {
int idx1 = 1e5, idx2 = -1e5, ans = 1e5;
for (int i = 0; i < words.size(); ++i)
int i = 1e5, j = -1e5, ans = 1e5;
for (int k = 0; k < words.size(); ++k)
{
string word = words[i];
if (word == word1) idx1 = i;
else if (word == word2) idx2 = i;
ans = min(ans, abs(idx1 - idx2));
string word = words[k];
if (word == word1) i = k;
else if (word == word2) j = k;
ans = min(ans, abs(i - j));
}
return ans;
}
};
```
```cpp
class Solution {
public:
int findClosest(vector<string>& words, string word1, string word2) {
unordered_map<string, vector<int>> d;
for (int i = 0; i < words.size(); ++i) d[words[i]].push_back(i);
vector<int> idx1 = d[word1], idx2 = d[word2];
int i = 0, j = 0, m = idx1.size(), n = idx2.size();
int ans = 1e5;
while (i < m && j < n)
{
int t = abs(idx1[i] - idx2[j]);
ans = min(ans, t);
if (idx1[i] < idx2[j]) ++i;
else ++j;
}
return ans;
}
Expand All @@ -105,14 +166,14 @@ public:

```go
func findClosest(words []string, word1 string, word2 string) int {
idx1, idx2, ans := 100000, -100000, 100000
for i, word := range words {
i, j, ans := 100000, -100000, 100000
for k, word := range words {
if word == word1 {
idx1 = i
i = k
} else if word == word2 {
idx2 = i
j = k
}
ans = min(ans, abs(idx1-idx2))
ans = min(ans, abs(i-j))
}
return ans
}
Expand All @@ -132,6 +193,37 @@ func abs(x int) int {
}
```

```go
func findClosest(words []string, word1 string, word2 string) int {
d := map[string][]int{}
for i, w := range words {
d[w] = append(d[w], i)
}
idx1, idx2 := d[word1], d[word2]
i, j, m, n := 0, 0, len(idx1), len(idx2)
ans := 100000
for i < m && j < n {
t := abs(idx1[i] - idx2[j])
if t < ans {
ans = t
}
if idx1[i] < idx2[j] {
i++
} else {
j++
}
}
return ans
}

func abs(x int) int {
if x < 0 {
return -x
}
return x
}
```

### **Rust**

```rust
Expand Down
12 changes: 6 additions & 6 deletions lcci/17.11.Find Closest/Solution.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
class Solution {
public:
int findClosest(vector<string>& words, string word1, string word2) {
int idx1 = 1e5, idx2 = -1e5, ans = 1e5;
for (int i = 0; i < words.size(); ++i)
int i = 1e5, j = -1e5, ans = 1e5;
for (int k = 0; k < words.size(); ++k)
{
string word = words[i];
if (word == word1) idx1 = i;
else if (word == word2) idx2 = i;
ans = min(ans, abs(idx1 - idx2));
string word = words[k];
if (word == word1) i = k;
else if (word == word2) j = k;
ans = min(ans, abs(i - j));
}
return ans;
}
Expand Down
Loading

0 comments on commit f1c34bc

Please sign in to comment.