🚀 Day 89/100 – Permutations II 🧠 Problem: Given an array that may contain duplicates, return all unique permutations ✔️ No duplicate permutations ✔️ Order doesn’t matter 💡 Core Idea 🔥 Classic Backtracking + Duplicate Handling 👉 Sort the array first 👉 Use a used[] array 👉 Skip duplicates using condition: if(i > 0 && nums[i] == nums[i-1] && !used[i-1]) continue; ⚡ This ensures only unique permutations are generated 📚 Key Learnings Backtracking with pruning Handling duplicates efficiently Importance of sorting before recursion ⏱️ Complexity Time Complexity: O(n! ) Space Complexity: O(n) #100DaysOfCode #Day89 #LeetCode #DSA #Backtracking #Algorithms #ProblemSolving #CodingJourney #Consistency
Unique Permutations with Duplicates Handling
More Relevant Posts
-
🚀 Day 72/100 – Combinations 🧠 Problem: Given two integers n and k, return all possible combinations of k numbers from 1 to n. 💡 Core Idea Classic Backtracking / Recursion problem 🔥 1️⃣ Start from number 1 2️⃣ Choose current element → move forward 3️⃣ Reduce k (remaining elements to pick) 4️⃣ Backtrack (remove element & explore next options) 👉 Build combinations step-by-step 📚 Key Learnings 1. Backtracking fundamentals 2. Decision tree exploration 3. Include / Exclude pattern 3. Generating all possible combinations ⏱️ Complexity Time Complexity: O(C(n, k)) Space Complexity: O(k) (recursion stack) #100DaysOfCode #Day73 #LeetCode #DSA #Backtracking #Recursion #Algorithms #ProblemSolving #CodingJourney #Consistency
To view or add a comment, sign in
-
-
🚀 Day 83/100 – Subsets II 🧠 Problem: Given an integer array that may contain duplicates, return all possible subsets (power set). ⚠️ The solution set must not contain duplicate subsets 💡 Core Idea This is Backtracking + Duplicate Handling 🔥 1️⃣ First sort the array 2️⃣ Use Pick / Not Pick approach 3️⃣ While skipping elements → avoid duplicates: 👉 Skip all same elements using a loop 4️⃣ Build subsets recursively 👉 Key trick: Skip duplicates carefully 📚 Key Learnings Handling duplicates in recursion Importance of sorting Avoiding redundant subsets ⏱️ Complexity Time Complexity: O(2ⁿ) Space Complexity: O(n) #100DaysOfCode #Day83 #LeetCode #DSA #Backtracking #Recursion #Subsets #Algorithms #ProblemSolving #CodingJourney #Consistency
To view or add a comment, sign in
-
-
LeetCode #40 — Combination Sum II | Backtracking with Precision Just tackled Combination Sum II — where backtracking meets careful duplicate handling Problem Insight: Find all unique combinations that sum to a target, but: Each number can be used only once The input may contain duplicates Output must NOT have duplicate combinations Approach: Sort + Backtracking + Smart Pruning Sort the array to handle duplicates easily Skip duplicates using: if i > start and candidates[i] == candidates[i-1]: continue Move forward with i + 1 (since reuse is NOT allowed) Stop early if candidates[i] > target → pruning What I Learned: Difference between Combination Sum I vs II lies in index movement & reuse Sorting simplifies duplicate handling significantly Pruning unnecessary paths boosts performance Complexity: Time: O(2^n) (with pruning helping in practice) Space: O(n) recursion stack #LeetCode #Backtracking #Algorithms #CodingJourney #DSA #ProblemSolving #TechLearning
To view or add a comment, sign in
-
-
Day 109 Solved: 1855. Maximum Distance Between a Pair of Values (Medium) Today’s problem was a solid exercise in two-pointer technique on non-increasing arrays. 🔍 Key Idea: Since both arrays are non-increasing, we can avoid brute force and use a greedy two-pointer approach: Start with i = 0, j = 0 If nums1[i] <= nums2[j], update max distance and move j forward Otherwise, move i forward ⚡ Why it works: We leverage the sorted nature (non-increasing) to ensure we never revisit unnecessary pairs → O(n + m) time complexity. 💡 Learning: Understanding array properties (like sorted order) can completely change your approach—from brute force to optimal. 📈 Progress: Day 109 of consistency. Still learning, still improving. #LeetCode #DataStructures #Algorithms #CodingJourney #100DaysOfCode #TwoPointers
To view or add a comment, sign in
-
-
Word Search Using Backtracking on Grid 🧩 Day 12 👈 The challenge is to check if a word exists in a grid by moving in 4 directions (up, down, left, right) without revisiting the same cell. 💡 Key learnings: 👉 This is a DFS + Backtracking problem 👉 Start from every cell matching the first character 👉 Explore all 4 directions recursively 👉 Mark visited cells temporarily to avoid reuse Approach: 👉 Traverse the grid 👉 When a match is found, start DFS 👉 Move in all possible directions 👉 Backtrack by restoring the cell value #DSA #Backtracking #DFS #Matrix #Algorithms #CodingJourney #LeetCode #ProblemSolving #InterviewPreparation #SoftwareEngineer
To view or add a comment, sign in
-
-
🚀 Day 117 of My DSA Problem Solving Journey - The Grind Continues! 🎉 After conquering Monotonic Stacks, today I tackled an interesting array and Hash Map problem on LeetCode: "Minimum Distance Between Three Equal Elements I" in C++. The Problem: Given an array, we need to find three identical elements at distinct indices (i, j, k) such that their "distance" abs(i - j) + abs(j - k) + abs(k - i) is minimized. If no such tuple exists, return -1. My Approach: Instead of a brute-force O(N^3) loop to find triplets, I optimized the solution using a Hash Map (unordered_map)! 🧠 The Logic: Math Simplification: The distance formula abs(i - j) + abs(j - k) + abs(k - i) actually simplifies to 2 \times (k - i) assuming i < j < k. This was the "Aha!" moment! I didn't even need the middle index j for the calculation, just the first and third indices of the triplet. Tracking Indices: I used a hash map where the key is the array element and the value is a vector storing all the indices where this element appears. On-the-Fly Calculation: As I iterate through the array, the moment a number appears for the third time (or more), I grab its current index (i3) and the index from two occurrences ago (i1). I calculate the distance as 2 times (i3 - i1) and update my minimum answer. Sliding Window Feel: By always checking the latest index (n-1) and the index two steps back (n-3), I ensure I'm always calculating the tightest possible distance for any three consecutive occurrences of a number. Takeaway: This problem is a great reminder of how mathematical simplification combined with Hash Maps can turn an intimidating problem into an elegant O(N) Time and Space complexity solution. Keep an eye out for those hidden formulas! ⚡ Keep pushing! 💻🔥 #Day117 #CPP #HashMap #Arrays #LeetCode #DataStructures #Algorithms #DSA #ProblemSolving #CodingJourney #ContinuousLearning #REGexSoftwareServices
To view or add a comment, sign in
-
-
-- Solved: Sum of Distances (LeetCode 2615) -- Today I worked on a problem that looks simple at first… but quickly punishes brute force thinking. -- Problem Insight For each index, calculate the sum of distances to all other indices with the same value. -- Mistake I Made - I initially tried comparing every element with every other element having the same value. - It worked for small cases but completely breaks for large inputs. -- Key Optimization The breakthrough came when I realized: - Instead of comparing every pair, group indices by value - Use prefix sums to compute distances efficiently - This reduces the complexity to O(n) Always question: “Can I reuse previous computations?” - Every problem like this improves how I think about scaling solutions. - Less brute force, more structure. #DSA #LeetCode #ProblemSolving #CodingJourney #SoftwareEngineering #Optimization #algorithm
To view or add a comment, sign in
-
-
LeetCode #46 — Permutations | Classic Backtracking Back to fundamentals with Permutations — one of the best problems to truly understand recursion Problem Insight:Given distinct integers, generate all possible permutations (order matters). Approach: Backtracking (build → explore → undo) Try each number in every position Skip elements already used in the current path Once path length == n → valid permutation Core Idea: At each step, you have a choice among remaining elements → this forms a decision tree Key Line: if num in path: continue Ensures we don’t reuse elements What I Learned: Backtracking is about exploring all possibilities systematically “Choose → Explore → Unchoose” is the golden pattern #LeetCode #Backtracking #Recursion #Algorithms #CodingJourney #DSA #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 9 — Check if the Array is Sorted Continuing my journey of improving problem-solving skills with consistency and discipline — one problem every day. Today’s problem focused on verifying order — a simple concept, but very important in many algorithms. 🧩 Problem Solved: • Check if the Array is Sorted 📚 Topic: Arrays (Traversal) 💡 Key Insight: To check if an array is sorted, we only need to ensure that every element is less than or equal to the next one. ⚡ Approach: • Traverse the array from start to end • Compare each element with the next • If any element is greater than the next → return False • If no violations found → return True 🎯 Takeaway: Simple checks like this are often building blocks for more complex algorithms like binary search and sorting techniques. ⏱ Complexity: • Time → O(n) • Space → O(1) 💻 Example: Input → [1, 2, 3, 4, 5] → Output → True Input → [1, 2, 1, 4, 5] → Output → False Strong fundamentals make advanced problems easier 🚀 #ProblemSolving #Arrays #CodingJourney #100DaysOfCode #Consistency #LearningInPublic
To view or add a comment, sign in
-
-
Day 90 of #1000DaysOfCoding 🚀 Today’s focus: mastering array manipulation with an optimized approach. Solved the classic “Rotate Array” problem using an in-place reversal technique. Instead of extra space, I used a three-step reverse strategy to achieve O(n) time and O(1) space complexity. Key takeaway: Sometimes the most efficient solution isn’t the most obvious one. Breaking a problem into smaller transformations can lead to clean and optimal code. Progress update: ✔️ 40/40 test cases passed ✔️ 0 ms runtime ✔️ Improved problem-solving intuition Nearing the 100-day milestone. Staying consistent, staying curious. #100DaysOfCode #CodingJourney #LeetCode #DataStructures #Algorithms #CPP #ProblemSolving #SoftwareEngineering #DeveloperLife #CodingChallenge
To view or add a comment, sign in
-
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