小华和小薇一起通过玩积木游戏学习数学。
他们有很多积木,每个积木块上都有一个数字,
积木块上的数字可能相同。
小华随机拿一些积木挨着排成一排,请小薇找到这排积木中数字相同且所处位置最远的2块积木块,计算他们的距离。
小薇请你帮忙替她解决这个问题。
第一行输入为 ,表示小华排成一排的积木总数。
接下来 行每行一个数字,表示小花排成一排的积木上数字。
相同数字的积木的位置最远距离;
如果所有积木数字都不相同,请返回 -1
5
1
2
3
1
4
3
2
1
2
-1
0 <= 积木上的数字 < 10^9
1 <= 积木长度 <= 10^5
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
* Author: Amos
* E-mail: amos@amoscloud.com
* Date: 2022/12/2
* Time: 14:08
* Description: 100%
*/
public class Main0171 {
public static void main(String[] args) {
try (Scanner scanner = new Scanner(System.in)) {
int n = scanner.nextInt();
int[] ints = new int[n];
for (int i = 0; i < n; i++) {
ints[i] = scanner.nextInt();
}
int res = solution(ints);
System.out.println(res);
}
}
private static int solution(int[] ints) {
int maxLen = -1;
if (ints.length == 0) {
return maxLen;
}
Map<Integer, Integer> numCount = new HashMap<>();
for (int i : ints) {
numCount.put(i, numCount.getOrDefault(i, 0) + 1);
}
if (numCount.entrySet().size() == ints.length) {
return maxLen;
}
for (Map.Entry<Integer, Integer> entry : numCount.entrySet()) {
Integer number = entry.getKey();
Integer count = entry.getValue();
if (count < 2) {
continue;
}
int lo = 0, hi = ints.length - 1;
while (ints[lo] != number) {
lo++;
}
while (ints[hi] != number) {
hi--;
}
maxLen = Math.max(maxLen, hi - lo);
}
return maxLen;
}
}