华为OD题目: 通信误码
package com.sf.ccmas.video.config.odd.od15;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();
String[] split = line.split(" ");
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < split.length; i++) {
int num = Integer.parseInt(split[i]);
map.put(num, map.getOrDefault(num, 0) + 1);
}
List<Map.Entry<Integer, Integer>> list = new ArrayList<>(map.entrySet());
list.sort((a1, a2) -> a2.getValue() - a1.getValue());
int max = list.get(0).getValue();
Map<Integer, List<Integer>> maxNumMap = new HashMap<>();
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
if (entry.getValue().equals(max)) {
maxNumMap.put(entry.getKey(), new ArrayList<>());
}else {
break;
}
}
for (int i = 0; i < split.length; i++) {
int num = Integer.parseInt(split[i]);
if (maxNumMap.containsKey(num)) {
List<Integer> indexList = maxNumMap.get(num);
indexList.add(i);
}
}
int res = Integer.MAX_VALUE;
for (List<Integer> indexList : maxNumMap.values()) {
int val = indexList.get(indexList.size() - 1) - indexList.get(0) + 1;
res = Math.min(val, res);
}
System.out.println(res);
}
}