classSolution { private: std::unordered_map<int, int> _map; // Use a map to keep previous results. public: intmemorizatio(int n){ // Check if the answer has been calculated. if (_map.find(n) != _map.end()) return _map[n]; int ans = 0;
// Base case // ans = ...
// Recursion relation // ans = ...
// Save results _map[n] = ans;
return ans; } };
2. Divide and Conquer
Divide Divide the problem $S$ into subproblems ${S_1, S_2, …S_n}$, where $n >= 2$.
Conquer Solve each subproblem.
Combine Combine the results of each subproblem.
1 2 3 4 5 6 7 8 9 10 11 12 13
booldivideAndConquer(S){ // Base case // 1. Null detection // 2. Solve the minimal subproblem
// Divide the problem and solve subproblems bool res1 = divideAndConquer(S1); bool res2 = divideAndConquer(S2); // ...
voidbacktracking(candidate){ // Return if candidate is valid. if (candidate) return;
// Iterate all possible candidates. for (auto candi : candidates) { // Make sure candi is valid if (candi) { // 1. Modify candi modify(candi) // 2. Run backtracking backtracking(modified(candi)); // 3. Revert the change revert(candi); } } }