Java Top Down DP
Simple dfs + memorization to avoid duplicate calc.\nroute fr -> dest is\nsigma(fr -> i ... dest) where i != fr, and i is in [0, n -1];\n\n\nclass Solution {\n  
Simple dfs + memorization to avoid duplicate calc.\nroute fr -> dest is\nsigma(fr -> i ... dest) where i != fr, and i is in [0, n -1];\n\n```\nclass Solution {\n    int n, dp[][], cs[], dest, mod = 1_000_000_007;\n    public int countRoutes(int[] locations, int fr, int to, int f) {\n        cs = locations; n = cs.length; dp = new int[n][f + 1]; dest = to;\n        for (int i = 0; i < n; i++) Arrays.fill(dp[i], -1);\n        return dfs(fr, f);  //route fr...dest\n    }\n    \n    private int dfs(int fr, int f) {\n        if (f < 0) return 0;\n        if (dp[fr][f] > -1) return dp[fr][f];\n        int res = fr == dest ? 1 : 0;\n        for (int i = 0; i < n; i++) {\n            if (i == fr) continue;\n            res = (res + dfs(i, f - Math.abs(cs[i] - cs[fr]))) % mod;  // route fr -> i ... dest;\n        }\n        dp[fr][f] = res;\n        return dp[fr][f];\n    }\n}\n```
[]