Skip to main content

2610. Convert an Array Into a 2D Array With Conditions

Hi, Today i bring to you problem of the day from Leetcode. So after the busy day and so called "this is my new year resolution" (just kidding obviously). i am going to at least look at leetcode of the day in Leetcode rather thinking f doing minimum 5 questions a day. (gets bit tough when you are lazy). 

Anyways today's problem was very cool. It was no brainier obviously f you are going for the brute force approach. But we need to optimize that brute approach as well. 

So, the qs was  Convert an Array Into a 2D Array With Conditions. I will go with step by step explanation for the code in a bit. But first I'd rather want my fellow ciders to at least spend 15-20 mins thinking about it. 

Hint: You need only unique elements in an array. There can be multiple unique arrays and the result will be in array of arrays format.

I hope you got enough time to think about the code. Let's begin in 

5

.

.

4.

.

.

3

.

.

2

.

.

1

.

.

(obviously for the dramatic effects)

So this is screenshot of my code, straight from my submissions.

 

 Let me give you little walk through of my code. As I said the problem statement was straight forward. I have used hashmap for my solution and to return the final result i used a list. 


    idx = 0
    hm = defaultdict(list)

 

  • `idx` is initialized to 0, and hm (short for hash map) is created as a defaultdict with lists as the default values. This dictionary will be used to store lists of numbers based on their index.

 

    for num in nums:
        for i in range(idx+1):
            if hm[i]:
                if num not in hm[i]:
                    hm[i].append(num)
                    break
            else:
                hm[i].append(num)
                idx +=1

 

 

  • The code then iterates over each element in the input list nums. For each number (num), it iterates over the range from 0 to idx+1.
  • Inside the nested loop, it checks if hm[i] (the list at index i in the hash map) is not empty. If it's not empty and num is not already present in the list, it appends num to the list and breaks out of the loop.

  • If hm[i] is empty (i.e., there's no list at index i), it appends num to a new list at index i and increments idx.

 

    res = []
    for key, val in hm.items():
        res.append(val)
    return res

 

  • Finally, the code creates an empty list res. It then iterates over the items in the hash map (hm) and appends the lists as values to the result list res. The function returns the final list of lists.

In summary, the code is grouping numbers from the input list into sublists based on their unique appearance as asked in the problem. The resulting list of lists (res) is returned as the output.


Below is the performance of the given code.

 Time complexity - O(N)

Space Complexity - O(N)

N, here is length of the array.


 

Hope it helps :)

 

Hi, thanks for reading my post, I hope you enjoyed it. So, if you want me to keep you updated with the tech references in coming future please do share your valuable comments with me and also do not forget to share it with your friends....:)

Comments

Popular posts from this blog

How to bypass Android pattern lock using CMD.

Many of you have encountered the situation where you forget your phone's pattern lock. This is totally common and we can't deny this. Then after trying all the combinations in mind and losing hope we just choose to reset our phone. But here I am providing you with the solution to this problem. You can just open up your phone following these few steps and without undergoing any trouble of losing your data and wasting your time.  Yes, I am talking about bypassing the pattern lock you used to unlock your phone. This procedure will be done using CMD. CMD, also known as command prompt is an amazing tool provided in Windows OP. You can control your entire computer using CMD which includes copying files from one folder to another, hiding your files, checking details of connected wireless devices and even shutting down your PC. You just need to learn some easy commands. How to bypass Pattern lock on your Android Phone? Before trying to Bypass the pattern lock...

1721 - Swapping Nodes in a Linked List.

Hi, Today we will be solving leetcode qs 1721 - Swapping Nodes in a Linked List. This qs is really simple. We need to swap the nodes placed at k distance from the start and end of the Linked List. So let's suppose you have a Linked List input as follows: 1 -> 2-> 4 -> 5 -> 7 -> 8 -> 10-> 9  and you are given a k = 2, so the output should be 1 -> 10 -> 4 -> 5 -> 7 -> 8 -> 2 -> 9 so how do we achieve it??? Intuition The most brute force solution one can think of is using Array data structure. For that, we will traverse through linked List and store all the elements in the Array, swap the two numbers and again insert it in theLinked List again. Not bad, but let's deal with it, it is not the optimal solution and far from being as an acceptable solution in interviews. So what can we done instead of that??? Let's try to look deep into the working of Linked List first and how do we traverse it. Linked List is a linear data structure that...

1235. Maximum Profit in Job Scheduling

Hi everyone, Today's post is little bit intriguing to me as it took me some time to get t the solution. I have been out of practice for a bit so I was not able to do it in the DP. I didn't want to copy the code. Though i understood the code after i checked its solution in the discussions. Anyways, i tried to do it with heap and with some thinking I was able to do it and with my surprise It performed real well. I would like to jump to the solution but first I would like to explain a little bit about heaps so you can get better understanding of the code. A heap is like a special type of list where the smallest (or largest) item is always at the front. Think of it like a priority line at a theme park, where the person with the highest priority goes to the front. There are two types of heaps: Min Heap: The smallest item is at the front. It's like standing in a line where the shortest person is always at the front. Max Heap: The largest item is at the front. It's like stand...