2078. Two Furthest Houses With Different Colors Easy

@problem@discussion
#Array#Greedy



1/**
2 * [2078] Two Furthest Houses With Different Colors
3 *
4 * 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 i^th house.
5 * Return the maximum distance between two houses with different colors.
6 * The distance between the i^th and j^th houses is abs(i - j), where abs(x) is the absolute value of x.
7 *  
8 * Example 1:
9 * <img alt="" src="https://assets.leetcode.com/uploads/2021/10/31/eg1.png" style="width: 610px; height: 84px;" />
10 * Input: colors = [<u>1</u>,1,1,<u>6</u>,1,1,1]
11 * Output: 3
12 * Explanation: In the above image, color 1 is blue, and color 6 is red.
13 * The furthest two houses with different colors are house 0 and house 3.
14 * House 0 has color 1, and house 3 has color 6. The distance between them is abs(0 - 3) = 3.
15 * Note that houses 3 and 6 can also produce the optimal answer.
16 * 
17 * Example 2:
18 * <img alt="" src="https://assets.leetcode.com/uploads/2021/10/31/eg2.png" style="width: 426px; height: 84px;" />
19 * Input: colors = [<u>1</u>,8,3,8,<u>3</u>]
20 * Output: 4
21 * Explanation: In the above image, color 1 is blue, color 8 is yellow, and color 3 is green.
22 * The furthest two houses with different colors are house 0 and house 4.
23 * House 0 has color 1, and house 4 has color 3. The distance between them is abs(0 - 4) = 4.
24 * 
25 * Example 3:
26 * 
27 * Input: colors = [<u>0</u>,<u>1</u>]
28 * Output: 1
29 * Explanation: The furthest two houses with different colors are house 0 and house 1.
30 * House 0 has color 0, and house 1 has color 1. The distance between them is abs(0 - 1) = 1.
31 * 
32 *  
33 * Constraints:
34 * 
35 * 	n == colors.length
36 * 	2 <= n <= 100
37 * 	0 <= colors[i] <= 100
38 * 	Test data are generated such that at least two houses have different colors.
39 * 
40 */
41pub struct Solution {}
42
43// problem: https://leetcode.com/problems/two-furthest-houses-with-different-colors/
44// discuss: https://leetcode.com/problems/two-furthest-houses-with-different-colors/discuss/?currentPage=1&orderBy=most_votes&query=
45
46// submission codes start here
47
48impl Solution {
49    pub fn max_distance(colors: Vec<i32>) -> i32 {
50        0
51    }
52}
53
54// submission codes end
55
56#[cfg(test)]
57mod tests {
58    use super::*;
59
60    #[test]
61    fn test_2078() {
62    }
63}
64


Back
© 2025 bowen.ge All Rights Reserved.