Reputation:
List<List<Integer>> triangle = new ArrayList<List<Integer>>();
triangle.add(Arrays.asList(75));
triangle.add(Arrays.asList(64, 94));
triangle.add(Arrays.asList(82, 47, 82));
int max = 0;
int x = 0;
int y = 0;
int sum = 75;
int j = 0;
for (int i = 1; i < triangle.size() - 1; i++) {
FOUND: {
for (j = j; j <= triangle.get(i).size(); j++) {
x = triangle.get(i).get(j);
y = triangle.get(i).get(j + 1);
max = Math.max(x, y);
int index = triangle.get(i).indexOf(max);
System.out.println("INDEX: "+index);
j = index;
sum += max;
break FOUND;
}
}
}
}
Here when I am comparing let's say between 47 and 82 x.add(Arrays.asList(82, 47, 82))
. Here 47 is at index 1 and 82 is at index 2 and maximimum of two is 82, which is at index 2. So I want it to print index as 2 instead of 0.
Is there any way I can do that? Any help is appreciated!
Upvotes: 0
Views: 69
Reputation: 30809
You can use lastIndexOf
method to get the last index of that element, e.g.:
int index = triangle.get(i).lastIndexOf(max);
System.out.println("INDEX: "+index);
Here's an exmple of the same.
Upvotes: 1