🚀 #100DaysOfCode Day 77/100 – Permutations 🧠 Problem: Given an array of distinct integers, return all possible permutations. 👉 Order matters here → [1,2,3] ≠ [3,2,1] 💡 Core Idea This is a classic Backtracking + Swapping problem 🔥 1️⃣ Fix one element at current index 2️⃣ Swap it with every possible element 3️⃣ Recursively generate permutations for remaining 4️⃣ Backtrack (swap back) 👉 Swap → Recurse → Undo 📚 Key Learnings 1. Backtracking with swapping technique 2. Generating all permutations efficiently 3. Understanding recursion tree deeply ⏱️ Complexity Time Complexity: O(n!) Space Complexity: O(n) (recursion stack) #100DaysOfCode #Day77 #LeetCode #DSA #Backtracking #Recursion #Algorithms #CodingJourney #ProblemSolving #Consistency
Backtracking Permutations Algorithm
More Relevant Posts
-
Day 84/150 🚀 LeetCode 124: Binary Tree Maximum Path Sum 🧠 Problem Find the maximum path sum in a binary tree. A path can start and end at any node. 💡 Approach • Use DFS (post-order traversal) • Ignore negative paths → max(0, left/right) • Update global max using left + right + root • Return max single path for recursion ⏱ Time Complexity O(n) 📦 Space Complexity O(h) (recursion stack) ✅ Result: Accepted ⚡ Runtime: 0 ms (Beats 100%) Hard problems getting comfortable 🌳 #Day84 #LeetCode #BinaryTree #DFS #Recursion #DSA #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
Day 75/150 🚀 LeetCode 101: Symmetric Tree 🧠 Problem Given the root of a binary tree, check whether it is a mirror of itself. 📌 Example Input → root = [1,2,2,3,4,4,3] Output → true 💡 Approach • Use recursion • Compare left subtree with right subtree • Check mirror structure • Continue recursively ⏱ Time Complexity O(n) 📦 Space Complexity O(h) ✅ Result: Accepted ⚡ Runtime: 0 ms (Beats 100%) #Day75 #LeetCode #DSA #BinaryTree #Recursion #CodingJourney #ProblemSolving #SoftwareEngineering
To view or add a comment, sign in
-
-
Day 91 of #100DaysOfCode Today I solved "Insert into a Binary Search Tree" on LeetCode using a Recursive approach. Key Idea: In a Binary Search Tree (BST): • Left subtree → values less than root • Right subtree → values greater than root So we just follow the correct path until we find the right position to insert the new value. Approach: • If root is null → create and return new node • If value > root → go to right subtree • Else → go to left subtree • Recursively insert until correct position is found Concepts Used: • Binary Search Tree (BST) • Recursion • Tree traversal Time Complexity: O(h) Space Complexity: O(h) This problem reinforces the importance of BST properties for efficient insertion Simple logic, powerful structure #Day91 #100DaysOfCode #LeetCode #BST #Recursion #Cpp #CodingJourney
To view or add a comment, sign in
-
-
Day 74/150 🚀 LeetCode 226: Invert Binary Tree 🧠 Problem Given the root of a binary tree, invert the tree and return its root. 📌 Example Input → root = [4,2,7,1,3,6,9] Output → [4,7,2,9,6,3,1] 💡 Approach • Use recursion • Swap left and right child • Recursively invert left subtree • Recursively invert right subtree ⏱ Time Complexity O(n) 📦 Space Complexity O(h) ✅ Result: Accepted ⚡ Runtime: 0 ms (Beats 100%) #Day74 #LeetCode #DSA #BinaryTree #Recursion #CodingJourney #ProblemSolving #SoftwareEngineering
To view or add a comment, sign in
-
-
🚀 Day 65/100 – LeetCode Challenge. 🔍 Problem: Remove All Adjacent Duplicates in String. At first glance, this problem looks like a simple string manipulation task… but the real magic lies in using the right approach! 💡 Approach Used: Stack (via string) 👉 Idea: Traverse the string If current character = last character of result → remove it Else → add it 🧠 Key Insight: We simulate a stack using a string. Last added character = top of stack → result.back() ⚡ Time Complexity: O(n) ⚡ Space Complexity: O(n) 📌 Takeaway: Whenever you see adjacent duplicate removal / pair cancellation, think of stack pattern instantly! 💬 Example: Input: "abbaca" Output: "ca" #Day65 #100DaysOfCode #DSA #LeetCode #CodingJourney
To view or add a comment, sign in
-
-
Day 90 of #100DaysOfCode Today I solved "Path Sum III" on LeetCode using a DFS + Recursion approach. Key Idea: We need to count all paths where the sum of node values equals the target. The path doesn’t have to start from the root — it can start from any node! Approach: • For every node, treat it as a starting point • Use DFS to explore all downward paths • Reduce the target at each step (target - node->val) • If a node matches the remaining target → count it • Repeat the process for left and right subtrees Why this works: Every node gets a chance to act as the starting point, and DFS ensures we explore all possible paths efficiently. Concepts Used: • Binary Trees • Depth First Search (DFS) • Recursion • Backtracking (implicit) Time Complexity: O(n²) in worst case Space Complexity: O(h) This problem helped me understand how to explore all possible paths in a tree, not just root-based ones — a big step forward in mastering tree problems From single path problems → to handling multiple dynamic paths… growing every day #Day90 #100DaysOfCode #LeetCode #BinaryTree #DFS #Cpp #CodingJourney
To view or add a comment, sign in
-
-
Day 46 Today I solved: Binary Search (LeetCode 704) 💡 Problem: Given a sorted array, find the index of a target element. If it doesn’t exist, return -1. 💡 My Approach: I used the classic Binary Search technique: 1️⃣ Initialize two pointers: start and end 2️⃣ Find middle index mid 3️⃣ Compare nums[mid] with target 👉 If equal → return mid 👉 If target is greater → search right half 👉 If target is smaller → search left half 4️⃣ Repeat until found or search space is exhausted 💡 Key Insight: Instead of checking every element ❌ Divide the search space in half each time ✅ ⚡ Complexity: Time: O(log n) Space: O(1) #LeetCode #DSA #CodingJourney #LearnInPublic
To view or add a comment, sign in
-
-
Day 35/75 🚀 Solved Count Good Nodes in Binary Tree (LeetCode 1448) today! ✅ All 63/63 test cases passed ⚡ Runtime: 91 ms (Beats ~82%) 💾 Memory: 88.34 MB (Beats ~64%) 🔍 Approach: Used DFS (recursion) while tracking the maximum value seen so far on the path. ✔️ Start from root with initial max = root value ✔️ If current node value ≥ max so far → it's a “good node” ✔️ Update max and continue traversal ✔️ Recursively check left and right subtree 💡 Key Learning: Passing state (like max value) in recursion helps solve path-based problems efficiently. No need to store full paths—just keep track of what's important. Consistency + depth thinking = stronger tree skills 🌳🔥 #LeetCode #CPP #DSA #ProblemSolving #CodingJourney #75DaysOfCode #Focused
To view or add a comment, sign in
-
-
🚀 Day 72 of #100DaysOfDSA Solved a classic Backtracking problem 🔥 🧩 Problem Solved: LeetCode 77 – Combinations 💡 Key Idea: Generate all possible ways to choose k numbers from 1 to n using Backtracking. 🔍 Approach: Start from a number and pick it Recursively choose next numbers in increasing order Once size becomes k, store the combination Backtrack and try other possibilities ⚡ What I Learned: How recursion builds combinations efficiently Importance of start index to avoid duplicates Backtracking = Choose → Explore → Undo 💻 Language Used: C++ #Day72 #100DaysOfDSA #LeetCode #Cpp #Backtracking #Recursion #CodingJourney
To view or add a comment, sign in
-
-
Day 32/75 🚀 Solved Maximum Depth of Binary Tree (LeetCode 104) today! ✅ All 39/39 test cases passed ⚡ Runtime: 0 ms (Beats 100%) 💾 Memory: 18.94 MB (Beats ~77%) 🔍 Approach: Used recursion (DFS) to calculate depth of the tree. ✔️ If node is NULL → return 0 ✔️ Recursively find depth of left subtree ✔️ Recursively find depth of right subtree ✔️ Return 1 + max(leftDepth, rightDepth) This naturally explores the tree depth-first. 💡 Key Learning: Tree problems often become intuitive with recursion. Think in terms of subproblems (left + right) and combine results. Consistency + recursion thinking = strong tree concepts 🌳 #LeetCode #CPP #DSA #ProblemSolving #CodingJourney #75DaysOfCode #Focused
To view or add a comment, sign in
Explore related topics
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development