saran.1191
BAN USERpublic static void findSeq(int[][] grid, int[] values) {
for(int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
findSeqFromGivenPoint(grid, i, j, values, 0);
}
}
}
public static void findSeqFromGivenPoint(int[][] grid, int i, int j, int[] values, int level) {
values[level] = grid[i][j];
if(i < grid.length && j < grid[0].length) {
if((j + 1) < grid[0].length && Math.abs(grid[i][j] - grid[i][j + 1]) == 1) {
findSeqFromGivenPoint(grid, i, j + 1, values, level + 1);
}
if((i + 1) < grid.length && Math.abs(grid[i][j] - grid[i + 1][j]) == 1) {
findSeqFromGivenPoint(grid, i + 1, j, values, level + 1);
}
if(level > 0) {
ArrayList<Integer> finalList = new ArrayList<>();
for(int k = 0; k <= level; k++) {
finalList.add(values[k]);
}
list.add(finalList);
}
}
}
We can calculate it using the formula
(0.5) A + (0.3)B + (0.2)C
For iteration 1: Let people = 6;
(0.5) 6 + (0.3) 6 + (0.2) 6 = 3.0 + 2.0 + 1.0 = 6
For iteration 2: Let people = 7;
(0.5) 7 + (0.3) 7 + (0.2) 7 = 3.5 + 2.1 + 1.4 = 4(approx) + 2 + 1 = 7
For iteration 3: Let people = 8;
(0.5) 8 + (0.3) 8 + (0.2) 8 = 4.0 + 2.4 + 1.6 = 4 + 2 + 2(approx) = 8
For iteration 4: Let people = 9;
(0.5) 9 + (0.3) 9 + (0.2) 9 = 4.5 + 2.7 + 1.8 = 5(approx) + 2 + 2 = 9 (Giving priority to max value to have minimum difference in percentage).
And so on.
Here is the code I tried. Adding a helper method to check if I have to step down or step right at each iteration
public static int minSteps(int m, int n) {
// A(i, j) = A(i+j , i)
// A(i, j) = A(i , i+j)
int i = 1, j = 1;
int steps = 0;
while(i != m || j != n) {
if(canStepDown(m,n,i,j)) {
i += j;
} else {
j += i;
}
steps++;
System.out.println("Step " + steps + " :: i = " + i + " j = " + j );
}
return steps;
}
private static boolean canStepDown(int m, int n, int currM, int currN) {
int updatedM = currM + currN;
if(updatedM <= m && (n == currN || (updatedM + n) <= n)) {
return true;
}
return false;
}
- saran.1191 April 15, 2015