Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static Integer number_of_paths(ArrayList<ArrayList<Integer>> matrix) {
- // We are going to use the Lazy manager approach to solve this problem.
- // We start at the top left corner. the formula that describe this problem
- // is given by the following :
- // NOW(r, c) = NOW(r, c-1) + NOW(r-1, c). OR NOW(r, c) = NOW(r, c+1) + NOW(r+1, c)
- int nr = matrix.size();
- int nc = matrix.get(0).size();
- int modulo = 1000000007;
- long[][] now = new long[nr][nc];
- now[0][0] = matrix.get(0).get(0);
- for (int r = 0; r < nr; r++) {
- for (int c = 0; c < nc; c++) {
- if (matrix.get(r).get(c) == 1 || (r == 0 && c == 0)) {
- continue;
- }
- now[r][c] = (c >= 1 ? now[r][c - 1] : 0) % modulo +
- (r >= 1 ? now[r - 1][c] : 0) % modulo;
- }
- }
- return (int)now[nr-1][nc-1] % modulo;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement