Skip to content

Commit 5980d6c

Browse files
committed
feat: add solutions to lc problem: No.1182. Shortest Distance to Target Color
1 parent 59d54a5 commit 5980d6c

File tree

4 files changed

+198
-5
lines changed

4 files changed

+198
-5
lines changed

solution/1100-1199/1182.Shortest Distance to Target Color/README.md

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,27 +46,92 @@
4646
<li><code>1 &lt;= queries[i][1] &lt;= 3</code></li>
4747
</ul>
4848

49-
5049
## 解法
5150

5251
<!-- 这里可写通用的实现逻辑 -->
5352

53+
二分查找。
54+
55+
先用哈希表记录每种颜色的索引位置。然后遍历 `queries`,如果当前 `color` 不在哈希表中,说明不存在解决方案,此时此 `query` 对应的结果元素是 `-1`。否则,在哈希表中取出当前 `color` 对应的索引列表,二分查找即可。
56+
5457
<!-- tabs:start -->
5558

5659
### **Python3**
5760

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

6063
```python
61-
64+
class Solution:
65+
def shortestDistanceColor(self, colors: List[int], queries: List[List[int]]) -> List[int]:
66+
color_indexes = collections.defaultdict(list)
67+
for i, c in enumerate(colors):
68+
color_indexes[c].append(i)
69+
res = []
70+
for i, c in queries:
71+
if c not in color_indexes:
72+
res.append(-1)
73+
else:
74+
t = color_indexes[c]
75+
left, right = 0, len(t) - 1
76+
while left < right:
77+
mid = (left + right) >> 1
78+
if t[mid] >= i:
79+
right = mid
80+
else:
81+
left = mid + 1
82+
val = abs(t[left] - i)
83+
if left > 0:
84+
val = min(val, abs(t[left - 1] - i))
85+
if left < len(t) - 1:
86+
val = min(val, abs(t[left + 1] - i))
87+
res.append(val)
88+
return res
6289
```
6390

6491
### **Java**
6592

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

6895
```java
69-
96+
class Solution {
97+
public List<Integer> shortestDistanceColor(int[] colors, int[][] queries) {
98+
Map<Integer, List<Integer>> colorIndexes = new HashMap<>();
99+
for (int i = 0; i < colors.length; ++i) {
100+
int c = colors[i];
101+
if (!colorIndexes.containsKey(c)) {
102+
colorIndexes.put(c, new ArrayList<>());
103+
}
104+
colorIndexes.get(c).add(i);
105+
}
106+
List<Integer> res = new ArrayList<>();
107+
for (int[] query : queries) {
108+
int i = query[0], c = query[1];
109+
if (!colorIndexes.containsKey(c)) {
110+
res.add(-1);
111+
continue;
112+
}
113+
List<Integer> t = colorIndexes.get(c);
114+
int left = 0, right = t.size() - 1;
115+
while (left < right) {
116+
int mid = (left + right) >> 1;
117+
if (t.get(mid) >= i) {
118+
right = mid;
119+
} else {
120+
left = mid + 1;
121+
}
122+
}
123+
int val = Math.abs(t.get(left) - i);
124+
if (left > 0) {
125+
val = Math.min(val, Math.abs(t.get(left - 1) - i));
126+
}
127+
if (left < t.size() - 1) {
128+
val = Math.min(val, Math.abs(t.get(left + 1) - i));
129+
}
130+
res.add(val);
131+
}
132+
return res;
133+
}
134+
}
70135
```
71136

72137
### **...**

solution/1100-1199/1182.Shortest Distance to Target Color/README_EN.md

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,82 @@ The nearest 1 from index 6 is at index 3 (3 steps away).
4343

4444
## Solutions
4545

46+
Binary search.
47+
4648
<!-- tabs:start -->
4749

4850
### **Python3**
4951

5052
```python
51-
53+
class Solution:
54+
def shortestDistanceColor(self, colors: List[int], queries: List[List[int]]) -> List[int]:
55+
color_indexes = collections.defaultdict(list)
56+
for i, c in enumerate(colors):
57+
color_indexes[c].append(i)
58+
res = []
59+
for i, c in queries:
60+
if c not in color_indexes:
61+
res.append(-1)
62+
else:
63+
t = color_indexes[c]
64+
left, right = 0, len(t) - 1
65+
while left < right:
66+
mid = (left + right) >> 1
67+
if t[mid] >= i:
68+
right = mid
69+
else:
70+
left = mid + 1
71+
val = abs(t[left] - i)
72+
if left > 0:
73+
val = min(val, abs(t[left - 1] - i))
74+
if left < len(t) - 1:
75+
val = min(val, abs(t[left + 1] - i))
76+
res.append(val)
77+
return res
5278
```
5379

5480
### **Java**
5581

5682
```java
57-
83+
class Solution {
84+
public List<Integer> shortestDistanceColor(int[] colors, int[][] queries) {
85+
Map<Integer, List<Integer>> colorIndexes = new HashMap<>();
86+
for (int i = 0; i < colors.length; ++i) {
87+
int c = colors[i];
88+
if (!colorIndexes.containsKey(c)) {
89+
colorIndexes.put(c, new ArrayList<>());
90+
}
91+
colorIndexes.get(c).add(i);
92+
}
93+
List<Integer> res = new ArrayList<>();
94+
for (int[] query : queries) {
95+
int i = query[0], c = query[1];
96+
if (!colorIndexes.containsKey(c)) {
97+
res.add(-1);
98+
continue;
99+
}
100+
List<Integer> t = colorIndexes.get(c);
101+
int left = 0, right = t.size() - 1;
102+
while (left < right) {
103+
int mid = (left + right) >> 1;
104+
if (t.get(mid) >= i) {
105+
right = mid;
106+
} else {
107+
left = mid + 1;
108+
}
109+
}
110+
int val = Math.abs(t.get(left) - i);
111+
if (left > 0) {
112+
val = Math.min(val, Math.abs(t.get(left - 1) - i));
113+
}
114+
if (left < t.size() - 1) {
115+
val = Math.min(val, Math.abs(t.get(left + 1) - i));
116+
}
117+
res.add(val);
118+
}
119+
return res;
120+
}
121+
}
58122
```
59123

60124
### **...**
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Solution {
2+
public List<Integer> shortestDistanceColor(int[] colors, int[][] queries) {
3+
Map<Integer, List<Integer>> colorIndexes = new HashMap<>();
4+
for (int i = 0; i < colors.length; ++i) {
5+
int c = colors[i];
6+
if (!colorIndexes.containsKey(c)) {
7+
colorIndexes.put(c, new ArrayList<>());
8+
}
9+
colorIndexes.get(c).add(i);
10+
}
11+
List<Integer> res = new ArrayList<>();
12+
for (int[] query : queries) {
13+
int i = query[0], c = query[1];
14+
if (!colorIndexes.containsKey(c)) {
15+
res.add(-1);
16+
continue;
17+
}
18+
List<Integer> t = colorIndexes.get(c);
19+
int left = 0, right = t.size() - 1;
20+
while (left < right) {
21+
int mid = (left + right) >> 1;
22+
if (t.get(mid) >= i) {
23+
right = mid;
24+
} else {
25+
left = mid + 1;
26+
}
27+
}
28+
int val = Math.abs(t.get(left) - i);
29+
if (left > 0) {
30+
val = Math.min(val, Math.abs(t.get(left - 1) - i));
31+
}
32+
if (left < t.size() - 1) {
33+
val = Math.min(val, Math.abs(t.get(left + 1) - i));
34+
}
35+
res.add(val);
36+
}
37+
return res;
38+
}
39+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution:
2+
def shortestDistanceColor(self, colors: List[int], queries: List[List[int]]) -> List[int]:
3+
color_indexes = collections.defaultdict(list)
4+
for i, c in enumerate(colors):
5+
color_indexes[c].append(i)
6+
res = []
7+
for i, c in queries:
8+
if c not in color_indexes:
9+
res.append(-1)
10+
else:
11+
t = color_indexes[c]
12+
left, right = 0, len(t) - 1
13+
while left < right:
14+
mid = (left + right) >> 1
15+
if t[mid] >= i:
16+
right = mid
17+
else:
18+
left = mid + 1
19+
val = abs(t[left] - i)
20+
if left > 0:
21+
val = min(val, abs(t[left - 1] - i))
22+
if left < len(t) - 1:
23+
val = min(val, abs(t[left + 1] - i))
24+
res.append(val)
25+
return res

0 commit comments

Comments
 (0)