Skip to content

Commit 31572eb

Browse files
Added task 2078.
1 parent be7bcde commit 31572eb

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g2001_2100.s2078_two_furthest_houses_with_different_colors;
2+
3+
// #Easy #Array #Greedy #2022_05_29_Time_0_ms(100%)_Space_43_MB(5.49%)
4+
5+
public class Solution {
6+
public int maxDistance(int[] colors) {
7+
int left = 0;
8+
int right = colors.length - 1;
9+
int max = 0;
10+
while (left < right) {
11+
if (colors[left] != colors[right]) {
12+
max = Math.max(max, right - left);
13+
break;
14+
} else {
15+
left++;
16+
}
17+
}
18+
left = 0;
19+
while (left < right) {
20+
if (colors[left] != colors[right]) {
21+
max = Math.max(max, right - left);
22+
break;
23+
} else {
24+
right--;
25+
}
26+
}
27+
return max;
28+
}
29+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
2078\. Two Furthest Houses With Different Colors
2+
3+
Easy
4+
5+
There are `n` houses evenly lined up on the street, and each house is beautifully painted. You are given a **0-indexed** integer array `colors` of length `n`, where `colors[i]` represents the color of the <code>i<sup>th</sup></code> house.
6+
7+
Return _the **maximum** distance between **two** houses with **different** colors_.
8+
9+
The distance between the <code>i<sup>th</sup></code> and <code>j<sup>th</sup></code> houses is `abs(i - j)`, where `abs(x)` is the **absolute value** of `x`.
10+
11+
**Example 1:**
12+
13+
![](https://assets.leetcode.com/uploads/2021/10/31/eg1.png)
14+
15+
**Input:** colors = [**1**,1,1,**6**,1,1,1]
16+
17+
**Output:** 3
18+
19+
**Explanation:** In the above image, color 1 is blue, and color 6 is red.
20+
21+
The furthest two houses with different colors are house 0 and house 3.
22+
23+
House 0 has color 1, and house 3 has color 6.
24+
25+
The distance between them is abs(0 - 3) = 3.
26+
27+
Note that houses 3 and 6 can also produce the optimal answer.
28+
29+
**Example 2:**
30+
31+
![](https://assets.leetcode.com/uploads/2021/10/31/eg2.png)
32+
33+
**Input:** colors = [**1**,8,3,8,**3**]
34+
35+
**Output:** 4
36+
37+
**Explanation:** In the above image, color 1 is blue, color 8 is yellow, and color 3 is green.
38+
39+
The furthest two houses with different colors are house 0 and house 4.
40+
41+
House 0 has color 1, and house 4 has color 3.
42+
43+
The distance between them is abs(0 - 4) = 4.
44+
45+
**Example 3:**
46+
47+
**Input:** colors = [**0**,**1**]
48+
49+
**Output:** 1
50+
51+
**Explanation:** The furthest two houses with different colors are house 0 and house 1.
52+
53+
House 0 has color 0, and house 1 has color 1.
54+
55+
The distance between them is abs(0 - 1) = 1.
56+
57+
**Constraints:**
58+
59+
* `n == colors.length`
60+
* `2 <= n <= 100`
61+
* `0 <= colors[i] <= 100`
62+
* Test data are generated such that **at least** two houses have different colors.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g2001_2100.s2078_two_furthest_houses_with_different_colors;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void maxDistance() {
11+
assertThat(new Solution().maxDistance(new int[] {1, 1, 1, 6, 1, 1, 1}), equalTo(3));
12+
}
13+
14+
@Test
15+
void maxDistance2() {
16+
assertThat(new Solution().maxDistance(new int[] {1, 8, 3, 8, 3}), equalTo(4));
17+
}
18+
19+
@Test
20+
void maxDistance3() {
21+
assertThat(new Solution().maxDistance(new int[] {0, 1}), equalTo(1));
22+
}
23+
}

0 commit comments

Comments
 (0)