Day 7 🚀 of the #100DaysOfLeetCode challenge Problem 88: Merge Sorted Array Today’s focus was on in-place merging using the insertion and shifting approach. Instead of using an extra array, I compared elements from nums1 and nums2. Whenever an element from nums2 was smaller, I shifted the remaining elements in nums1 to the right and inserted it at the correct position. After each insertion, I updated the effective size of nums1 and continued the process until all elements were merged. This approach helped me clearly understand: -->How in-place array manipulation works -->Why boundary management (m and n) is critical -->How shifting impacts time complexity Time Complexity: O(m × n) in worst case due to shifting Space Complexity: O(1) (no extra space used) Even though there’s a more optimal two-pointer solution from the back (O(m + n)), practicing this method strengthened my fundamentals in array handling and index control. Every problem teaches something beyond just passing test cases. Consistency > Intensity. #LeetCode #Java #DataStructures #ProblemSolving #CodingJourney #100DaysOfCode
Merge Sorted Array with In-Place Insertion Approach
More Relevant Posts
-
Day 70 - LeetCode Journey Solved LeetCode 88: Merge Sorted Array (Easy) today — a classic problem that focuses on array manipulation and the two-pointer technique. The challenge is to merge two sorted arrays into one sorted array without using extra space, by modifying nums1 in-place. 💡 Core Idea: Instead of merging from the beginning, we start from the end of the arrays. Why? Because nums1 already has extra space at the end to accommodate elements from nums2. We use three pointers: • i → last valid element in nums1 • j → last element in nums2 • k → last position of merged array At each step, we place the larger element at position k, ensuring the array remains sorted. ⚡ Key Learning Points: • Using the two-pointer technique from the end • Performing in-place array merging • Maintaining O(m + n) time complexity • Avoiding unnecessary extra space This problem is a great reminder that sometimes changing the direction of traversal makes the solution much simpler. ✅ Better understanding of array manipulation ✅ Stronger grasp of two-pointer techniques ✅ Improved problem-solving efficiency Small problems like this build strong fundamentals for bigger challenges 🚀 #LeetCode #DSA #Java #TwoPointers #Arrays #Algorithms #ProblemSolving #CodingJourney #Consistency #100DaysOfCode #InterviewPreparation #DeveloperGrowth #KeepCoding
To view or add a comment, sign in
-
-
Day 15/100 – LeetCode Challenge Problem Solved: Rotate Image Today’s problem involved rotating an n × n matrix by 90 degrees clockwise without using additional space. The challenge is performing the transformation directly on the existing matrix. The key idea behind the solution is breaking the rotation into two simpler operations. First, transpose the matrix by swapping elements across the main diagonal. This converts rows into columns. After the transpose, reverse each row of the matrix. Together, these two steps effectively rotate the matrix by 90 degrees clockwise. This approach avoids creating a new matrix and performs all operations in-place, which satisfies the constraint of constant extra space. Time Complexity: O(n²) Space Complexity: O(1) Performance: Runtime 0 ms Key takeaway: Complex matrix transformations often become easier when decomposed into simpler operations like transpose and reversal. Day 15 completed. Staying consistent and continuing to strengthen problem-solving skills. #100DaysOfLeetCode #Java #Algorithms #Matrix #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
🚀 Day 12 of My LeetCode Journey Today I solved Rotate Image (LeetCode 48). Problem: Given an n × n matrix representing an image, rotate the matrix by 90 degrees clockwise. The challenge is to perform the rotation in-place, without using another matrix. Approach: I used a simple two-step trick: 1️⃣ Transpose the matrix (swap rows and columns) 2️⃣ Reverse each row This combination effectively rotates the matrix by 90° clockwise. Time Complexity: O(n²) Space Complexity: O(1) Problems like this improve understanding of matrix manipulation and in-place transformations. #leetcode #datastructures #algorithms #java #codinginterview #softwareengineering
To view or add a comment, sign in
-
-
Day 17/100 – LeetCode Challenge Problem Solved: Spiral Matrix Today’s problem required traversing a matrix in spiral order, starting from the top-left corner and moving right, down, left, and up repeatedly until all elements are visited. The key idea is to maintain four boundaries that represent the current layer of the matrix being processed: top, bottom, left, and right. We move in four directions step by step: ->Traverse from left to right across the top row. ->Traverse from top to bottom along the right column. ->Traverse from right to left across the bottom row. ->Traverse from bottom to top along the left column. After completing each direction, the corresponding boundary is adjusted inward so the traversal continues with the inner layer of the matrix. This process continues until the boundaries cross, ensuring every element is visited exactly once. Time Complexity: O(m × n) Space Complexity: O(1) (excluding the result list) Performance: Runtime 0 ms Key takeaway: Many matrix traversal problems become manageable when you define clear boundaries and shrink them step by step. Day 17 completed. Continuing the journey of strengthening algorithmic thinking through consistent practice. #100DaysOfLeetCode #Java #Algorithms #MatrixTraversal #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
🚀 Day 521 of #750DaysOfCode 🚀 🔎 1582. Special Positions in a Binary Matrix (LeetCode - Easy) Today I solved a simple yet interesting matrix traversal problem. 🧠 Problem Summary We are given an m × n binary matrix. A position (i, j) is called special if: ✔ mat[i][j] == 1 ✔ All other elements in row i are 0 ✔ All other elements in column j are 0 Our task is to count how many such special positions exist. 💡 Key Idea Instead of checking the entire row and column every time, we can: 1️⃣ Count the number of 1s in every row 2️⃣ Count the number of 1s in every column Then a cell (i, j) is special if: mat[i][j] == 1 rowCount[i] == 1 colCount[j] == 1 ⏱ Time Complexity O(m × n) We traverse the matrix twice. 🧩 What I Learned ✔ Efficiently using row and column counting ✔ Simplifying matrix conditions with precomputation ✔ Writing cleaner solutions for matrix-based problems Step by step progress every day. Consistency is the real key. 🚀 #LeetCode #Java #MatrixProblems #ProblemSolving #DataStructures #CodingJourney #750DaysOfCode #Day521
To view or add a comment, sign in
-
-
Day 49 – DSA Journey 🚀 | Revisiting a Classic Merge Problem Continuing my daily DSA practice, today I revisited a classic array problem on LeetCode to strengthen my understanding of pointer-based logic. 📌 Problem Practiced: Merge Sorted Array (LeetCode 88) 🔍 Problem Idea: We are given two sorted arrays where the first array has extra space at the end to hold elements from the second array. The task is to merge both arrays into a single sorted array inside the first array. 💡 Key Insight: Instead of merging from the start (which may overwrite elements), we can start from the end and place the larger element first using pointers. 📌 Approach Used: • Use three pointers for the last valid elements of both arrays and the final position • Compare elements from the end of both arrays • Place the larger element at the last position of the first array • Move pointers until all elements are merged 📌 Concepts Strengthened: • Two-pointer technique • In-place array manipulation • Merge logic used in Merge Sort • Careful pointer movement ⏱️ Time Complexity: O(m + n) 📦 Space Complexity: O(1) Today’s takeaway: Revisiting problems helps reinforce core concepts and improves clarity in implementation. On to Day 50! 🚀 #Day49 #DSAJourney #LeetCode #Arrays #TwoPointers #Java #ProblemSolving #LearningInPublic #Consistency
To view or add a comment, sign in
-
-
Day 21 of my LeetCode consistency journey 🚀 Today I solved the Daily Temperatures problem. The goal was to determine how many days we must wait to experience a warmer temperature for each day in the list. Instead of using a brute-force approach, I implemented an optimal O(n) solution using a Monotonic Stack. This technique efficiently tracks unresolved indices and updates them when a warmer temperature appears. Key takeaway: Using a stack-based approach helps reduce unnecessary comparisons and significantly improves performance compared to the naive O(n²) method. Consistency > intensity. Small progress every day compounds into real skill. #LeetCode #DataStructures #Algorithms #ProblemSolving #Java #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 44 of #100DaysOfCode Solved 1011. Capacity To Ship Packages Within D Days on LeetCode 📦 🧠 Key Insight: We need to find the minimum ship capacity such that all packages can be shipped within D days. This is a classic case of Binary Search on Answer. ⚙️ Approach: 1️⃣ The minimum capacity = max(weight) (since one package must fit) 2️⃣ The maximum capacity = sum of all weights 3️⃣ Apply Binary Search between these bounds 4️⃣ For each capacity mid, simulate shipping: 🔹Keep adding weights until capacity exceeds 🔹Move to the next day when exceeded 5️⃣ If we can ship within D days → try smaller capacity 6️⃣ Else → increase capacity This helps us find the minimum valid capacity efficiently. ⏱️ Time Complexity: O(n log(sum)) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #BinarySearch #Algorithms #Java #InterviewPrep #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 12 of My LeetCode Journey Today I solved Pow(x, n) (LeetCode 50). Problem: Implement a function to calculate x raised to the power n (xⁿ). The challenge is to handle large powers efficiently instead of multiplying x repeatedly. Approach: I used Binary Exponentiation (Fast Power). Key Idea: • If n is even → xⁿ = (xⁿ⁄²)² • If n is odd → xⁿ = x × xⁿ⁻¹ • If n is negative → convert to 1/x and make n positive This reduces the time complexity from O(n) to O(log n). Concepts Used: • Math • Binary Exponentiation • Iterative optimization Time Complexity: O(log n) Space Complexity: O(1) #leetcode #datastructures #algorithms #java #codinginterview #softwareengineering
To view or add a comment, sign in
-
-
Day 80/100 – LeetCode Challenge ✅ Problem: #6 Zigzag Conversion Difficulty: Medium Language: Java Approach: Direct String Traversal with Row Pattern Time Complexity: O(n) Space Complexity: O(n) Key Insight: Zigzag pattern repeats every 2 * (numRows - 1) characters. For each row, characters appear at fixed intervals with one middle character for non-first/last rows. Solution Brief: Handled special case numRows == 1 separately. For each row i: Start at index i Add character at current position For middle rows (not first or last), add second character at offset j + 2*(numRows-1) - 2*i Jump by 2*(numRows-1) for next cycle #LeetCode #Day80 #100DaysOfCode #String #Java #Algorithm #CodingChallenge #ProblemSolving #ZigzagConversion #MediumProblem #Pattern #Math #DSA
To view or add a comment, sign in
-
Explore related topics
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