Combinations(LeetCode)
Problem Statement​
Given two integers n and k, return all possible combinations of k numbers chosen from the range [1, n].
You may return the answer in any order.
Examples​
Example 1:
Input: n = 4, k = 2
Output: [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]
Explanation: There are 4 choose 2 = 6 total combinations.
Note that combinations are unordered, i.e., [1,2] and [2,1] are considered to be the same combination.
Example 2:
Input: n = 1, k = 1
Output: [[1]]
Explanation: There is 1 choose 1 = 1 total combination.
Constraints​
1 <= n <= 201 <= k <= n
Solution​
Approach 1: Minimum Window Substring (Java Implementation)​
Algorithm​
- Initialization:
- Create a frequency map for characters in string
t. - Initialize counters and pointers:
counter(number of characters still needed fromt),begin(start pointer),end(end pointer),d(length of the minimum window), andhead(starting index of the minimum window).
- Expand the Window:
- Traverse through string
swith theendpointer. - If the current character is needed (frequency in
mapis greater than 0), decrementcounter. - Decrease the frequency of the current character in the map.
- Contract the Window:
- When
counteris 0 (all characters fromtare found in the window):- Check if the current window is smaller than the previously found minimum window (
d). - Move the
beginpointer to find a smaller valid window. - If the character at
beginis int, increment its frequency in the map and incrementcounterif the frequency becomes positive.
- Check if the current window is smaller than the previously found minimum window (
- Return Result:
- Return the minimum window substring.
Implementation​
def backtrack(candidate):
if find_solution(candidate):
output(candidate)
return
# iterate all possible candidates
for next_candidate in list_of_candidates:
if is_valid(next_candidate):
# try this partial candidate solution
place(next_candidate)
# explore further with the given candidate
backtrack(next_candidate)
# backtrack
remove(next_candidate)
Complexity Analysis​
- Time complexity:
- Space complexity:
Approach 2: Python Implementation​
Algorithm​
- Initialization:
- Create a frequency map for characters in string
t. - Initialize counters and pointers:
needcnt(number of characters still needed fromt),res(tuple to store the start and end indices of the minimum window), andstart(start pointer).
- Expand the Window:
- Traverse through string
swith theendpointer. - If the current character is needed (frequency in
needstris greater than 0), decrementneedcnt. - Decrease the frequency of the current character in the map.
- Contract the Window:
- When
needcntis 0 (all characters fromtare found in the window):- Move the
startpointer to find a smaller valid window. - If the character at
startis int, increment its frequency in the map and incrementneedcntif the frequency becomes positive. - Update the
restuple if a smaller valid window is found.
- Move the
- Return Result:
- Return the minimum window substring using the indices stored in res.
Implementation​
def combine(self, n, k):
sol=[]
def backtrack(remain,comb,nex):
# solution found
if remain==0:
sol.append(comb.copy())
else:
# iterate through all possible candidates
for i in range(nex,n+1):
# add candidate
comb.append(i)
#backtrack
backtrack(remain-1,comb,i+1)
# remove candidate
comb.pop()
backtrack(k,[],1)
return sol
Complexity Analysis​
- Time complexity:
- Space complexity:
Conclusion​
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.