From d3779acebdfd7d238df662e490ad0de06edce5ae Mon Sep 17 00:00:00 2001 From: --aman singh Date: Mon, 19 Aug 2024 21:04:49 +0530 Subject: [PATCH 1/7] add backtracking --- ..." => "\342\234\205 Pattern 09: Two Heaps.md" | 0 "\342\234\205 Pattern 17: BackTracking.md" | 16 ++++++++++++++++ 2 files changed, 16 insertions(+) rename "\342\234\205 \360\237\231\203 Pattern 09: Two Heaps.md" => "\342\234\205 Pattern 09: Two Heaps.md" (100%) create mode 100644 "\342\234\205 Pattern 17: BackTracking.md" diff --git "a/\342\234\205 \360\237\231\203 Pattern 09: Two Heaps.md" "b/\342\234\205 Pattern 09: Two Heaps.md" similarity index 100% rename from "\342\234\205 \360\237\231\203 Pattern 09: Two Heaps.md" rename to "\342\234\205 Pattern 09: Two Heaps.md" diff --git "a/\342\234\205 Pattern 17: BackTracking.md" "b/\342\234\205 Pattern 17: BackTracking.md" new file mode 100644 index 0000000..eade82c --- /dev/null +++ "b/\342\234\205 Pattern 17: BackTracking.md" @@ -0,0 +1,16 @@ + +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); // Explore candidate. + backtrack(arguments); + current.pop_back(); // Abandon candidate. + } +} +``` \ No newline at end of file From 385d9528e1b7fe5f81f18b82e6ea9ac9a4cded95 Mon Sep 17 00:00:00 2001 From: Aman <53884276+aman246149@users.noreply.github.com> Date: Mon, 19 Aug 2024 21:11:45 +0530 Subject: [PATCH 2/7] =?UTF-8?q?Update=20=E2=9C=85=20Pattern=2017:=20BackTr?= =?UTF-8?q?acking.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "\342\234\205 Pattern 17: BackTracking.md" | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git "a/\342\234\205 Pattern 17: BackTracking.md" "b/\342\234\205 Pattern 17: BackTracking.md" index eade82c..545ff2f 100644 --- "a/\342\234\205 Pattern 17: BackTracking.md" +++ "b/\342\234\205 Pattern 17: BackTracking.md" @@ -13,4 +13,12 @@ void backtrack(arguments) { current.pop_back(); // Abandon candidate. } } -``` \ No newline at end of file +``` +**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/) From 01030a7c76d9208ddf9a255bd781d4ec84a1ab75 Mon Sep 17 00:00:00 2001 From: Aman <53884276+aman246149@users.noreply.github.com> Date: Mon, 19 Aug 2024 21:23:39 +0530 Subject: [PATCH 3/7] =?UTF-8?q?Update=20=E2=9C=85=20Pattern=2017:=20BackTr?= =?UTF-8?q?acking.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "\342\234\205 Pattern 17: BackTracking.md" | 73 +++++++++++++++++++++- 1 file changed, 70 insertions(+), 3 deletions(-) diff --git "a/\342\234\205 Pattern 17: BackTracking.md" "b/\342\234\205 Pattern 17: BackTracking.md" index 545ff2f..8ee2290 100644 --- "a/\342\234\205 Pattern 17: BackTracking.md" +++ "b/\342\234\205 Pattern 17: BackTracking.md" @@ -8,9 +8,9 @@ void backtrack(arguments) { return; } for (int i = num; i <= last; i++) { - current.push_back(i); // Explore candidate. - backtrack(arguments); - current.pop_back(); // Abandon candidate. + current.push_back(i); // Choose candidate. + backtrack(arguments); // Explore more + current.pop_back(); // Remove candidate. } } ``` @@ -22,3 +22,70 @@ void backtrack(arguments) { 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 +![image](https://github.com/user-attachments/assets/f00d62aa-1a01-4080-9dfb-c872e5e6b663) + + +**SUBSET** +**Example Input** + +A = [1, 2, 3] + + + +**Example Output** + +[ + [], + [1], + [1, 2], + [1, 2, 3], + [1, 3], + [2], + [2, 3], + [3], +] + +![image](https://github.com/user-attachments/assets/96978d2d-5be0-4b1b-b243-507484208465) + +**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] ] + +![image](https://github.com/user-attachments/assets/8d5d7fdd-b505-4cde-aa50-12084f495ae8) + From 18dccc9dcfbe25fa61aa209644d7cf81f898cc19 Mon Sep 17 00:00:00 2001 From: Aman <53884276+aman246149@users.noreply.github.com> Date: Mon, 19 Aug 2024 21:39:35 +0530 Subject: [PATCH 4/7] =?UTF-8?q?Update=20=E2=9C=85=20Pattern=2017:=20BackTr?= =?UTF-8?q?acking.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "\342\234\205 Pattern 17: BackTracking.md" | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git "a/\342\234\205 Pattern 17: BackTracking.md" "b/\342\234\205 Pattern 17: BackTracking.md" index 8ee2290..5cc2fe8 100644 --- "a/\342\234\205 Pattern 17: BackTracking.md" +++ "b/\342\234\205 Pattern 17: BackTracking.md" @@ -1,4 +1,5 @@ + Backtracking used for to get all possible solutions. ``` @@ -85,7 +86,24 @@ Output 1: Output 2: -[ [2, 2, 3] , [7] ] +[ [2, 2, 3], [7] ] ![image](https://github.com/user-attachments/assets/8d5d7fdd-b505-4cde-aa50-12084f495ae8) +**Combination Sum2** +Only diff in combination sum and this problem is duplicate not allowed so we move to next index asap + +Input and output +![image](https://github.com/user-attachments/assets/ca4e43aa-ee52-4837-b970-4bea3e0718cf) + +![image](https://github.com/user-attachments/assets/efe1c936-ba27-4522-925b-eec376c6fd59) + +**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 +![image](https://github.com/user-attachments/assets/6eb69ee9-7807-44e7-a5f9-a6f5e82ca8db) + +![image](https://github.com/user-attachments/assets/1c500ee1-1200-4d9a-9d44-2aca157c7701) + + + From a2b5039d443c1fa421d266e58a59c56b27d03e1e Mon Sep 17 00:00:00 2001 From: Aman <53884276+aman246149@users.noreply.github.com> Date: Mon, 19 Aug 2024 21:50:26 +0530 Subject: [PATCH 5/7] =?UTF-8?q?Update=20=E2=9C=85=20Pattern=2017:=20FIxed?= =?UTF-8?q?=20Spacing=20Issue=20and=20Add=20more=20Questions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "\342\234\205 Pattern 17: BackTracking.md" | 40 ++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git "a/\342\234\205 Pattern 17: BackTracking.md" "b/\342\234\205 Pattern 17: BackTracking.md" index 5cc2fe8..5ad83c3 100644 --- "a/\342\234\205 Pattern 17: BackTracking.md" +++ "b/\342\234\205 Pattern 17: BackTracking.md" @@ -91,19 +91,59 @@ Output 2: ![image](https://github.com/user-attachments/assets/8d5d7fdd-b505-4cde-aa50-12084f495ae8) **Combination Sum2** + Only diff in combination sum and this problem is duplicate not allowed so we move to next index asap Input and output + ![image](https://github.com/user-attachments/assets/ca4e43aa-ee52-4837-b970-4bea3e0718cf) ![image](https://github.com/user-attachments/assets/efe1c936-ba27-4522-925b-eec376c6fd59) **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 ![image](https://github.com/user-attachments/assets/6eb69ee9-7807-44e7-a5f9-a6f5e82ca8db) ![image](https://github.com/user-attachments/assets/1c500ee1-1200-4d9a-9d44-2aca157c7701) +**Letter Phone** + +Input + +![image](https://github.com/user-attachments/assets/8568e54f-6068-4f5f-823d-c1e8a30b3678) + +Output + +Used All Possible Sum Approach + +![image](https://github.com/user-attachments/assets/e2ee6556-1c43-4797-8f61-dad21457162b) + +**Permutation** + +Input + +![image](https://github.com/user-attachments/assets/44adba86-10e0-4adc-9af9-9c1220b3ab85) + +Output + +![image](https://github.com/user-attachments/assets/2a8d4705-41ad-40c5-8904-991549d2e877) + +**N QUEEN** + +![image](https://github.com/user-attachments/assets/0fc7b275-9e0b-4a57-84c1-63ae5a1efb62) + +Output + +![image](https://github.com/user-attachments/assets/d408d73c-520f-40ad-a029-ffbafed4364a) + + + + + + + From 5aebac692399e700acbb5a871e62255f18f69aa0 Mon Sep 17 00:00:00 2001 From: Aman <53884276+aman246149@users.noreply.github.com> Date: Tue, 20 Aug 2024 08:44:14 +0530 Subject: [PATCH 6/7] =?UTF-8?q?Create=20=E2=9C=85=20Pattern=2018:Graph.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "\342\234\205 Pattern 18:Graph.md" | 1 + 1 file changed, 1 insertion(+) create mode 100644 "\342\234\205 Pattern 18:Graph.md" diff --git "a/\342\234\205 Pattern 18:Graph.md" "b/\342\234\205 Pattern 18:Graph.md" new file mode 100644 index 0000000..6a7d911 --- /dev/null +++ "b/\342\234\205 Pattern 18:Graph.md" @@ -0,0 +1 @@ +https://leetcode.com/discuss/study-guide/3903992/graph-its-implementation-and-some-popular-bfsdfs-problems From daa6c7bd3a927b03410c5ad667d03c3ffa2f005b Mon Sep 17 00:00:00 2001 From: Aman <53884276+aman246149@users.noreply.github.com> Date: Tue, 20 Aug 2024 08:47:29 +0530 Subject: [PATCH 7/7] =?UTF-8?q?Update=20=E2=9C=85=20Pattern=2018:Graph.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added Bfs and Dfs Code Snippet --- "\342\234\205 Pattern 18:Graph.md" | 65 ++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git "a/\342\234\205 Pattern 18:Graph.md" "b/\342\234\205 Pattern 18:Graph.md" index 6a7d911..bd2fc84 100644 --- "a/\342\234\205 Pattern 18:Graph.md" +++ "b/\342\234\205 Pattern 18:Graph.md" @@ -1 +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 bfsOfGraph(int n, List> 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 q = new LinkedList<>(); + vis[0] = true; + q.add(0); + List 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, vectoradj[], vector&vis, vector& 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 dfsOfGraph(int n, vector adj[]) { + vectorvis(n,0); + int s = 0; + vectorans; + dfs(s, adj, vis, ans); + return ans; + } +}; +```