-
-
Notifications
You must be signed in to change notification settings - Fork 156
Create 0077-combinations.md #2079
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ajay-dhangar
merged 4 commits into
codeharborhub:main
from
PradnyaGaitonde:PradnyaGaitonde-patch-30
Jun 27, 2024
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e55f103
Create 0077-combinations.md
PradnyaGaitonde 4c1ba36
Merge branch 'main' into PradnyaGaitonde-patch-30
ajay-dhangar 1b7678b
Update 0077-combinations.md
PradnyaGaitonde 5578fbc
Merge branch 'CodeHarborHub:main' into PradnyaGaitonde-patch-30
PradnyaGaitonde File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
144 changes: 144 additions & 0 deletions
144
dsa-solutions/lc-solutions/0000-0099/0077-combinations.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
--- | ||
id: combinations | ||
title: Combinations(LeetCode) | ||
sidebar_label: 0077-Combinations | ||
tags: | ||
- Backtracking | ||
description: Given two integers n and k, return all possible combinations of k numbers chosen from the range [1, n]. | ||
--- | ||
|
||
## 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:** | ||
|
||
```plaintext | ||
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:** | ||
|
||
```plaintext | ||
Input: n = 1, k = 1 | ||
Output: [[1]] | ||
Explanation: There is 1 choose 1 = 1 total combination. | ||
``` | ||
|
||
### Constraints | ||
|
||
- `1 <= n <= 20` | ||
`1 <= k <= n` | ||
|
||
## Solution | ||
|
||
### Approach 1: Minimum Window Substring (Java Implementation) | ||
|
||
#### Algorithm | ||
|
||
1. Initialization: | ||
* Create a frequency map for characters in string `t`. | ||
* 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). | ||
2. Expand the Window: | ||
* Traverse through string `s` with the `end` pointer. | ||
* If the current character is needed (frequency in `map` is greater than 0), decrement `counter`. | ||
* Decrease the frequency of the current character in the map. | ||
3. Contract the Window: | ||
* When `counter` is 0 (all characters from `t` are found in the window): | ||
* Check if the current window is smaller than the previously found minimum window (`d`). | ||
* Move the `begin` pointer to find a smaller valid window. | ||
* If the character at `begin` is in `t`, increment its frequency in the map and increment `counter` if the frequency becomes positive. | ||
4. Return Result: | ||
* Return the minimum window substring. | ||
|
||
#### Implementation | ||
|
||
```Java | ||
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**: $O(N)$ | ||
- **Space complexity**: $O(1)$ | ||
|
||
### Approach 2: Python Implementation | ||
|
||
#### Algorithm | ||
|
||
1. Initialization: | ||
* Create a frequency map for characters in string `t`. | ||
* 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). | ||
2. Expand the Window: | ||
* Traverse through string `s` with the `end` pointer. | ||
* If the current character is needed (frequency in `needstr` is greater than 0), decrement `needcnt`. | ||
* Decrease the frequency of the current character in the map. | ||
3. Contract the Window: | ||
* When `needcnt` is 0 (all characters from `t` are found in the window): | ||
* Move the `start` pointer to find a smaller valid window. | ||
* If the character at `start` is in `t`, increment its frequency in the map and increment `needcnt` if the frequency becomes positive. | ||
* Update the `res` tuple if a smaller valid window is found. | ||
4. Return Result: | ||
* Return the minimum window substring using the indices stored in res. | ||
|
||
#### Implementation | ||
|
||
```Python | ||
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**: $O(N)$ | ||
- **Space complexity**: $O(1)$ | ||
|
||
### Conclusion | ||
|
||
Both approaches for finding the minimum window substring are efficient, operating with a time complexity of | ||
𝑂 | ||
( | ||
𝑛 | ||
) | ||
O(n) and a space complexity of | ||
𝑂 | ||
( | ||
1 | ||
) | ||
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. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.