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

WhatsApp hidden Fonts

WhatsApp is a messaging app used by over 1 million world wide. It have been the default messaging app for the people since it was first made, Social messaging app supports Android, IOS and windows. It surely have become crucial part of our lives but some of us may not know about some of its hidden features. Whatsapp gives its user full liberty to block useless contacts, choose wisely to let other people know whether you read their messages or not (read receipts), setting privacy, secret fonts, and recent updates gave us the option to post stories too, no doubt why it is number 1 messaging app. Some of us may know the feature of hidden text and some of us may not, they are very simple. If you ever wanted to highlight the particular text or say make it bold so you can do that on whatsapp Here are some cool tricks that you can do while typing messages to your friends. Italicize the text by placing asterisks before and after the text. _techgen_ make the text appear bold by placing tidle sy...

What is a Processor?

Smartphones have been the most important part of our lives. We have seen the evolution of our  handsets. First we saw landlines which were mess of tangled wires then we saw mobile phones which is successor of landlines. They were portable devices from which you can make calls then we witnessed emergence of Smartphones which were  considered as portable computers which also allows us to make calls. What is a Processor? Underneath that touchscreen display there is a full- fledged computer that commands your apps so that they function properly. A processor executes what you want your smartphone to do or in other word you can say processor is brain of computer. there is Exynos, Snapdragon, quad-core octa-core etc. What is a core? It is an element of surprises. They are present inside processor and are responsible for reading the command and executing them. First devices came with single core processor but later on scientists invented more advanced processors such as dual core , So...

Samsung launches CFG70 curved gaming monitors in India.

Samsung electronics launched its two gaming monitors in India on Thursday, LC24FG70 (24 inch) and LC27FG70 (27 inch)-priced at ₹ 35000 and ₹ 42000 respectively. The company claims that they are first curved gaming computer with a 1 millisecond moving picture response time (MPRT). These two monitors are part of CFG70 series of curved gaming computers which were launched in IFA 2016. Apart from ther fast response time with their VA panels, the both come with Quantum dot technologyand a 144 Hz refresh rate that is optimised for AMD's FreeSynch technology. Both the monitors support a full HD resolution, and company is touting a contrast ratio of 3000:1.Samsung is also boasting of a UX that's specially designed for gamers, with an "intuitive settings dashboard" as well as hotkeys on the front and back of the display to quickly adjust settings between presets. The company also says each monitor undergoes rigorous pre-shipment factory calibration for compatibility with vario...