问题描述
问题指出,有N个无序排列的K个不同风味的蛋糕。我需要打印连续段的最大长度,其中至少一种风味会丢失。
例如如果N = 20 K = 6并且数组为6 6 6 6 6 2 5 1 2 3 4 5 6 6 3 2 4 5 6 1 3则应打印11,因为从第8个元素到第18个元素的长度最大,并且风味1为失踪了。
我已经尝试了所有可以通过的测试案例,并且所有测试案例都通过了很好的测试。但是在提交时给出了错误的答案。有人可以帮我解决这个问题在哪里。
import java.util.*;
import java.lang.*;
import java.io.*;
class Codechef{
public static void main (String[] args) throws java.lang.Exception{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt(),a[];
long n,k,count=0,max=0;
while(t--!=0){
n=sc.nextLong();
k=sc.nextLong();
a=new int[(int)n];
for(int i=0;i<n;i++) a[i]=sc.nextInt();
if(k>n) System.out.println(n);
else{
for(int i=1;i<=k;i++){
for(int j=0;j<n;j++){
if(a[j]==i) count=0;
else{
count++;
max=max>count?max:count;
}
}
if(count==n) break;
count=0;
}
System.out.println(max);
}
}
}
}
这是我的代码。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)