Day 88 of my #100DaysOfCode journey 🚀 Today I implemented Depth First Search (DFS) on Graphs using recursion. Unlike BFS, DFS explores as deep as possible along a branch before backtracking. It follows a recursive approach (or stack-based) and is fundamental for many advanced graph problems. Approach: • Start from a source node (0 in this case) • Mark it as visited • Recursively visit all unvisited neighbors Key concepts reinforced: • Graph traversal using DFS • Recursion in graphs • Using a visited set to avoid infinite loops (cycles) Time Complexity: • O(V + E) → where V = vertices, E = edges DFS is widely used in: • Cycle detection • Topological sorting • Connected components • Backtracking problems Now having covered both BFS and DFS, the core graph traversal foundation is in place. Graphs officially unlocked. 🔓 #100DaysOfCode #DSA #Graphs #DFS #Algorithms #Python #CodingJourney #ProblemSolving #TechLearning #SoftwareEngineering
Implementing Depth First Search (DFS) on Graphs with Recursion
More Relevant Posts
-
Day 22/100 – DSA Journey Problem: Find Mode in Binary Search Tree (BST) Today’s problem focused on understanding how Binary Search Trees (BST) behave and how we can efficiently extract useful insights from them. Understanding the BST A Binary Search Tree follows a structured property: Left subtree → values ≤ root Right subtree → values ≥ root Because of this, when we perform an Inorder Traversal (Left → Root → Right), the values are visited in sorted order. Why Inorder Traversal? Since duplicates appear consecutively in a sorted sequence, inorder traversal allows us to: Track frequency without using extra space Compare current value with previous value Efficiently determine the most frequent element (mode) Approach Used Traverse the BST using inorder traversal Maintain: Previous value Current count Maximum frequency Update result list whenever a new maximum frequency is found Key Learning This problem highlights how leveraging tree properties can help optimize solutions. Instead of using extra space (like hashmaps), we used traversal behavior to achieve an efficient solution. Conclusion Understanding the underlying structure of data (like BST properties) is often more powerful than brute-force approaches. Smart traversal choices can significantly reduce space complexity and improve performance. #Day22 #100DaysOfCode #DSA #BinarySearchTree #Python #CodingJourney #LeetCode #ProblemSolving
To view or add a comment, sign in
-
-
✅ Day 98 of 100 Days LeetCode Challenge Problem: 🔹 #338 – Counting Bits 🔗 https://lnkd.in/gXdNxX66 Learning Journey: 🔹 Today’s problem focused on counting the number of 1s in the binary representation of numbers from 0 to n. 🔹 I initialized an array ans of size n+1. 🔹 For each number i, I converted it to binary using bin(i)[2:]. 🔹 Counted the number of '1' bits by iterating through the binary string. 🔹 Stored the count in ans[i] and returned the final array. Concepts Used: 🔹 Bit Manipulation (Binary Representation) 🔹 Array Traversal 🔹 String Processing 🔹 Brute Force Approach Key Insight: 🔹 Each number’s bit count can be computed independently. 🔹 Converting to binary and counting '1' works, but can be optimized further using DP or bit tricks. Complexity: 🔹 Time: O(n log n) (binary conversion for each number) 🔹 Space: O(n) #LeetCode #Algorithms #DataStructures #100DaysOfCode #Python #CodingJourney #ProblemSolving #LearningInPublic
To view or add a comment, sign in
-
-
🚀 Day 39/60 — LeetCode Discipline Problem Solved: Sqrt(x) Difficulty: Easy Today’s challenge was to compute the square root of a number without using built-in functions. Instead of brute force, I used Binary Search — a classic, elegant approach that narrows down the answer efficiently. 💡 Key Learnings: • Binary Search application beyond arrays • Handling edge cases (x < 2) • Avoiding overflow using conditions carefully • Finding floor value of square root • Optimized thinking over brute force ⚡ Performance: Runtime: 4 ms Like walking in a foggy path, I didn’t see the answer directly… But step by step— cutting the search space in half— the truth revealed itself. That’s the beauty of algorithms. #LeetCode #60DaysOfCode #DSA #BinarySearch #ProblemSolving #CodingJourney #Python #Consistency #TechGrowth
To view or add a comment, sign in
-
-
🗓 7 April 2026 LeetCode Problem #128 – Longest Consecutive Sequence Solved the problem of finding the longest consecutive sequence in an unsorted array. Key insight: use a set for O(1) lookups and only start counting sequences from numbers that are the beginning of a sequence. Takeaways: - Using the right data structure reduces time complexity from O(n²) to O(n). - Avoid redundant work while scanning arrays. - Handle edge cases like empty or single-element arrays efficiently. This problem reinforces how a smart approach beats brute force every time! #LeetCode #Algorithms #Python #DataStructures #ProblemSolving #Coding #TechLearning
To view or add a comment, sign in
-
-
Day 14/100 – Data Structures & Algorithms Today, I worked on the problem “First Unique Character in a String.” Overview The task is to identify the first non-repeating character in a string and return its index. If no such character exists, the result is -1. Approach I used a two-pass strategy: • First pass to store character frequencies using a hashmap • Second pass to identify the first character with a frequency of one Complexity • Time Complexity: O(n) • Space Complexity: O(1) Key Takeaway This problem reinforces how effective hashmaps are for frequency-based problems and how a simple two-pass approach can lead to optimal solutions. Staying consistent and building problem-solving intuition step by step. #Day14 #100DaysOfCode #DSA #Python #LeetCode #ProblemSolving #SoftwareEngineering
To view or add a comment, sign in
-
-
LeetCode Problem 1448. Count Good Nodes in Binary Tree: "Given a binary tree root, a node X in the tree is named good if in the path from root to X there are no nodes with a value greater than X. Return the number of good nodes in the binary tree." Approach: make use of pre-order traversal i.e., visit root->left->right child, in each visit, keep track of the max element upto that point by maintaining a stack, top of the stk should represent the max element upto that point in the path, pop() elements from stack when both the children (left,right) are explored. Time Complexity: O(n) Space Complexity: O(n) b/c of maintaining a stack #Python #LeetCode #DSA #DataStructures #Algorithms #Stack #Recursion #DFS #BinaryTree #ProblemSolving
To view or add a comment, sign in
-
-
🚀 DSA Practice Update! Solved Remove Duplicates from Sorted Array on LeetCode with an optimized approach. Brute Force: O(n log n) time | O(n) space Optimized (Two Pointer):O(n) time | O(1) space Choosing the right approach matters more than just solving the problem. The Brute Force approach removes duplicates by using extra data structures like a set and then sorting the array again. Because of sorting, its time complexity becomes O(n log n) and it also requires O(n) extra space. Additionally, it is not in-place, which makes it less efficient and not ideal for optimized solutions. “I used a two-pointer approach where one pointer tracks the position of unique elements and the other scans the array. This gives O(n) time and O(1) space complexity.” Consistency + Optimization = Growth 📈 #DSA #LeetCode #InterviewPrep #Python #CodingJourney
To view or add a comment, sign in
-
-
Days 68-69 of the #three90challenge 📊 Today I explored NumPy operations — specifically indexing and slicing arrays. After understanding NumPy basics, this step made it easier to access and manipulate data efficiently. What I practiced today: • Accessing elements using indexing • Extracting subsets of data using slicing • Working with multi-dimensional arrays • Performing operations on selected data Example thinking: Instead of looping through data manually, I can directly select and operate on specific parts of an array. Example: import numpy as np arr = np.array([10, 20, 30, 40, 50]) print(arr[1:4]) # Output: [20 30 40] This makes data manipulation faster and more intuitive. From handling data → to controlling it efficiently 🚀 GeeksforGeeks #three90challenge #commitwithgfg #Python #NumPy #DataAnalytics #LearningInPublic #Consistency #Upskilling
To view or add a comment, sign in
-
Day 91 of my #100DaysOfCode journey 🚀 Today I solved Cycle Detection in an Undirected Graph using Breadth First Search (BFS). Problem intuition: In an undirected graph, a cycle exists if during traversal we reach an already visited node that is not the parent of the current node. Approach: • Traverse all graph components • Use a queue for BFS • Store both the current node and its parent in the queue • If a visited neighbor is found that is not the parent → cycle exists Key insight: In undirected graphs, revisiting the parent is normal. But revisiting any other already visited node indicates a cycle. Concepts reinforced: • BFS on graphs • Parent tracking • Connected components • Cycle detection logic Time Complexity: • O(V + E) → where V = vertices, E = edges This is one of the most important graph interview patterns because the same idea extends into: • DFS cycle detection • Detecting cycles in connected/disconnected graphs • Advanced graph traversal problems Slowly building stronger graph intuition one pattern at a time. 🌱 #100DaysOfCode #DSA #Graphs #BFS #CycleDetection #Algorithms #Python #CodingJourney #ProblemSolving #SoftwareEngineering
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