-
Notifications
You must be signed in to change notification settings - Fork 871
Added Backtracking pattern #17
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
Open
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d3779ac
add backtracking
amancodebix 385d952
Update ✅ Pattern 17: BackTracking.md
aman246149 01030a7
Update ✅ Pattern 17: BackTracking.md
aman246149 18dccc9
Update ✅ Pattern 17: BackTracking.md
aman246149 a2b5039
Update ✅ Pattern 17: FIxed Spacing Issue and Add more Questions
aman246149 5aebac6
Create ✅ Pattern 18:Graph.md
aman246149 daa6c7b
Update ✅ Pattern 18:Graph.md
aman246149 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
File renamed without changes.
149 changes: 149 additions & 0 deletions
✅ Pattern 17: BackTracking.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,149 @@ | ||
|
||
|
||
Backtracking used for to get all possible solutions. | ||
|
||
``` | ||
void backtrack(arguments) { | ||
if (condition == true) { // Condition when we should stop our exploration. | ||
result.push_back(current); | ||
return; | ||
} | ||
for (int i = num; i <= last; i++) { | ||
current.push_back(i); // Choose candidate. | ||
backtrack(arguments); // Explore more | ||
current.pop_back(); // Remove candidate. | ||
} | ||
} | ||
``` | ||
**Popular Questions** | ||
|
||
1. [All Possible Combinations](https://www.interviewbit.com/problems/all-possible-combinations/) | ||
2. [Subset](https://www.interviewbit.com/problems/subset/) | ||
3. [Combination Sum](https://www.interviewbit.com/problems/combination-sum/) | ||
4. [Combination Sum 2](https://www.interviewbit.com/problems/combination-sum-ii/) | ||
5. [Combinations](https://www.interviewbit.com/problems/combinations/) | ||
6. [Subset2](https://www.interviewbit.com/problems/subsets-ii/) | ||
|
||
**All Possible Combinations** | ||
|
||
Input 1: | ||
|
||
``` | ||
A = ['ab', 'cd'] | ||
``` | ||
**Example Output** | ||
|
||
Output 1: | ||
|
||
``` | ||
['ac', 'ad', 'bc', 'bd'] | ||
``` | ||
CODE -- ACCORDING TO OUR PATTERN | ||
 | ||
|
||
|
||
**SUBSET** | ||
**Example Input** | ||
|
||
A = [1, 2, 3] | ||
|
||
|
||
|
||
**Example Output** | ||
|
||
[ | ||
[], | ||
[1], | ||
[1, 2], | ||
[1, 2, 3], | ||
[1, 3], | ||
[2], | ||
[2, 3], | ||
[3], | ||
] | ||
|
||
 | ||
|
||
**Combination Sum** | ||
|
||
**Example Input** | ||
|
||
Input 1: | ||
|
||
A = [2, 3] | ||
B = 2 | ||
|
||
Input 2: | ||
|
||
A = [2, 3, 6, 7] | ||
B = 7 | ||
|
||
**Example Output** | ||
|
||
Output 1: | ||
|
||
[ [2] ] | ||
|
||
Output 2: | ||
|
||
[ [2, 2, 3], [7] ] | ||
|
||
 | ||
|
||
**Combination Sum2** | ||
|
||
Only diff in combination sum and this problem is duplicate not allowed so we move to next index asap | ||
|
||
Input and output | ||
|
||
 | ||
|
||
 | ||
|
||
**SubSet2** | ||
|
||
The only difference between this and the subset problem is, here we store results in set to avoid duplicates while in the set question we used ArrayList to get all subsets including duplicates | ||
|
||
Input | ||
 | ||
|
||
 | ||
|
||
|
||
**Letter Phone** | ||
|
||
Input | ||
|
||
 | ||
|
||
Output | ||
|
||
Used All Possible Sum Approach | ||
|
||
 | ||
|
||
**Permutation** | ||
|
||
Input | ||
|
||
 | ||
|
||
Output | ||
|
||
 | ||
|
||
**N QUEEN** | ||
|
||
 | ||
|
||
Output | ||
|
||
 | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
66 changes: 66 additions & 0 deletions
✅ Pattern 18:Graph.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,66 @@ | ||
https://leetcode.com/discuss/study-guide/3903992/graph-its-implementation-and-some-popular-bfsdfs-problems | ||
|
||
**BFS** | ||
|
||
``` | ||
import java.util.*; | ||
|
||
class Solution { | ||
public List<Integer> bfsOfGraph(int n, List<List<Integer>> adj) { | ||
// Create a visited array to keep track of visited nodes | ||
boolean[] vis = new boolean[n]; | ||
|
||
// 1. Pick a starting node, push it into the queue, and mark it as visited. | ||
Queue<Integer> q = new LinkedList<>(); | ||
vis[0] = true; | ||
q.add(0); | ||
List<Integer> ans = new ArrayList<>(); | ||
|
||
// 4. Repeat steps 2 and 3. | ||
while (!q.isEmpty()) { | ||
|
||
// 2. In every iteration, pop out the 'x' node and put it in the solution list. | ||
int node = q.poll(); | ||
ans.add(node); | ||
|
||
// 3. All the unvisited adjacent nodes from 'x' are pushed into the queue. | ||
for (int a : adj.get(node)) { | ||
if (!vis[a]) { | ||
vis[a] = true; | ||
q.add(a); | ||
} | ||
} | ||
} | ||
return ans; | ||
} | ||
|
||
|
||
``` | ||
|
||
**DFS** | ||
|
||
``` | ||
class Solution { | ||
public: | ||
|
||
void dfs(int node, vector<int>adj[], vector<bool>&vis, vector<int>& ans) { | ||
vis[node] = 1; | ||
ans.push_back(node); | ||
|
||
for(auto a:adj[node]) { | ||
if(!vis[a]) { | ||
dfs(a,adj,vis,ans); | ||
} | ||
} | ||
} | ||
|
||
// Function to return a list containing the DFS traversal of the graph. | ||
vector<int> dfsOfGraph(int n, vector<int> adj[]) { | ||
vector<bool>vis(n,0); | ||
int s = 0; | ||
vector<int>ans; | ||
dfs(s, adj, vis, ans); | ||
return ans; | ||
} | ||
}; | ||
``` |
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.