🧩 Day 36 — Combination Sum (LeetCode 39) 📝 Problem -Given an array of distinct integers candidates and a target integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target. -The same number may be chosen from candidates an unlimited number of times. -Two combinations are unique if the frequency of at least one number is different. 🔁 Approach -Use backtracking (DFS) to explore all possible combinations. -At each step, choose a number and subtract it from the remaining target. -If the remaining target becomes 0, store the current combination. -If it becomes negative, backtrack (stop exploring that path). -To avoid duplicates, always start the next recursive call from the current index or later. -This ensures combinations like [2,3] and [3,2] are treated as the same. 📊 Complexity -Time Complexity: O(2ⁿ) -Space Complexity: O(n) 🔑 Concepts Practiced -Backtracking and recursion -Depth-first search (DFS) -Combination generation with repetition allowed -Control of duplicate combinations using start index #Leetcode #DSA #Python #DFS #Search
How to solve Combination Sum problem with DFS on LeetCode
More Relevant Posts
-
💯 Day 48 / 100 – 100 Days of #LeetCode Challenge 🔁 Problem: 290. Word Pattern Goal: Given a pattern and a string s, determine if s follows the same pattern. Here, follow means a bijection between a letter in pattern and a unique word in s: Each letter in pattern maps to exactly one unique word in s. Each word in s maps to exactly one unique letter in pattern. No two letters map to the same word and vice versa. Approach: ✅ Split the input string s into individual words. ✅ If the number of pattern characters doesn’t match the number of words → return False. ✅ Use two dictionaries to ensure one-to-one mapping: char_to_word: maps each pattern character to a word. word_to_char: maps each word back to a pattern character. ✅ Verify mappings remain consistent through iteration. Complexity: ⏱ Time: O(n) – single pass through pattern and words. 🗂 Space: O(n) – for two hash maps. 💡 Key Takeaway: This problem emphasizes understanding of bijection (one-to-one mapping). Using two dictionaries ensures the relationship is perfectly mutual — a great lesson in mapping logic and validation. #100DaysOfLeetCode #Day48 #Python #DSA #HashMap #ProblemSolving #CodingChallenge #LeetCode #StringMapping #LogicBuilding
To view or add a comment, sign in
-
-
🚀 𝗗𝗮𝘆 𝟲𝟵 𝗼𝗳 𝟯𝟲𝟱 𝗤𝘂𝗲𝘀𝘁𝗶𝗼𝗻𝘀 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲! Today's leetcode problem: Generate all unique permutations of a list that may contain duplicate values. 𝗣𝗿𝗼𝗯𝗹𝗲𝗺: 47. Permutations II 🔗 𝐋𝐢𝐧𝐤: https://lnkd.in/gKTNuUaR ✔️ 𝗠𝘆 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵: - Used depth-first search (DFS) recursion to build permutations. - After each permutation is formed, store it in a set to ensure uniqueness (since duplicates are possible). - At the end, convert the set of tuples back into a list of lists for the required format. ⏱️ Time Complexity: O(n!) 📦 Space Complexity: O(n!) 📝 Shared handwritten notes & code as well for deeper reference. This approach handles duplicates by leveraging a set — guaranteeing only unique outputs and demonstrating elegant use of recursion and Python data structures! #LeetCode365 #Day69 #PermutationsII #Recursion #Deduplication #Backtracking #DFS #ProblemSolving
To view or add a comment, sign in
-
Handle database upserts in one operation with Delta Lake's MERGE ⚡ In pandas, implementing upserts means running 3 separate operations: filter existing records, update matches, and append new ones. Each step requires a full data scan, increasing both code complexity and execution time. Delta Lake's MERGE replaces this 3-step process with a single transaction that updates existing records and inserts new ones. How it works: • Compares source data with existing table records • Updates matching records with new values • Inserts records that don't exist yet • Executes all changes together with automatic rollback if any step fails Plus, Delta Lake is open source! Install it with "pip install deltalake". See the first comment for the complete guide 📖 #DeltaLake #DataEngineering #Pandas #Python
To view or add a comment, sign in
-
-
🚀 Day 40 / 100 — #100DaysOfLeetCode 💡 (1625) Lexicographically Smallest String After Applying Operations (Medium) This was a really fun graph + BFS problem disguised as a string manipulation challenge. The trick is realizing that applying the two operations (add & rotate) repeatedly explores a finite set of states, forming an implicit graph, where each node is a string transformation. 🧩 Problem Overview You are given: A numeric string s Two integers a and b You can repeatedly: Add a (mod 10) to all digits at odd indices. Rotate the string right by b positions. Find the lexicographically smallest string possible after any number of operations. ⚙️ Approach — BFS on States Used Breadth-First Search (BFS) to explore all reachable strings: Start from the initial string s. Use a set vis to track visited states. For each string, generate: One by performing addition on odd indices. One by rotating by b. Continue until all unique transformations are explored. Track the smallest lexicographical string seen. 📈 Complexities Time Complexity: O(10 * n) Each state can appear up to 10 times due to modulo operations, and each state has n length. Space Complexity: O(10 * n) For storing visited transformations. ✅ Key Insights The problem is finite because both operations are modular and cyclic. BFS guarantees we explore all possible reachable strings in an optimal manner. Lexicographical comparison after BFS is straightforward — track the smallest string so far. ✨ Takeaway This problem beautifully demonstrates how: “Even string problems can secretly be graph traversal problems.” It’s a neat mix of modular arithmetic, rotation logic, and state-space search. #LeetCode #100DaysOfCode #BFS #GraphTraversal #StringManipulation #ProblemSolving #Python #Algorithms
To view or add a comment, sign in
-
-
🧩 Day 32 — Find the Difference of Two Arrays (LeetCode 2215) 📝 Problem Given two 0-indexed integer arrays nums1 and nums2, return a list answer of size 2 where: - answer[0] contains all distinct integers in nums1 not present in nums2. - answer[1] contains all distinct integers in nums2 not present in nums1. The order of elements in the output lists does not matter. 🔁 Approach - Use sets to eliminate duplicates and enable fast lookups. - Convert both arrays to sets: set1 and set2. - Compute differences using set operations: - set1 - set2 gives elements in nums1 not in nums2. - set2 - set1 gives elements in nums2 not in nums1. - Convert the results back to lists to match the required output format. 📊 Complexity - Time Complexity: O(n + m) - Space Complexity: O(n + m) 🔑 Concepts Practiced - Set operations for fast difference computation - Removing duplicates using set() - Efficient list transformation - Clean and readable Python logic #Python #DSA #Leetcode #ProblemSolving #Arrays
To view or add a comment, sign in
-
-
🔁 Day 45 of #100DaysOfDSA — Reverse Linked List Problem: Reverse Linked List (LeetCode #206) Given the head of a singly linked list, reverse it and return the reversed list. Concepts Used: 🧠 Stack-based approach Traverse the linked list and push each node’s value into a stack. Then reassign values by popping from the stack to reverse the list. 🧩 Learning Reflection: This problem helped me understand how stacks can simplify linked list manipulations. Though not the most space-efficient solution, it reinforces the concept of LIFO (Last In, First Out) operations beautifully. ⏱️ Complexity: Time: O(N) Space: O(N) 💬 Closing Thought: Sometimes, using an extra data structure makes logic clearer — and that’s okay while learning. #LeetCode #DSA #LinkedList #Python #100DaysOfCode #CodingJourney
To view or add a comment, sign in
-
-
🚀 𝐃𝐚𝐲 𝟏𝟑 𝐨𝐟 #𝟏𝟔𝟎𝐃𝐚𝐲𝐬𝐎𝐟𝐂𝐨𝐝𝐞 — 𝐏𝐚𝐥𝐢𝐧𝐝𝐫𝐨𝐦𝐞 𝐍𝐮𝐦𝐛𝐞𝐫 | 𝐒𝐭𝐫𝐢𝐧𝐠𝐬 🧩 Today’s focus was on a simple yet classic logic check — determining if a number reads the same forward and backward. While it looks straightforward, it’s a great reminder that elegant solutions often come from clarity, not complexity. Problem: 𝐂𝐡𝐞𝐜𝐤 𝐢𝐟 𝐚𝐧 𝐢𝐧𝐭𝐞𝐠𝐞𝐫 𝐢𝐬 𝐚 𝐩𝐚𝐥𝐢𝐧𝐝𝐫𝐨𝐦𝐞. Approach: Convert the integer to a string and compare it with its reverse. Time Complexity: O(n) Space Complexity: O(n) This small exercise reinforces foundational reasoning that scales up when designing more complex systems — where reversing, mirroring, or symmetry detection shows up in data validation, pattern recognition, and even natural language tasks. 🔗 GitHub: https://lnkd.in/gaim_PJS #Python #LeetCode #CodingChallenge #160DaysOfCode #ProblemSolving #DSA #AIEngineerJourney
To view or add a comment, sign in
-
🚀 DSA Challenge – Day 83 Problem: Minimum Possible Length of Original String from Concatenated Anagrams 🔡✨ This was an elegant string manipulation and frequency-matching problem — a beautiful blend of observation and brute-force validation. 🧠 Problem Summary: You are given a string s, known to be a concatenation of anagrams of some original string t. The task is to determine the minimum possible length of t. ✅ Each substring of length len(t) should contain exactly the same frequency of characters. ✅ The string s can thus be divided into equal-length chunks, all being anagrams of t. ✅ The goal is to find the smallest such length that satisfies this property. ⚙️ My Approach: 1️⃣ Iterate through all divisors i of n = len(s) — potential lengths of t. 2️⃣ For each possible i, check if the string can be divided into equal parts where each part has identical character frequencies. 3️⃣ Use hash maps to store frequency counts and compare each block. 4️⃣ Return the smallest i that satisfies the condition. 📈 Complexity: Time: O(n²) → Checking frequency for each valid divisor. Space: O(k) → For storing character counts per block. ✨ Key Takeaway: When tackling anagram-based problems, frequency comparison is your strongest ally. Instead of guessing patterns, rely on structure and repetition to reveal the hidden base string. 🔍 🔖 #DSA #100DaysOfCode #LeetCode #ProblemSolving #StringManipulation #Anagram #Python #CodingChallenge #InterviewPrep #EfficientCode #HashMap #DataStructures #TechCommunity #CodeEveryday #LearningByBuilding
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