Day 3 in Bangalore

Day 3 in Bangalore

ยท

2 min read

๐Ÿ‘‹
Hi, I'm Pranay Sinha, a 3rd year B.Tech student with a keen interest in the tech industry and a passion for learning.

I'm on a mission to master programming and tackle any coding challenge that comes my way. In this series, you'll find my daily journal entries, presented as simple bullet points. Nothing fancyโ€”just my honest thoughts and experiences from each day. So, please, no judgment! ๐Ÿ˜‚

Join me as I document my tech journey in Bangalore. Let's dive in!

Day 3 (02/07/24)

  • Today's LeetCode question was as easy it gets 350. Intersection of Two Arrays II

    This one didn't even take a minute to solve. I had two approaches in mind when I first saw the question, both the approaches are Linear time so both are fine I guess.
    In the first approach, we can iterate over the first array and record the frequency of the elements. Then we build the solution array by iterating over the second array and putting elements in the resulting array if they have a frequency greater than 0 in the first array.

      class Solution:
          def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]:
              count = {}
              for num in nums1:
                  if num in count.keys():
                      count[num] += 1
                  else:
                      count[num] = 1
    
              result = []
              for num in nums2:
                  if num not in count.keys():
                      continue
                  elif count[num] > 0:
                      count[num] -= 1
                      result.append(num)
    
              return result
    

    In the second approach we have to sort both the arrays and use two pointer variables to keep track of the current index in both the arrays. I think the code is self explanatory for this one:

      class Solution:
          def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]:
              nums1.sort()
              nums2.sort()
    
              result = []
              i = 0
              j = 0
    
              while i<len(nums1) and j<len(nums2):
                  if nums1[i]==nums2[j]:
                      result.append(nums1[i])
                      i += 1
                      j += 1
                  elif nums1[i] < nums2[j]:
                      i += 1
                  else:
                      j += 1
    
              return result
    
  • Completed the basics section of the Striver's DSA Course, looks like I'll be revising some Sorting algorithms tomorrow

  • Most of my day was spent at the hospital getting my ear checkup, gotta go again tomorrow

  • Was really tired so I didn't do much else today

Hoping that tomorrow will be a productive day! See ya ๐Ÿ‘‹

ย