返回介绍

solution / 2000-2099 / 2078.Two Furthest Houses With Different Colors / README_EN

发布于 2024-06-17 01:03:11 字数 5831 浏览 0 评论 0 收藏 0

2078. Two Furthest Houses With Different Colors

中文文档

Description

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 ith house.

Return _the maximum distance between two houses with different colors_.

The distance between the ith and jth houses is abs(i - j), where abs(x) is the absolute value of x.

 

Example 1:

Input: colors = [1,1,1,6,1,1,1]
Output: 3
Explanation: In the above image, color 1 is blue, and color 6 is red.
The furthest two houses with different colors are house 0 and house 3.
House 0 has color 1, and house 3 has color 6. The distance between them is abs(0 - 3) = 3.
Note that houses 3 and 6 can also produce the optimal answer.

Example 2:

Input: colors = [1,8,3,8,3]
Output: 4
Explanation: In the above image, color 1 is blue, color 8 is yellow, and color 3 is green.
The furthest two houses with different colors are house 0 and house 4.
House 0 has color 1, and house 4 has color 3. The distance between them is abs(0 - 4) = 4.

Example 3:

Input: colors = [0,1]
Output: 1
Explanation: The furthest two houses with different colors are house 0 and house 1.
House 0 has color 0, and house 1 has color 1. The distance between them is abs(0 - 1) = 1.

 

Constraints:

  • n == colors.length
  • 2 <= n <= 100
  • 0 <= colors[i] <= 100
  • Test data are generated such that at least two houses have different colors.

Solutions

Solution 1

class Solution:
  def maxDistance(self, colors: List[int]) -> int:
    ans, n = 0, len(colors)
    for i in range(n):
      for j in range(i + 1, n):
        if colors[i] != colors[j]:
          ans = max(ans, abs(i - j))
    return ans
class Solution {
  public int maxDistance(int[] colors) {
    int ans = 0, n = colors.length;
    for (int i = 0; i < n; ++i) {
      for (int j = i + 1; j < n; ++j) {
        if (colors[i] != colors[j]) {
          ans = Math.max(ans, Math.abs(i - j));
        }
      }
    }
    return ans;
  }
}
class Solution {
public:
  int maxDistance(vector<int>& colors) {
    int ans = 0, n = colors.size();
    for (int i = 0; i < n; ++i)
      for (int j = i + 1; j < n; ++j)
        if (colors[i] != colors[j])
          ans = max(ans, abs(i - j));
    return ans;
  }
};
func maxDistance(colors []int) int {
  ans, n := 0, len(colors)
  for i := 0; i < n; i++ {
    for j := i + 1; j < n; j++ {
      if colors[i] != colors[j] {
        ans = max(ans, abs(i-j))
      }
    }
  }
  return ans
}

func abs(x int) int {
  if x >= 0 {
    return x
  }
  return -x
}

Solution 2

class Solution:
  def maxDistance(self, colors: List[int]) -> int:
    n = len(colors)
    if colors[0] != colors[-1]:
      return n - 1
    i, j = 1, n - 2
    while colors[i] == colors[0]:
      i += 1
    while colors[j] == colors[0]:
      j -= 1
    return max(n - i - 1, j)
class Solution {
  public int maxDistance(int[] colors) {
    int n = colors.length;
    if (colors[0] != colors[n - 1]) {
      return n - 1;
    }
    int i = 0, j = n - 1;
    while (colors[++i] == colors[0])
      ;
    while (colors[--j] == colors[0])
      ;
    return Math.max(n - i - 1, j);
  }
}
class Solution {
public:
  int maxDistance(vector<int>& colors) {
    int n = colors.size();
    if (colors[0] != colors[n - 1]) return n - 1;
    int i = 0, j = n;
    while (colors[++i] == colors[0])
      ;
    while (colors[--j] == colors[0])
      ;
    return max(n - i - 1, j);
  }
};
func maxDistance(colors []int) int {
  n := len(colors)
  if colors[0] != colors[n-1] {
    return n - 1
  }
  i, j := 1, n-2
  for colors[i] == colors[0] {
    i++
  }
  for colors[j] == colors[0] {
    j--
  }
  return max(n-i-1, j)
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文