코린이의 소소한 공부노트

[LeetCode/Easy] 2078. Two Furthest Houses With Different Colors 본문

코딩테스트 풀이/JAVA

[LeetCode/Easy] 2078. Two Furthest Houses With Different Colors

무지맘 2023. 4. 26. 19:57

1. Input

1) int[] colors

- colors[i] == i번째 집의 색을 나타내는 숫자

 

2. Output

1) 색이 다른 집의 거리 차의 최댓값을 반환

- 거리 >= 0이므로 반환 값도 0 이상이다.

 

3. Constraint

1) n == colors.length

2) 2 <= n <= 100

3) 0 <= colors[i] <= 100

4) 테스트 케이스에는 반드시 색이 다른 집이 최소 2개 있다.

 

4. Example

Input: colors = [1,8,3,8,3] -> Output: 4

설명: 0번째와 4번째가 가장 멀다.

 

5. Code

1) 첫 코드(2023/04/26)

class Solution {
    public int maxDistance(int[] colors) {
        int max = 0;
        for(int i=0 ; i<colors.length-1 ; i++)
            for(int j=i+1 ; j<colors.length ; j++)
                if(colors[i]!=colors[j])
                    max = Math.max(j-i, max);
        return max;
    }
}