upvote
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    int solve(vector<int>& locations, int currCity, int finish, int remainingFuel,\n              vector<vector<int>>& memo) {\n        if (remainingFuel < 0) {\n            return 0;\n        }\n        if (memo[currCity][remainingFuel] != -1) {\n            return memo[currCity][remainingFuel];\n        }\n\n        int ans = currCity == finish ? 1 : 0;\n        for (int nextCity = 0; nextCity < locations.size(); nextCity++) {\n            if (nextCity != currCity) {\n                ans = (ans + solve(locations, nextCity, finish,\n                                   remainingFuel - abs(locations[currCity] - locations[nextCity]),\n                                   memo)) % 1000000007;\n            }\n        }\n\n        return memo[currCity][remainingFuel] = ans;\n    }\n\n    int countRoutes(vector<int>& locations, int start, int finish, int fuel) {\n        int n = locations.size();\n        vector<vector<int>> memo(n, vector<int>(fuel + 1, -1));\n\n        return solve(locations, start, finish, fuel, memo);\n    }\n};\n```
['C++']