Understanding Grid Ways using Recursion 🚀 Recently, I explored an interesting DSA problem — finding the number of ways to move from the top-left corner (0,0) to the bottom-right (n-1, m-1) in a grid. 👉 Allowed moves: • Right ➡ • Down ⬇ 💡 Key Idea: From any cell (i, j), we have 2 choices: → Move Right → (i, j+1) → Move Down → (i+1, j) So the recurrence becomes: f (i , j) = f (i + 1, j) + f (i, j + 1) ⚡ Insight: • Time Complexity: O(2^(n+m)) • Optimized using combinatorics: Total ways = (n+m-2)! / ((n-1)! (m-1)!) This problem helped me understand how recursion explores all possible paths and how we can optimize it further mathematically. Next step: diving deeper into optimization techniques🚀 #DSA #Java #Recursion #Backtracking #ProblemSolving #LearningJourney
Grid Recursion Problem Solving Techniques
More Relevant Posts
-
🚀 DSA Preparation 💪 Solved an advanced Binary Search problem on a rotated sorted array. Learned how to identify the sorted half and efficiently narrow down the search space 🔥 🧠 Problem 🔎 Search in Rotated Sorted Array Given a sorted array nums that is rotated at an unknown index, and a target value: 👉 Return the index of the target if found, otherwise return -1. 👉 The solution must run in O(log n) time complexity. Example Input: nums = [4,5,6,7,0,1,2], target = 0 Output: 4 Input: nums = [4,5,6,7,0,1,2], target = 3 Output: -1 Input: nums = [1], target = 0 Output: -1 ⚡ Key Learning 📌 Even after rotation, one half of the array is always sorted 📌 Use Binary Search to decide which half to explore 📌 Time Complexity: O(log n) → reduces search space by half in each step, making it highly efficient even for large inputs 📌 Space Complexity: O(1) Improving DSA with advanced searching techniques 🚀 #DSA #LeetCode #BinarySearch #Algorithms #Java #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
Consistency in solving DSA problems is helping me think in terms of patterns and optimizations rather than brute force. 🚀 Today’s Practice: Circular Array Query Problem The task was to find the minimum circular distance between indices having the same value for given queries. 💡 Key Insight: Instead of checking every index repeatedly, we can preprocess the array using a value → indices mapping. ⚡ Efficient Approach: • Store all indices for each value in a map • Use binary search to locate the current index • Check nearest neighbors in a circular manner 🧠 Approach: 1️⃣ Build a map of value → list of indices 2️⃣ For each query index: • Get all occurrences of that value • If only one → return -1 3️⃣ Use binary search to find position 4️⃣ Check previous & next indices (circular) 5️⃣ Compute minimum circular distance ⏱️ Time Complexity: O(n + q log n) 📦 Space Complexity: O(n) 📌 Key Learnings: • Preprocessing (hashing) reduces repeated work • Binary search helps in fast lookups • Circular arrays require careful distance handling #DSA #Algorithms #CodingPractice #Java #ProblemSolving #LearningJourney #TechGrowth #DeveloperJourney GeeksforGeeks National Payments Corporation Of India (NPCI)
To view or add a comment, sign in
-
-
🚀 DSA Preparation 💪 Solved a fundamental Binary Search problem, a key concept for efficient searching. Focused on reducing search space by half in each step to achieve optimal performance 🔥 🧠 Problem 🔎 Binary Search Given a sorted array nums and a target value, return its index if found. If the target does not exist, return -1. 👉 The solution must run in O(log n) time complexity. Example Input: nums = [-1,0,3,5,9,12], target = 9 Output: 4 Input: nums = [-1,0,3,5,9,12], target = 2 Output: -1 ⚡ Key Learning 📌 Binary Search works only on sorted arrays 📌 Each step halves the search space → highly efficient 📌 Time Complexity: O(log n) 📌 Space Complexity: O(1) Improving DSA with strong fundamentals 🚀 #DSA #LeetCode #BinarySearch #Algorithms #Java #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
🚀 #100DaysOfDSA – Day 9 Today’s problem was one of the most popular in DSA — Maximum Subarray Sum. 🔍 Problem Statement: Given an integer array, find the contiguous subarray with the largest sum. 💡 Brute Force Approach: Check all possible subarrays and calculate their sums. 🧠 Idea: ✔️ Generate all subarrays ✔️ Compute sum for each ✔️ Track maximum ⚠️ Time Complexity: O(n²) (or O(n³) if sum is recalculated every time) ⚠️ Space Complexity: O(1) ⚡ Optimized Approach (Kadane’s Algorithm): 💡 Key Insight: At every step, decide: 👉 Start a new subarray 👉 OR continue the existing one 🧠 Logic: ✔️ sum = max(nums[i], sum + nums[i]) ✔️ Track maximum sum globally ⚡ Time Complexity: O(n) ⚡ Space Complexity: O(1) 💻 Code (Kadane’s Algorithm): class Solution { public int maxSubArray(int[] nums) { int sum = 0; int max_sum = Integer.MIN_VALUE; for (int i = 0; i < nums.length; i++) { sum = Math.max(nums[i], sum + nums[i]); max_sum = Math.max(max_sum, sum); } return max_sum; } } 🔥 Takeaway: Brute force helps understand the problem, but recognizing patterns like Kadane’s Algorithm helps optimize from O(n²) → O(n). That’s the real DSA growth 📈 #100DaysOfDSA #100DaysChallenge #CodingJourney #Java #ProblemSolving #InterviewPrep #KadaneAlgorithm #DSA
To view or add a comment, sign in
-
🚀 Consistency + Clarity = Progress Today I solved the Root to Leaf Paths problem on binary trees using recursion and backtracking. 🔍 Key Learnings: Understood the backtracking pattern (add → recurse → remove) Learned how to correctly identify leaf nodes Improved clarity on managing state (path list) during recursion 💡 What made the difference: Instead of memorizing, I focused on understanding the pattern — and everything clicked. 📊 Result: ✅ All test cases passed (1115/1115) ✅ 100% accuracy This is part of my ongoing journey to strengthen my Data Structures & Algorithms (DSA) fundamentals. 🎯 Takeaway: Mastering patterns like recursion and backtracking makes complex problems feel simple. #DSA #Java #Coding #ProblemSolving #Recursion #Backtracking #BinaryTree #LearningJourney #Consistency
To view or add a comment, sign in
-
-
Consistency in solving DSA problems is helping me turn repetitive work into efficient solutions. 🚀 Today’s Practice: Range Mean Queries The task was to find the mean of elements for multiple subarray queries. 💡 Key Insight: Calculating sum for every query separately is expensive. Using Prefix Sum makes it efficient. ⚡ Efficient Approach: • Precompute prefix sum array • Use it to get range sum in O(1) • Divide by length to get mean 🧠 Approach: 1️⃣ Build prefix sum array 2️⃣ For each query (l, r): • If l == 0 → sum = prefix[r] • Else → sum = prefix[r] - prefix[l-1] 3️⃣ Compute mean = sum / (r - l + 1) 4️⃣ Store result ⏱️ Time Complexity: O(n + q) 📦 Space Complexity: O(n) 📌 Key Learnings: • Prefix Sum is powerful for range queries • Preprocessing reduces repeated computation • Turning O(n) per query → O(1) is a big optimization #GeekStreak60 #DSA #Algorithms #PrefixSum #CodingPractice #Java #ProblemSolving #LearningJourney #TechGrowth GeeksforGeeks National Payments Corporation Of India (NPCI)
To view or add a comment, sign in
-
-
#CodeEveryday — My DSA Journey | Day 14 🧩 Problem Solved: Search a 2D Matrix (LeetCode #74) 💭 What I Learned: Used binary search by treating the 2D matrix as a flattened sorted array. Key idea: 👉 Convert 1D index → 2D coordinates using: row = mid / number_of_columns col = mid % number_of_columns At each step: ✔️ Calculated row and column from mid ✔️ Compared matrix value with target ✔️ Adjusted search space accordingly This approach avoids nested loops and gives optimal performance. ⏱ Time Complexity: O(log (m × n)) 🧠 Space Complexity: O(1) ⚡ Key Takeaways: ✔️ 2D problems can often be reduced to 1D using indexing tricks ✔️ Binary search works whenever the data is globally sorted ✔️ Index mapping is a powerful technique in matrix problems 💻 Language Used: Java ☕ 📘 Concepts: Binary Search · Matrix · Index Mapping · Optimization #CodeEveryday #DSA #LeetCode #Java #BinarySearch #Matrix #ProblemSolving #Algorithms #CodingJourney #Consistency 🚀
To view or add a comment, sign in
-
-
Consistency in solving DSA problems is helping me think in terms of patterns and optimizations rather than brute force. 🚀 Today’s Practice: Circular Array Query Problem The task was to find the **minimum circular distance** between indices having the same value for given queries. 💡 Key Insight: Instead of checking every index repeatedly, we can preprocess the array using a **value → indices mapping**. ⚡ Efficient Approach: • Store all indices for each value in a map • Use binary search to locate the current index • Check nearest neighbors in a circular manner 🧠 Approach: 1️⃣ Build a map of value → list of indices 2️⃣ For each query index: • Get all occurrences of that value • If only one → return -1 3️⃣ Use binary search to find position 4️⃣ Check previous & next indices (circular) 5️⃣ Compute minimum circular distance ⏱️ Time Complexity: O(n + q log n) 📦 Space Complexity: O(n) 📌 Key Learnings: • Preprocessing (hashing) reduces repeated work • Binary search helps in fast lookups • Circular arrays require careful distance handling #geekstreak60 #DSA #Algorithms #CodingPractice #Java #ProblemSolving #LearningJourney #TechGrowth #DeveloperJourney GeeksforGeeks National Payments Corporation Of India (NPCI)
To view or add a comment, sign in
-
-
Day 11/60 This graph illustrates how different algorithms perform as the input size (n) increases. The x-axis represents input size, and the y-axis represents time (or operations). 💡 Explanation of Each Complexity: 🔹 O(1) — Constant Time Execution time remains the same regardless of input size. ➡️ Example: Accessing an element in an array 🔹 O(log n) — Logarithmic Time Time increases slowly as input grows. ➡️ Example: Binary Search 🔹 O(n) — Linear Time Execution time grows proportionally with input size. ➡️ Example: Iterating through a list 🔹 O(n log n) — Linearithmic Time Efficient for sorting large datasets. ➡️ Example: Merge Sort, Quick Sort 🔹 O(n²) — Quadratic Time Time increases rapidly due to nested operations. ➡️ Example: Nested loops (Bubble Sort) 🔹 O(2ⁿ) — Exponential Time Time doubles with each additional input. ➡️ Example: Recursive problems (subsets, Fibonacci) 🔹 O(n!) — Factorial Time Extremely slow growth; impractical for large inputs. ➡️ Example: Generating all permutations ⚠️ Key Insight As input size increases: Lower complexity algorithms scale efficiently Higher complexity algorithms become impractical 📌 Summary Order (Best → Worst) O(1) → O(log n) → O(n) → O(n log n) → O(n²) → O(2ⁿ) → O(n!) #DataStructures #Algorithms #BigO #TimeComplexity #Java #Programming #SoftwareEngineering #BackendDevelopment #DSA #Coding
To view or add a comment, sign in
-
-
Day 76/100 | #100DaysOfDSA 🧩🚀 Today’s problem: Lowest Common Ancestor of a Binary Tree A fundamental tree problem that builds strong recursion intuition. Problem idea: Find the lowest node in the tree that has both given nodes as descendants. Key idea: DFS + recursion (bottom-up approach). Why? • Each subtree can independently tell if it contains p or q • Combine results while backtracking • First node where both sides return non-null → answer How it works: • If current node is null / p / q → return it • Recursively search left and right subtree • If both left & right are non-null → current node is LCA • Else return the non-null side Time Complexity: O(n) Space Complexity: O(h) (recursion stack) Big takeaway: Tree problems often rely on post-order traversal + combining child results. Understanding this pattern unlocks many binary tree problems. 🔥 Day 76 done. 🚀 #100DaysOfCode #LeetCode #DSA #Algorithms #BinaryTree #Recursion #DFS #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity
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