I like to read this solution as a small machine: keep the useful information, throw away the noise. For 780. Reaching Points, the solution in this repository is mainly a backtracking 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: backtracking.
The notes already sitting in the source point us in the right direction:
- backtracking
- 100 / 190 test cases passed.
Guide
When?
Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are backtrack, reachingPoints.
Guide
Why?
The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.
- 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(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//backtracking
02//Runtime Error
03//100 / 190 test cases passed.
04class Solution {
05public:
06 int tx, ty;
07
08 bool backtrack(int x, int y){
09 if(x > tx || y > ty){
10 return false;
11 }
12
13 if(x == tx && y == ty){
14 return true;
15 }
16
17 return backtrack(x+y, y) || backtrack(x, x+y);
18 };
19
20 bool reachingPoints(int sx, int sy, int tx, int ty) {
21 this->tx = tx;
22 this->ty = ty;
23 return backtrack(sx, sy);
24 }
25};
26
27//https://leetcode.com/problems/reaching-points/discuss/114856/JavaC%2B%2BPython-Modulo-from-the-End
28//Runtime: 0 ms, faster than 100.00% of C++ online submissions for Reaching Points.
29//Memory Usage: 5.9 MB, less than 50.00% of C++ online submissions for Reaching Points.
30class Solution {
31public:
32 bool reachingPoints(int sx, int sy, int tx, int ty) {
33 while(tx > sx && ty > sy){
34 /*
35 (x,y) -> (x,x+y) or (x+y,x)
36 so the larger one in {tx, ty} must be
37 the one increased from last step,
38 to restore to the previous state,
39 we reduce the addend from it
40 */
41 if(tx > ty) tx %= ty;
42 else ty %= tx;
43 }
44
45 return tx == sx && ty >= sy && (ty-sy)%sx == 0 ||
46 ty == sy && tx >= sx && (tx-sx)%sy == 0;
47 }
48};
Cost