|
| 1 | +--- |
| 2 | +id: combinations |
| 3 | +title: Combinations(LeetCode) |
| 4 | +sidebar_label: 0077-Combinations |
| 5 | +tags: |
| 6 | + - Backtracking |
| 7 | +description: Given two integers n and k, return all possible combinations of k numbers chosen from the range [1, n]. |
| 8 | +--- |
| 9 | + |
| 10 | +## Problem Statement |
| 11 | + |
| 12 | +Given two integers `n` and `k`, return all possible combinations of `k` numbers chosen from the range `[1, n]`. |
| 13 | + |
| 14 | +You may return the answer in any order. |
| 15 | + |
| 16 | +### Examples |
| 17 | + |
| 18 | +**Example 1:** |
| 19 | + |
| 20 | +```plaintext |
| 21 | +Input: n = 4, k = 2 |
| 22 | +Output: [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]] |
| 23 | +Explanation: There are 4 choose 2 = 6 total combinations. |
| 24 | +Note that combinations are unordered, i.e., [1,2] and [2,1] are considered to be the same combination. |
| 25 | +``` |
| 26 | + |
| 27 | +**Example 2:** |
| 28 | + |
| 29 | +```plaintext |
| 30 | +Input: n = 1, k = 1 |
| 31 | +Output: [[1]] |
| 32 | +Explanation: There is 1 choose 1 = 1 total combination. |
| 33 | +``` |
| 34 | + |
| 35 | +### Constraints |
| 36 | + |
| 37 | +- `1 <= n <= 20` |
| 38 | + `1 <= k <= n` |
| 39 | + |
| 40 | +## Solution |
| 41 | + |
| 42 | +### Approach 1: Minimum Window Substring (Java Implementation) |
| 43 | + |
| 44 | +#### Algorithm |
| 45 | + |
| 46 | +1. Initialization: |
| 47 | +* Create a frequency map for characters in string `t`. |
| 48 | +* Initialize counters and pointers: `counter` (number of characters still needed from `t`), `begin` (start pointer), `end` (end pointer), `d` (length of the minimum window), and `head` (starting index of the minimum window). |
| 49 | +2. Expand the Window: |
| 50 | +* Traverse through string `s` with the `end` pointer. |
| 51 | +* If the current character is needed (frequency in `map` is greater than 0), decrement `counter`. |
| 52 | +* Decrease the frequency of the current character in the map. |
| 53 | +3. Contract the Window: |
| 54 | +* When `counter` is 0 (all characters from `t` are found in the window): |
| 55 | + * Check if the current window is smaller than the previously found minimum window (`d`). |
| 56 | + * Move the `begin` pointer to find a smaller valid window. |
| 57 | + * If the character at `begin` is in `t`, increment its frequency in the map and increment `counter` if the frequency becomes positive. |
| 58 | +4. Return Result: |
| 59 | +* Return the minimum window substring. |
| 60 | + |
| 61 | +#### Implementation |
| 62 | + |
| 63 | +```Java |
| 64 | +def backtrack(candidate): |
| 65 | + if find_solution(candidate): |
| 66 | + output(candidate) |
| 67 | + return |
| 68 | + |
| 69 | + # iterate all possible candidates |
| 70 | + for next_candidate in list_of_candidates: |
| 71 | + if is_valid(next_candidate): |
| 72 | + # try this partial candidate solution |
| 73 | + place(next_candidate) |
| 74 | + # explore further with the given candidate |
| 75 | + backtrack(next_candidate) |
| 76 | + # backtrack |
| 77 | + remove(next_candidate) |
| 78 | +``` |
| 79 | + |
| 80 | +### Complexity Analysis |
| 81 | + |
| 82 | +- **Time complexity**: $O(N)$ |
| 83 | +- **Space complexity**: $O(1)$ |
| 84 | + |
| 85 | +### Approach 2: Python Implementation |
| 86 | + |
| 87 | +#### Algorithm |
| 88 | + |
| 89 | +1. Initialization: |
| 90 | +* Create a frequency map for characters in string `t`. |
| 91 | +* Initialize counters and pointers: `needcnt` (number of characters still needed from `t`), `res` (tuple to store the start and end indices of the minimum window), and `start` (start pointer). |
| 92 | +2. Expand the Window: |
| 93 | +* Traverse through string `s` with the `end` pointer. |
| 94 | +* If the current character is needed (frequency in `needstr` is greater than 0), decrement `needcnt`. |
| 95 | +* Decrease the frequency of the current character in the map. |
| 96 | +3. Contract the Window: |
| 97 | +* When `needcnt` is 0 (all characters from `t` are found in the window): |
| 98 | + * Move the `start` pointer to find a smaller valid window. |
| 99 | + * If the character at `start` is in `t`, increment its frequency in the map and increment `needcnt` if the frequency becomes positive. |
| 100 | + * Update the `res` tuple if a smaller valid window is found. |
| 101 | +4. Return Result: |
| 102 | +* Return the minimum window substring using the indices stored in res. |
| 103 | + |
| 104 | +#### Implementation |
| 105 | + |
| 106 | +```Python |
| 107 | +def combine(self, n, k): |
| 108 | + sol=[] |
| 109 | + def backtrack(remain,comb,nex): |
| 110 | + # solution found |
| 111 | + if remain==0: |
| 112 | + sol.append(comb.copy()) |
| 113 | + else: |
| 114 | + # iterate through all possible candidates |
| 115 | + for i in range(nex,n+1): |
| 116 | + # add candidate |
| 117 | + comb.append(i) |
| 118 | + #backtrack |
| 119 | + backtrack(remain-1,comb,i+1) |
| 120 | + # remove candidate |
| 121 | + comb.pop() |
| 122 | + |
| 123 | + backtrack(k,[],1) |
| 124 | + return sol |
| 125 | +``` |
| 126 | + |
| 127 | +### Complexity Analysis |
| 128 | + |
| 129 | +- **Time complexity**: $O(N)$ |
| 130 | +- **Space complexity**: $O(1)$ |
| 131 | + |
| 132 | +### Conclusion |
| 133 | + |
| 134 | +Both approaches for finding the minimum window substring are efficient, operating with a time complexity of O(n) and a space complexity of O(1). The Java and Python implementations utilize similar sliding window techniques, adjusting the window size dynamically to find the smallest substring containing all characters of t. The primary difference lies in language-specific syntax and data structures, but the underlying algorithm remains consistent. |
0 commit comments