You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/cheatsheet/backtrack.md
+11-12Lines changed: 11 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -100,7 +100,7 @@ These are often **permutation problems**, where:
100
100
- You **should revisit** earlier choices (sometimes)
101
101
102
102
103
-
-> Don’t use `start_idx` when:
103
+
-> Don't use `start_idx` when:
104
104
- You're generating **permutations**
105
105
- You need **all orderings**
106
106
- Choices are not sequential (e.g., trying all positions)
@@ -789,10 +789,10 @@ if (i > 0 && nums[i] == nums[i - 1] && !used[i - 1])
789
789
790
790
// dfsFind(board, word, x+1, y, visited, start_idx + 1)
791
791
792
-
// 1) You’re passing a copy of start_idx + 1 to the recursive function. So, inside the recursive call, start_idx is a new variable, and changes to it won’t affect the start_idx in the calling function.
792
+
// 1) You're passing a copy of start_idx + 1 to the recursive function. So, inside the recursive call, start_idx is a new variable, and changes to it won't affect the start_idx in the calling function.
793
793
794
794
795
-
// 2) We don’t need start_idx -= 1; because start_idx is passed by value, not by reference. So modifying it in the recursive call doesn’t affect the caller’s start_idx. We’re already handling the correct index in each recursive call by passing start_idx + 1.
795
+
// 2) We don't need start_idx -= 1; because start_idx is passed by value, not by reference. So modifying it in the recursive call doesn't affect the caller's start_idx. We're already handling the correct index in each recursive call by passing start_idx + 1.
796
796
797
797
```
798
798
@@ -1092,7 +1092,7 @@ private boolean dfs_(char[][] board, int y, int x, int idx, String word, boolean
@@ -1033,7 +1033,7 @@ public int[][] updateMatrix_0_1(int[][] mat) {
1033
1033
*
1034
1034
* * You perform a **BFS for every 1** in the matrix.
1035
1035
* * In worst case, you scan the whole matrix **once per 1**.
1036
-
* * That’s **O(N × M × (N + M))** — very slow for large inputs.
1036
+
* * That's **O(N × M × (N + M))** — very slow for large inputs.
1037
1037
*
1038
1038
* ---
1039
1039
*
@@ -1043,7 +1043,7 @@ public int[][] updateMatrix_0_1(int[][] mat) {
1043
1043
*
1044
1044
* #### Why this works:
1045
1045
*
1046
-
* * You flip the problem: instead of asking *“how far is this 1 from a 0?”*, you ask *“how far can each 0 reach a 1?”*
1046
+
* * You flip the problem: instead of asking *"how far is this 1 from a 0?"*, you ask *"how far can each 0 reach a 1?"*
1047
1047
* * When you expand from all 0s **at the same time**, you ensure that **each 1 gets the shortest path to a 0**, because BFS guarantees minimum-distance traversal.
1048
1048
* * Time complexity is **O(N × M)** — each cell is visited only once.
1049
1049
*
@@ -1057,7 +1057,7 @@ public int[][] updateMatrix_0_1(int[][] mat) {
0 commit comments