Skip to content

Commit 7254b6b

Browse files
committed
dsa add: Backtracking
1 parent 64b3d67 commit 7254b6b

File tree

1 file changed

+63
-2
lines changed

1 file changed

+63
-2
lines changed

dsa/README.md

+63-2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
3. [Algorithms](#-algorithms)
3838
- [Techniques and Tricks](#-techniques-and-tricks)
3939
- [Recursion](#-recursion)
40+
- [Backtracking](#-backtracking)
4041
- [The Gauss' Trick](#-the-gauss-trick)
4142
- [Related to Arrays](#-related-to-arrays)
4243
- [Kadane's Algorithm](#-kadanes-algorithm)
@@ -1769,9 +1770,69 @@ function reverseStr(str) {
17691770

17701771
<br>
17711772

1773+
### 🔷 Backtracking
1774+
1775+
Backtracking is a technique used to solve problems recursively by trying to build a solution step by step. If a solution is not valid, it backtracks (goes back) to the previous step and tries a different path.
1776+
1777+
> Backtracking is essential in situations where the problem requires exploring many possible solutions, and you need to find a valid solution by trying different possibilities.
1778+
1779+
**Example:**
1780+
1781+
- Given a set of numbers `[1, 2, 3]`, generate all possible subsets (including the empty set).
1782+
1783+
```js
1784+
function subsets(nums) {
1785+
let result = [];
1786+
1787+
function backtrack(start, currentSubset) {
1788+
result.push([...currentSubset]);
1789+
1790+
for (let i = start; i < nums.length; i++) {
1791+
currentSubset.push(nums[i]);
1792+
backtrack(i + 1, currentSubset);
1793+
currentSubset.pop();
1794+
}
1795+
}
1796+
1797+
backtrack(0, []);
1798+
return result;
1799+
}
1800+
1801+
subsets([1, 2, 3]); // [ [], [ 1 ], [ 1, 2 ], [ 1, 2, 3 ], [ 1, 3 ], [ 2 ], [ 2, 3 ], [ 3 ] ]
1802+
```
1803+
1804+
- Given `n` pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
1805+
1806+
```js
1807+
function generateParenthesis(n) {
1808+
const res = [];
1809+
backtrack(0, 0, n, res, "");
1810+
return res;
1811+
}
1812+
1813+
function backtrack(openN, closeN, n, res, stack) {
1814+
if (openN === closeN && openN === n) {
1815+
res.push(stack);
1816+
return;
1817+
}
1818+
1819+
if (openN < n) {
1820+
backtrack(openN + 1, closeN, n, res, stack + "(");
1821+
}
1822+
1823+
if (closeN < openN) {
1824+
backtrack(openN, closeN + 1, n, res, stack + ")");
1825+
}
1826+
}
1827+
1828+
generateParenthesis(3); // ["((()))","(()())","(())()","()(())","()()()"]
1829+
```
1830+
1831+
<br>
1832+
17721833
### 🔷 The Gauss' Trick
17731834

1774-
A method for efficiently `O(1)` finding the sum of an arithmetic series
1835+
A method for efficiently `O(1)` finding the sum of an arithmetic series.
17751836

17761837
The formula:
17771838

@@ -1907,7 +1968,7 @@ The Kadane's Algorithm is an efficient `O(n)` method to find the maximum sum of
19071968

19081969
Two Pointers is a technique used to solve problems efficiently by using two pointer variables to traverse an array or list. These pointers move in a specific way to find a solution.
19091970

1910-
Examples:
1971+
**Examples:**
19111972

19121973
- Given a sorted array, find two numbers that add up to a target:
19131974

0 commit comments

Comments
 (0)