Leetcode- Day 1

Hi there,

Have you ever experienced the frustration of trying to stay consistent with solving LeetCode problems? I certainly have. It's a struggle that many of us face. You start with enthusiasm, but, before you know it, your progress is disrupted. It's disheartening to have to start from scratch every time you decide to give it another shot. Can you relate?

Well, I’ve had enough of this. That’s why I’ve made a firm decision. I will solve at least one LeetCode problem every single day, no matter what. But I won't stop there—I'll solve as many as I can, pushing my limits and expanding my coding skills. And guess what? I'm going to document this journey right here on this blog.

Why am I doing this? It's simple. I want to hold myself accountable and stay committed to my coding practice. By posting my daily problem-solving achievements, I intend to inspire and motivate myself in the face of these challenges.

So, here’s to a new chapter!


88. Merge Sorted Array

Generally, you might take a nested for loop approach (at least, I initially thought that was the way to go). Also, the time complexity would be O(n^2)

What information is provided to us?

  • nums1 is an integer array of length m and is in sorted (ascending)

  • nums2 is an integer array of length n and is in sorted (ascending)

  • nums1 and nums2 should be merged

  • final array of length m+n should be stored in nums1

  • you don’t have to return anything

I’ve taken the following approach:

  • have variables i, j, k as 0
i,j,k = 0,0,0
  • i will help you traverse nums1, j will help traverse nums2, k will help through the final array

  • copy nums1 into a temporary array (say, nums)

(here, the nums1 array they initially provide is actually of length m+n. first m elements are numbers in sorted order. next n are zeros to accommodate for the nums2 numbers)

nums = nums1.copy()
  • you would want to check if the number in nums1 is greater than, lesser than or equal to the number in nums2 in a particular position

  • Using while loop, you ensure that i doesn’t exceed m (because nums1 has m numbers and we use i to traverse nums1, remember?) and you also ensure j doesn’t exceed n (for the same reason as mentioned)

  • there's one more thing you should keep in mind. It is that k should not exceed m+n

while(i<m and j<n and k<m+n):
  • if the number in nums1 ≥ number in nums2, you have to assign the number in nums at index i to nums1 at index k you need to increment i and k.
if (nums[i] <= nums2[j]):
    nums1[k] = nums[i]
  i+=1
  k+=1
  • if the number in nums1 < number in nums2, you have to assign the number in nums2 as index j to nums1 at index k you need to increment j and k.
if (nums[i] > nums2[j]):
    nums1[k] = nums2[j]
    j+=1
    k+=1
  • There are times when numbers get left out. So, you need to add the extra numbers in nums and nums2 to nums1
for a in range(i,m):
    nums1[k] = nums[a]
    k+=1
for a in range(j,n):
    nums1[k] = nums2[a]
    k+=1

Here is the full code:

class Solution:
    def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:

        nums = nums1.copy()
        i,j,k = 0,0,0

        while(i<m and j<n and k<m+n):

            if (nums[i] <= nums2[j]):
                nums1[k] = nums[i]
                i+=1
                k+=1
            elif (nums[i] > nums2[j]):
                nums1[k] = nums2[j]
                j+=1
                k+=1

        for a in range(i,m):
            nums1[k] = nums[a]
            k+=1
        for a in range(j,n):
            nums1[k] = nums2[a]
            k+=1

This isn’t the best way, but it isn’t the worst way either.

Time complexity : O(m+n)


Here’s another short and fun approach:

class Solution:
    def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:

        for i in range(n):
            nums1[m+i] = nums2[i]

        nums1.sort()

Hope you understood the approach. Let me know how I can improve the solution.

Happy Coding!