🚀 Day 74 of #100DaysOfCode Today’s challenge was a clean and efficient array problem — LeetCode: Merge Sorted Array ✅ 📌 Problem Summary You’re given two sorted arrays, where the first array has extra space at the end. The task is to merge the second array into the first in-place, keeping everything sorted. 🧠 My Approach Instead of shifting elements forward (which is costly), I used a reverse two-pointer technique: Start comparing elements from the end of both arrays Place the larger element at the last available position Move pointers accordingly until all elements are merged This avoids extra space and keeps the solution optimal. ⚙️ Complexity ⏱ Time: O(m + n) 💾 Space: O(1) (in-place merge) 🔥 Key Learning Working from the back can simplify in-place array problems Two-pointer strategies are incredibly powerful for sorted data Another day, another solid problem solved 💪 Onward to Day 75 🚀 #100DaysOfCode #LeetCode #Java #DSA #ProblemSolving #CodingJourney
Dinesh Kolhe’s Post
More Relevant Posts
-
🚀 Day 5/50 – LeetCode Challenge 🧩 Length of Last Word Today’s problem focused on string handling and edge case management. 📌 Problem Summary: Given a string consisting of words and spaces, return the length of the last word in the string. A word is defined as a sequence of non-space characters. 🔍 Approach Used: Ignored trailing spaces Traversed the string from the end Counted characters until a space was found This approach avoids unnecessary splitting and improves efficiency. ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(1) 💡 Key Learning: ✔️ Efficient string traversal ✔️ Handling trailing spaces ✔️ Edge case management ✔️ Avoiding extra memory usage A simple problem that highlights the importance of clean logic and careful handling of input. Consistency builds mastery 💪 🔗 Problem Link: https://lnkd.in/ghA_rYmP #50DaysOfLeetCode #LeetCode #DSA #Java #Strings #ProblemSolving #CodingJourney #FutureAIEngineer #Consistency
To view or add a comment, sign in
-
-
🚀 Day 523 of #750DaysOfCode 🚀 📌 Problem Solved: Check if Binary String Has at Most One Segment of Ones (LeetCode 1784) 🔎 Problem Summary: Given a binary string s (without leading zeros), we need to check whether the string contains at most one contiguous segment of 1s. If the 1s appear in more than one separate segment, we return false. (LeetCode) 💡 Key Insight: Since the string starts with 1, the only valid structure is: 111...000... If we ever encounter the pattern "01", it means a 1 appeared again after a 0, which creates multiple segments of ones. (AlgoMonster) ⚙️ Approach: Traverse the string or simply check if "01" exists. If "01" is found → more than one segment → return false. ⏱ Complexity: Time: O(n) Space: O(1) 📚 Takeaway: Sometimes string problems become much easier when we identify patterns instead of counting segments. Consistency > Motivation. On to Day 524 tomorrow. 💪 #LeetCode #Java #DataStructures #Algorithms #CodingChallenge #Consistency #ProblemSolving #100DaysOfCode #750DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 7 of #100DaysOfCode Solved Search a 2D Matrix on LeetCode using an optimized matrix traversal approach 🔎 🧠 Key insight: Since rows and columns are sorted, starting from the top-right corner helps eliminate either a row or a column in each step. ⚙️ Approach: 🔹Start at top-right element 🔹If value < target → move down 🔹If value > target → move left 🔹Repeat until target is found or bounds are crossed ⏱️ Time Complexity: O(m + n) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #Matrix #Java #ProblemSolving #LearningInPublic #CodingJourney
To view or add a comment, sign in
-
-
Day 81 of my LeetCode Journey 🔥 📘 Problem: 108. Convert Sorted Array to Binary Search Tree 🎯 Difficulty: Easy 🌐 Platform: LeetCode 🔹 Problem Statement: Given an integer array nums sorted in ascending order, convert it into a height-balanced Binary Search Tree (BST). 🔹 Approach Used: Pick the middle element of the array as the root Recursively build the left subtree using the left half Recursively build the right subtree using the right half This ensures the tree remains height-balanced 🔹 Key Concepts: Binary Search Tree property Divide and conquer Recursion Height-balanced tree construction 🔹 Learning: This problem shows how sorted data can be leveraged to build balanced structures efficiently. Choosing the middle element at every step guarantees minimal height and optimal search performance — a classic divide-and-conquer pattern in tree construction. Time Complexity: O(n) Space Complexity: O(log n) (recursion stack) #LeetCode #Day81 #Java #BST #Trees #DSA #ProblemSolving #Recursion #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 19 of My LeetCode Challenge Today’s problem: Find All Duplicates in an Array (LeetCode 442) 🔍 Problem Summary: Given an array of integers where each value appears once or twice and lies between 1 and n, return all elements that appear twice. 🧠 Key Insight: Since numbers are in the range 1 to n, we can use the array itself to track visited elements by marking indices negative. ⚙ Approach (O(n) time | O(1) space): ✔ Traverse the array ✔ Use value → index mapping ✔ If index already negative → duplicate found ⏱ Complexity: ✅ Time: O(n) ✅ Space: O(1) 📌 What I Learned Today: Using index mapping for in-place marking How constraints help optimize space Recognizing patterns for array-based hashing #Day19 #LeetCode #Java #DSA #CodingChallenge #ProblemSolving
To view or add a comment, sign in
-
-
🔥 Day 84 of #100DaysOfCode Today’s problem: LeetCode: Find K Closest Elements 🎯 📌 Problem Summary Given: A sorted array arr An integer k A target value x Return the k closest integers to x in the array. The result must be sorted in ascending order. Example: arr = [1,2,3,4,5], k = 4, x = 3 Output → [1,2,3,4] 🧠 Approach: Two Pointers Shrinking Window Since the array is already sorted, we can: Start with: l = 0 r = arr.length - 1 While the window size is bigger than k: Compare distances: |x - arr[l]| |x - arr[r]| Remove the element that is farther from x ⚙️ Core Idea: while (r - l >= k): if left is closer: r-- else: l++ Finally, return elements from l to r. ⏱ Time Complexity: O(n - k) 💾 Space Complexity: O(1) (excluding result list) 🚀 Performance ⚡ Runtime: 4ms (98% faster) Solid optimization using the sorted property of the array. 💡 What I Learned When array is sorted → always think Two Pointers. Instead of building result, shrink unnecessary elements. Cleaner logic often gives better performance. Consistency > Motivation. On to Day 85 🔥 #100DaysOfCode #LeetCode #TwoPointers #Java #DSA #CodingJourney #InterviewPrep
To view or add a comment, sign in
-
-
🚀 Day 20/60 Completed – LeetCode Problem of the Day Challenge Today’s challenge: Check if Binary String Has at Most One Segment of Ones The problem asks us to determine whether a binary string contains at most one continuous segment of '1's. 🧠 Approach & Key Learnings: • Traversed the string once from left to right. • Once a segment of '1's starts, we track it. • If we encounter '0' after the first segment of '1's, we mark that a break occurred. • If another '1' appears after that zero, it means there is more than one segment of ones → return false. • Otherwise, the string contains only one contiguous segment of ones → return true. ⚙️ Key Concepts Practiced: • String traversal • Segment detection in binary strings • Boolean flag tracking • Writing clean linear-time logic Small problem, but a good exercise in carefully tracking patterns while scanning a string. 20 days down. Consistency is getting stronger every day. ⚡ 40 days to go — staying locked in. 🔥 #LeetCode #60DaysChallenge #ProblemOfTheDay #DSA #Java #CodingJourney #Consistency #SoftwareEngineering
To view or add a comment, sign in
-
-
🚀 Day 76 of #100DaysOfCode Today I worked on a clean and elegant array manipulation problem — LeetCode: Rotate Array ✅ 📌 Problem Summary Given an array, rotate it to the right by k steps in-place, without using extra space. 🧠 Approach Used: Reverse Technique Instead of shifting elements one by one, I used a smart 3-step reverse strategy: Reverse the entire array Reverse the first k elements Reverse the remaining n − k elements This achieves the rotation efficiently and keeps the solution simple. ⚙️ Complexity ⏱ Time: O(n) 💾 Space: O(1) (in-place) 🔥 Key Learnings Sometimes the best solution is about reordering operations, not adding complexity In-place algorithms are powerful when space constraints matter Reverse logic is a recurring and underrated pattern in array problems Onward to Day 77 🚀 Consistency > Motivation 💪 #100DaysOfCode #LeetCode #Java #DSA #Arrays #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
Day 27/100 – LeetCode Challenge 🚀 Problem: Sort List Approach: Applied Merge Sort on the linked list Used slow and fast pointers to find the middle Recursively sorted both halves Merged the sorted halves Time Complexity: O(n log n) Space Complexity: O(log n) (recursion stack) Key takeaway: Merge sort is the most efficient sorting technique for linked lists, as it avoids random-access operations required by other algorithms. #LeetCode #100DaysOfCode #DSA #Java #LinkedList #MergeSort #ProblemSolving
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