题目描述
在一个狭小的路口,每秒只能通过一辆车,假好车辆的颜色只有3种,找出N秒内经过的最多颜色的车辆数量。
三种颜色编号为0,1,2
输入描述
第—行输入的是通过的车辆颜色信息
[0,1,1,2]代表4秒钟通过的车辆颜色分别是0,1 ,1 ,2
第二行输入的是统计时间窗,整型,单位为秒
输出描述
输出指定时间窗内经过的最多颜色的车辆数量。
示例1输入输出示例仅供调试,后台判断数据─般不包含示例
输入
0 1 2 1
3
输出
2
样例解释
在3秒时间窗内,每个颜色最多出现2次。例如:[1,2,1]
个人解法
不保证通过率
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class 最多颜色的车辆 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] strings = br.readLine().split(" ");
int[] status = new int[3];
String s = br.readLine();
int count = Integer.parseInt(s);
for (int i = 0; i < count; i++) {
int parseInt = Integer.parseInt(strings[i]);
status[parseInt] += 1;
}
int[] copy = status.clone();
Arrays.sort(copy);
int max = copy[2];
for (int i = 0; i < strings.length - count; i++) {
int parseInt = Integer.parseInt(strings[i]);
int anInt = Integer.parseInt(strings[i + count]);
status[parseInt] -= 1;
status[anInt] += 1;
copy = status.clone();
Arrays.sort(copy);
if (copy[2] > max)
max = copy[2];
}
System.out.println(max);
}
}