如何解决并非所有风味CodeChef DSA学习第二周
问题指出,有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 举报,一经查实,本站将立刻删除。