Let's make this one less mysterious. For 1575. Count All Possible Routes, the solution in this repository is mainly a DFS + memoization solution.
Guide
What?
We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: DFS + memoization, dynamic programming.
The notes already sitting in the source point us in the right direction:
- recursion + memorization
- https://leetcode.com/problems/count-all-possible-routes/discuss/830400/Dynamic-Programming-or-Simple-Explanation
- time: O(fuel*N*N), space: O(fuel*N)
Guide
When?
This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are dfs, countRoutes.
Guide
Why?
The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Initialize the memory or helper structure.
- Process candidates in the order the invariant expects.
- Update the answer only when the current state is valid.
- Return the value that represents the fully processed input.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(fuel*N*N), space: O(fuel*N)
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//recursion + memorization
02//https://leetcode.com/problems/count-all-possible-routes/discuss/830400/Dynamic-Programming-or-Simple-Explanation
03//Runtime: 148 ms, faster than 28.57% of C++ online submissions for Count All Possible Routes.
04//Memory Usage: 8.8 MB, less than 100.00% of C++ online submissions for Count All Possible Routes.
05//time: O(fuel*N*N), space: O(fuel*N)
06class Solution {
07public:
08 int MOD = 1e9+7;
09 //location: [2,100], fuel: [1,200]
10 int memo[101][201];
11 int n;
12
13 int dfs(vector<int>& A, int finish, int cur, int cur_fuel){
14 if(cur_fuel < 0){
15 return 0;
16 }else if(memo[cur][cur_fuel] != -1){
17 return memo[cur][cur_fuel];
18 }else{
19 //cur_fuel >= 0
20 //if cur == finish, we have found a possible way
21 int ways = (cur == finish);
22
23 if(cur_fuel > 0){
24 for(int next = 0; next < n; ++next){
25 if(next == cur) continue;
26 ways = (ways +
27 dfs(A, finish, next, cur_fuel - abs(A[cur]-A[next]))) % MOD;
28 }
29 }
30
31 return memo[cur][cur_fuel] = ways;
32 }
33 };
34
35 int countRoutes(vector<int>& locations, int start, int finish, int fuel) {
36 /*
37 first we should observe that the "state" is (location, fuel),
38 so we can use recursion + memorization
39 */
40
41 n = locations.size();
42
43 memset(memo, -1, sizeof(memo));
44
45 return dfs(locations, finish, start, fuel);
46 }
47};
Cost