Count All Possible Routes LeetCode solution in C++
1class Solution {2public:3int fun(vector<int> &arr, int s, int f, int k, vector<vector<int>> &dp) {4if (k < 0) return 0;5if (dp[s][k] != -1) return dp[s][k];6int ans = 0;7if (s == f) ans = 1;8for (int i = 0; i < arr.size(); i++) {9if (i != s) ans = (ans + fun(arr, i, f, k - abs(arr[s] - arr[i]), dp)) % 1000000007;10}11return dp[s][k] = ans;12}1314int countRoutes(vector<int> &locations, int start, int finish, int fuel) {15vector<vector<int>> dp(locations.size() + 1, vector<int>(fuel + 1, -1));16int ans = fun(locations, start, finish, fuel, dp);17return ans;18}19};