|
| 1 | +package dynamic_programming; |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | +import java.util.stream.Collectors; |
| 5 | + |
| 6 | +/** |
| 7 | + * Created by gouthamvidyapradhan on 13/04/2021 You are given a square board of characters. You can |
| 8 | + * move on the board starting at the bottom right square marked with the character 'S'. |
| 9 | + * |
| 10 | + * <p>You need to reach the top left square marked with the character 'E'. The rest of the squares |
| 11 | + * are labeled either with a numeric character 1, 2, ..., 9 or with an obstacle 'X'. In one move you |
| 12 | + * can go up, left or up-left (diagonally) only if there is no obstacle there. |
| 13 | + * |
| 14 | + * <p>Return a list of two integers: the first integer is the maximum sum of numeric characters you |
| 15 | + * can collect, and the second is the number of such paths that you can take to get that maximum |
| 16 | + * sum, taken modulo 10^9 + 7. |
| 17 | + * |
| 18 | + * <p>In case there is no path, return [0, 0]. |
| 19 | + * |
| 20 | + * <p>Example 1: |
| 21 | + * |
| 22 | + * <p>Input: board = ["E23","2X2","12S"] Output: [7,1] Example 2: |
| 23 | + * |
| 24 | + * <p>Input: board = ["E12","1X1","21S"] Output: [4,2] Example 3: |
| 25 | + * |
| 26 | + * <p>Input: board = ["E11","XXX","11S"] Output: [0,0] |
| 27 | + * |
| 28 | + * <p>Constraints: |
| 29 | + * |
| 30 | + * <p>2 <= board.length == board[i].length <= 100 Solution: O(N x N) where N is the length of board. |
| 31 | + */ |
| 32 | +public class NumberOfPathsWithMaxScore { |
| 33 | + public static void main(String[] args) { |
| 34 | + String[] board = {"E11", "XXX", "11S"}; |
| 35 | + List<String> input = Arrays.stream(board).collect(Collectors.toList()); |
| 36 | + int[] r = new NumberOfPathsWithMaxScore().pathsWithMaxScore(input); |
| 37 | + System.out.println(r[0] + " " + r[1]); |
| 38 | + } |
| 39 | + |
| 40 | + long[][] M, N; |
| 41 | + final int[] R = {0, 1, 1}; |
| 42 | + final int[] C = {1, 1, 0}; |
| 43 | + int MOD = (int) 1e9 + 7; |
| 44 | + |
| 45 | + public int[] pathsWithMaxScore(List<String> board) { |
| 46 | + M = new long[board.size()][board.get(0).length()]; |
| 47 | + N = new long[board.size()][board.get(0).length()]; |
| 48 | + N[board.size() - 1][board.get(0).length() - 1] = 1; |
| 49 | + for (int i = board.size() - 1; i >= 0; i--) { |
| 50 | + for (int j = board.get(i).length() - 1; j >= 0; j--) { |
| 51 | + char curr = board.get(i).charAt(j); |
| 52 | + if (curr != 'X') { |
| 53 | + int currInt = 0; |
| 54 | + if (curr != 'S' && curr != 'E') { |
| 55 | + currInt = Integer.parseInt(String.valueOf(curr)); |
| 56 | + } |
| 57 | + long currMax = -1; |
| 58 | + for (int k = 0; k < 3; k++) { |
| 59 | + int newR = i + R[k]; |
| 60 | + int newC = j + C[k]; |
| 61 | + if (newR < board.size() |
| 62 | + && newC < board.get(0).length() |
| 63 | + && board.get(newR).charAt(newC) != 'X' |
| 64 | + && N[newR][newC] != 0) { |
| 65 | + M[i][j] = Math.max(M[i][j], ((currInt + M[newR][newC]) % MOD)); |
| 66 | + long newMax = ((currInt + M[newR][newC]) % MOD); |
| 67 | + if (newMax > currMax) { |
| 68 | + currMax = newMax; |
| 69 | + N[i][j] = N[newR][newC]; |
| 70 | + } else if (newMax == currMax) { |
| 71 | + N[i][j] = ((N[newR][newC] + N[i][j]) % MOD); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + int[] res = new int[2]; |
| 79 | + res[0] = (int) M[0][0]; |
| 80 | + res[1] = (int) N[0][0]; |
| 81 | + return res; |
| 82 | + } |
| 83 | +} |
0 commit comments