A good way into this one is to ask: what do we need to remember so we never redo work blindly? For 1359. Count All Valid Pickup and Delivery Options, the solution in this repository is mainly a straightforward implementation solution.
Guide
What?
The code is easier to read if we treat it as a controlled search through possible states. 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: straightforward implementation.
The notes already sitting in the source point us in the right direction:
- https://leetcode.com/problems/count-all-valid-pickup-and-delivery-options/discuss/516968/JavaC%2B%2BPython-Easy-and-Concise
- Recursion
Guide
When?
Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are rCountOrders, countOrders.
Guide
Why?
The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.
- 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:
- Read the setup variables first.
- Follow the main loop or recursive helper next.
- Watch where invalid states get skipped.
- Check which value survives to the return statement.
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(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//https://leetcode.com/problems/count-all-valid-pickup-and-delivery-options/discuss/516968/JavaC%2B%2BPython-Easy-and-Concise
02//Recursion
03/Runtime: 0 ms, faster than 100.00% of C++ online submissions for Count All Valid Pickup and Delivery Options.
04//Memory Usage: 7.4 MB, less than 100.00% of C++ online submissions for Count All Valid Pickup and Delivery Options.
05
06class Solution {
07public:
08 int rCountOrders(int n, long res){
09 return n > 0 ? rCountOrders(n-1, res * (2*n-1) * n % (long(1e9 + 7))) : res;
10 //overflow
11 // return n > 0 ? (2*n-1) * (2*n) /2 * rCountOrders(n-1, res) % long(pow(10, 9) + 7) : res;
12 };
13
14 int countOrders(int n) {
15 return rCountOrders(n, 1);
16 }
17};
18
19//Bottom-up
20//Runtime: 0 ms, faster than 100.00% of C++ online submissions for Count All Valid Pickup and Delivery Options.
21//Memory Usage: 7.5 MB, less than 100.00% of C++ online submissions for Count All Valid Pickup and Delivery Options.
22class Solution {
23public:
24 int countOrders(int n) {
25 //use long to avoid overflow
26 long res = 1, mod = 1e9+7;
27
28 for(int i = 1; i <= n; i++){
29 res = res * (2*i-1) * i % mod;
30 }
31
32 return (int)res;
33 }
34};
Cost