8000 * Done with "2078. Two Furthest Houses With Different Colors". · garciparedes/leetcode@037465d · GitHub
[go: up one dir, main page]

Skip to content

Commit 037465d

Browse files
committed
* Done with "2078. Two Furthest Houses With Different Colors".
1 parent b69234e commit 037465d

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution:
2+
def maxDistance(self, colors: List[int]) -> int:
3+
ranges = dict()
4+
5+
for i, color in enumerate(colors):
6+
if color in ranges:
7+
ranges[color][1] = i
8+
else:
9+
ranges[color] = [i, i]
10+
11+
min_1 = "", len(colors)
12+
for k, v in ranges.items():
13+
if v[0] < min_1[1]:
14+
min_1 = k, v[0]
15+
16+
max_1 = "", -1
17+
for k, v in ranges.items():
18+
if k != min_1[0] and v[1] > max_1[1]:
19+
max_1 = k, v[1]
20+
21+
min_2 = "", len(colors)
22+
for k, v in ranges.items():
23+
if k != min_1[0] and v[0] < min_2[1]:
24+
min_2 = k, v[0]
25+
26+
max_2 = "", -1
27+
for k, v in ranges.items():
28+
if k != min_2[0] and v[1] > max_2[1]:
29+
max_2 = k, v[1]
30+
31+
return max(max_1[1] - min_1[1], max_2[1] - min_2[1])
32+

0 commit comments

Comments
 (0)
0