第四题 离散化+滑动窗口,不知道写的对不对,有巨佬看看有问题没 public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int K = sc.nextInt(); int cnt = 0; int[] C = new int[N]; Map<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < N; i++) { int t = sc.nextInt(); if (!map.containsKey(t)){ map.put(t, cnt++); } C[i] = map.get(t); } int[] count = new int[cnt]; int left=0, right=0, result=0, maxCount=0; while (right<C.length){ count[C[right]]++; maxCount = Math.max(maxCount, count[C[right]]); if (right-left+1-maxCount>K){ count[C[left]]--; left++; } right++; } System.out.println(maxCount); }