Skip to content

Commit 0e3db4b

Browse files
authored
feat: add solutions to lc problem: No.1848 (#2139)
No.1848.Minimum Distance to the Target Element
1 parent f287f7f commit 0e3db4b

File tree

5 files changed

+82
-17
lines changed

5 files changed

+82
-17
lines changed

solution/1800-1899/1848.Minimum Distance to the Target Element/README.md

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,7 @@
6868
```python
6969
class Solution:
7070
def getMinDistance(self, nums: List[int], target: int, start: int) -> int:
71-
ans = inf
72-
for i, x in enumerate(nums):
73-
if x == target:
74-
ans = min(ans, abs(i - start))
75-
return ans
71+
return min(abs(i - start) for i, x in enumerate(nums) if x == target)
7672
```
7773

7874
### **Java**
@@ -133,6 +129,35 @@ func abs(x int) int {
133129
}
134130
```
135131

132+
### **TypeScript**
133+
134+
```ts
135+
function getMinDistance(nums: number[], target: number, start: number): number {
136+
let ans = Infinity;
137+
for (let i = 0; i < nums.length; ++i) {
138+
if (nums[i] === target) {
139+
ans = Math.min(ans, Math.abs(i - start));
140+
}
141+
}
142+
return ans;
143+
}
144+
```
145+
146+
### **Rust**
147+
148+
```rust
149+
impl Solution {
150+
pub fn get_min_distance(nums: Vec<i32>, target: i32, start: i32) -> i32 {
151+
nums.iter()
152+
.enumerate()
153+
.filter(|&(_, &x)| x == target)
154+
.map(|(i, _)| ((i as i32) - start).abs())
155+
.min()
156+
.unwrap_or_default()
157+
}
158+
}
159+
```
160+
136161
### **...**
137162

138163
```

solution/1800-1899/1848.Minimum Distance to the Target Element/README_EN.md

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,7 @@ The time complexity is $O(n)$, where $n$ is the length of the array $nums$. The
6060
```python
6161
class Solution:
6262
def getMinDistance(self, nums: List[int], target: int, start: int) -> int:
63-
ans = inf
64-
for i, x in enumerate(nums):
65-
if x == target:
66-
ans = min(ans, abs(i - start))
67-
return ans
63+
return min(abs(i - start) for i, x in enumerate(nums) if x == target)
6864
```
6965

7066
### **Java**
@@ -123,6 +119,35 @@ func abs(x int) int {
123119
}
124120
```
125121

122+
### **TypeScript**
123+
124+
```ts
125+
function getMinDistance(nums: number[], target: number, start: number): number {
126+
let ans = Infinity;
127+
for (let i = 0; i < nums.length; ++i) {
128+
if (nums[i] === target) {
129+
ans = Math.min(ans, Math.abs(i - start));
130+
}
131+
}
132+
return ans;
133+
}
134+
```
135+
136+
### **Rust**
137+
138+
```rust
139+
impl Solution {
140+
pub fn get_min_distance(nums: Vec<i32>, target: i32, start: i32) -> i32 {
141+
nums.iter()
142+
.enumerate()
143+
.filter(|&(_, &x)| x == target)
144+
.map(|(i, _)| ((i as i32) - start).abs())
145+
.min()
146+
.unwrap_or_default()
147+
}
148+
}
149+
```
150+
126151
### **...**
127152

128153
```
Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
class Solution:
2-
def getMinDistance(self, nums: List[int], target: int, start: int) -> int:
3-
ans = inf
4-
for i, x in enumerate(nums):
5-
if x == target:
6-
ans = min(ans, abs(i - start))
7-
return ans
1+
class Solution:
2+
def getMinDistance(self, nums: List[int], target: int, start: int) -> int:
3+
return min(abs(i - start) for i, x in enumerate(nums) if x == target)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
impl Solution {
2+
pub fn get_min_distance(nums: Vec<i32>, target: i32, start: i32) -> i32 {
3+
nums.iter()
4+
.enumerate()
5+
.filter(|&(_, &x)| x == target)
6+
.map(|(i, _)| ((i as i32) - start).abs())
7+
.min()
8+
.unwrap_or_default()
9+
}
10+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function getMinDistance(nums: number[], target: number, start: number): number {
2+
let ans = Infinity;
3+
for (let i = 0; i < nums.length; ++i) {
4+
if (nums[i] === target) {
5+
ans = Math.min(ans, Math.abs(i - start));
6+
}
7+
}
8+
return ans;
9+
}

0 commit comments

Comments
 (0)