[Coding Made Simple] Total ways in matrix
Given a 2 dimensional matrix, how many ways you can reach bottom right from top left provided you can only move down and right.
1 public class TotalWaysInMatrix { 2 public int getTotalWays(int[][] matrix) { 3 if(matrix == null || matrix.length == 0 || matrix[0].length == 0) { 4 return 0; 5 } 6 int[][] T = new int[matrix.length][matrix[0].length]; 7 for(int i = 0; i < T.length; i++) { 8 T[i][0] = 1; 9 } 10 for(int j = 0; j < T[0].length; j++) { 11 T[0][j] = 1; 12 } 13 for(int i = 1; i < T.length; i++) { 14 for(int j = 1; j < T[0].length; j++) { 15 T[i][j] = T[i - 1][j] + T[i][j - 1]; 16 } 17 } 18 return T[T.length - 1][T[0].length - 1]; 19 } 20 }