I've been stuck in this problem for a while. The goal is to return an Arraylist of the longest repetitive sequence. If I have
int[][] a = {{ 1, 1, 3, 4 }
{ 2, 2, 2, 1}
{ 3, 3, 3, 3}
{ 0, 0, 1, 1}};
The method longestSequence()
should return an Arraylist of [3,3,3,3]
as 3
has the longest sequence. I have to find sequence only horizontally. Please could smb tell me what I did wrong?
int[][] a = {{ 1, 1, 3, 4 }
{ 2, 2, 2, 1}
{ 3, 3, 3, 3}
{ 0, 0, 1, 1}};
public List<Integer> longestSequence(int[][]a){
int count = 1, max = 1;
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < a.length; i++)
for(int j = 1; j < a[i].length; j++) {
if (a[i][j] >= a[i][j-1]) {
count++;
list.add(a[i][j]);
} else {
if (count > max) {
max = count;
}
list.clear();
count = 1;
}
}
return list;
}