java.lang.Object
g2001_2100.s2078_two_furthest_houses_with_different_colors.Solution

public class Solution extends Object
2078 - Two Furthest Houses With Different Colors.<p>Easy</p> <p>There are <code>n</code> houses evenly lined up on the street, and each house is beautifully painted. You are given a <strong>0-indexed</strong> integer array <code>colors</code> of length <code>n</code>, where <code>colors[i]</code> represents the color of the <code>i<sup>th</sup></code> house.</p> <p>Return <em>the <strong>maximum</strong> distance between <strong>two</strong> houses with <strong>different</strong> colors</em>.</p> <p>The distance between the <code>i<sup>th</sup></code> and <code>j<sup>th</sup></code> houses is <code>abs(i - j)</code>, where <code>abs(x)</code> is the <strong>absolute value</strong> of <code>x</code>.</p> <p><strong>Example 1:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2021/10/31/eg1.png" alt="" /></p> <p><strong>Input:</strong> colors = [<strong>1</strong> ,1,1, <strong>6</strong> ,1,1,1]</p> <p><strong>Output:</strong> 3</p> <p><strong>Explanation:</strong> In the above image, color 1 is blue, and color 6 is red.</p> <p>The furthest two houses with different colors are house 0 and house 3.</p> <p>House 0 has color 1, and house 3 has color 6.</p> <p>The distance between them is abs(0 - 3) = 3.</p> <p>Note that houses 3 and 6 can also produce the optimal answer.</p> <p><strong>Example 2:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2021/10/31/eg2.png" alt="" /></p> <p><strong>Input:</strong> colors = [<strong>1</strong> ,8,3,8, <strong>3</strong> ]</p> <p><strong>Output:</strong> 4</p> <p><strong>Explanation:</strong> In the above image, color 1 is blue, color 8 is yellow, and color 3 is green.</p> <p>The furthest two houses with different colors are house 0 and house 4.</p> <p>House 0 has color 1, and house 4 has color 3.</p> <p>The distance between them is abs(0 - 4) = 4.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> colors = [<strong>0</strong> , <strong>1</strong> ]</p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong> The furthest two houses with different colors are house 0 and house 1.</p> <p>House 0 has color 0, and house 1 has color 1.</p> <p>The distance between them is abs(0 - 1) = 1.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == colors.length</code></li> <li><code>2 <= n <= 100</code></li> <li><code>0 <= colors[i] <= 100</code></li> <li>Test data are generated such that <strong>at least</strong> two houses have different colors.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • maxDistance

      public int maxDistance(int[] colors)