|
37 | 37 | 3. [Algorithms](#-algorithms)
|
38 | 38 | - [Techniques and Tricks](#-techniques-and-tricks)
|
39 | 39 | - [Recursion](#-recursion)
|
| 40 | + - [Backtracking](#-backtracking) |
40 | 41 | - [The Gauss' Trick](#-the-gauss-trick)
|
41 | 42 | - [Related to Arrays](#-related-to-arrays)
|
42 | 43 | - [Kadane's Algorithm](#-kadanes-algorithm)
|
@@ -1769,9 +1770,69 @@ function reverseStr(str) {
|
1769 | 1770 |
|
1770 | 1771 | <br>
|
1771 | 1772 |
|
| 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 | + |
1772 | 1833 | ### 🔷 The Gauss' Trick
|
1773 | 1834 |
|
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. |
1775 | 1836 |
|
1776 | 1837 | The formula:
|
1777 | 1838 |
|
@@ -1907,7 +1968,7 @@ The Kadane's Algorithm is an efficient `O(n)` method to find the maximum sum of
|
1907 | 1968 |
|
1908 | 1969 | 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.
|
1909 | 1970 |
|
1910 |
| -Examples: |
| 1971 | +**Examples:** |
1911 | 1972 |
|
1912 | 1973 | - Given a sorted array, find two numbers that add up to a target:
|
1913 | 1974 |
|
|
0 commit comments