Convert an Array Into a 2D Array With Conditions - Leetcode 2610 - Python
Key Takeaways
The video demonstrates a solution to the Leetcode problem 2610, converting an array into a 2D array with conditions, using Python and a hashmap to minimize the number of rows and ensure unique integers in each row.
Full Transcript
hey everyone welcome back and let's write some more neat code today so today let's solve the problem convert an array into a 2d array with conditions so the idea is we're given an input array of nums which can contain duplicate values but we need to create a two-dimensional array from all of these numbers and basically the Restriction is that every Row in the 2D array has to have unique integers meaning there can't be any duplicates we have to do that while minimizing the number of rows and I think the reason why they even say this is because the easiest thing that we could do if this restriction didn't exist is just put every single number in its own row cuz that would guarantee that there aren't any duplicates in any of the rows but they want us to minimize the number of rows so basically we will be building a 2d Matrix and we'll only be adding a new row if we have to so one thing that you might notice about a a problem like this one is that the number of rows that we're going to have is going to be bounded by the number of copies the most frequent number is let's say the most frequent number is 10 and there are let's say four copies of 10 then we're going to end up having four rows because we have to put one of the copies of 10 in each different row suppose we're iterating over this input array how would we know if we have found a duplicate integer well we'd have to keep track of it and probably the best way to do that would be using some kind of hash data structure either a hash set or a hash map then we would know okay like this is a one and we've seen one before but knowing that we've seen a number before actually isn't enough it would be good if we actually had a hash map where we could count the number of times that we've seen a number before for example if I've seen one three times and now now it's the fourth time I'm seeing it that is significant because that means we put each of these ones in its own row so we had three rows at least for these three ones but now we see a fourth one so we have to either create a fourth row or just insert the one in the fourth row if it already exists and you might kind of notice a pattern with this this is the zeroth row this is the first row this is the second row and this is the third row if this this is the fourth time we're seeing this number then we're going to put it in the fourth minus one row or if we've already seen this number three times as you can see over here then we're going to put the next copy in the third row and of course if we hadn't seen that value at all before if we' only seen it like this is the first time we're seeing a one we'd put it in the zeroth row so that's pretty much the idea of solving this problem what we're going to do is we're going to iterate over the input array we're going to count the number of times we've seen each number so here we see one this is the first time that we're seeing it so we add it to the first row now we see a three it's the first time that we're seeing it so we add it to the first row four same thing add it to the first row now we see one for the second time and we know that cuz we're counting it in our hashmap so then we add it to a new row that we have to create and we can set the count now to two sor about the handwriting now we see two for the first time so we can just add it to the first row and set its count to one and we see three for the second time so we have to put three here we'll set its count to two as well and now we see one for the third time so we do have to create a third row for one so here you can see solving it that way we get the same thing that they did in the output so this is going to be our 2D Matrix notice that every row doesn't have to be the same length so it's not actually a matrix it's a 2d array sorry about that in terms of time complexity clearly we only have to iterate over the input once for memory complexity we are technically building the 2D array but even if that doesn't count we do have a hash map which can be in the worst case the size of the input array so time and memory complexity is going to be Big O of n where n is the size of the input array now let's code this up so I'm going to create a hashmap I'm going to call it a default dict in Python and the default value is going to be an integer so this will basically like if we haven't inserted a key yet into this like one for example the value of this will still be considered zero so even if we haven't inserted a key we're not going to get a key does an exist error we will get zero as the default value also going to declare the result which is going to be the 2D Matrix that we ultimately end up returning and before we can return it we have to build it so let's go through every number in the input array we don't care about the index we just just want the number itself now let's figure out what is the row that we want to insert this number into so I'm going to say for our hashmap let's get how many times we've seen this number before initially it's definitely going to be zero so that would mean we insert it into the zeroth row but we haven't even added the zeroth row yet so what we would do here is check if the length of result is equal to the row that means we're going to get an index out of bounds error if we try to do result of row we're going to get index out of bounds so what we do is to result we append this empty row this is the new row that we're adding now we can say result at index row let's append the current number that we just saw we're adding this number to this row and before we forget we should probably increment the count of the number before this Loop iteration is finished so let's do that so this actually is the entire code notice we only insert a row if it doesn't already exist otherwise we just append the current number to the relevant row the row comes from how many times we've already seen that number before because we don't ever want any duplicates in the same row now let's just run this to make sure that it works as you can see on the left yes it does it's pretty efficient even though the run time is pretty random on lead code if you found this helpful please like And subscribe if you're preparing for coding interviews check out NE code. thanks for watching and I'll see you soon
Original Description
🚀 https://neetcode.io/ - A better way to prepare for Coding Interviews
🧑💼 LinkedIn: https://www.linkedin.com/in/navdeep-singh-3aaa14161/
🥷 Discord: https://discord.gg/ddjKRXPqtk
🐦 Twitter: https://twitter.com/neetcode1
🐮 Support the channel: https://www.patreon.com/NEETcode
⭐ BLIND-75 PLAYLIST: https://www.youtube.com/watch?v=KLlXCFG5TnA&list=PLot-Xpze53ldVwtstag2TL4HQhAnC8ATf
💡 DYNAMIC PROGRAMMING PLAYLIST: https://www.youtube.com/watch?v=73r3KWiEvyk&list=PLot-Xpze53lcvx_tjrr_m2lgD2NsRHlNO&index=1
Problem Link: https://leetcode.com/problems/convert-an-array-into-a-2d-array-with-conditions/
0:00 - Read the problem
0:50 - Drawing Explanation
4:22 - Coding Explanation
leetcode 2610
#neetcode #leetcode #python
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from NeetCodeIO · NeetCodeIO · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Leetcode 149 - Maximum Points on a Line - Python
NeetCodeIO
Design Linked List - Leetcode 707 - Python
NeetCodeIO
Minimum Time to Collect All Apples in a Tree - Leetcode 1443 - Python
NeetCodeIO
Design Browser History - Leetcode 1472 - Python
NeetCodeIO
Number of Good Paths - Leetcode 2421 - Python
NeetCodeIO
Flip String to Monotone Increasing - Leetcode 926 - Python
NeetCodeIO
Maximum Sum Circular Subarray - Leetcode 918 - Python
NeetCodeIO
Find Closest Node to Given Two Nodes - Leetcode 2359 - Python
NeetCodeIO
Concatenated Words - Leetcode 472 - Python
NeetCodeIO
Data Stream as Disjoint Intervals - Leetcode 352 - Python
NeetCodeIO
LFU Cache - Leetcode 460 - Python
NeetCodeIO
N-th Tribonacci Number - Leetcode 1137
NeetCodeIO
Best Team with no Conflicts - Leetcode 1626 - Python
NeetCodeIO
Greatest Common Divisor of Strings - Leetcode 1071 - Python
NeetCodeIO
Shortest Path in a Binary Matrix - Leetcode 1091 - Python
NeetCodeIO
Insert into a Binary Search Tree - Leetcode 701 - Python
NeetCodeIO
Delete Node in a BST - Leetcode 450 - Python
NeetCodeIO
Shuffle the Array (Constant Space) - Leetcode 1470 - Python
NeetCodeIO
Fruits into Basket - Leetcode 904 - Python
NeetCodeIO
Number of Subarrays of size K and Average Greater than or Equal to Threshold - Leetcode 1343 Python
NeetCodeIO
Naming a Company - Leetcode 2306 - Python
NeetCodeIO
As Far from Land as Possible - Leetcode 1162 - Python
NeetCodeIO
Shortest Path with Alternating Colors - Leetcode 1129 - Python
NeetCodeIO
Minimum Fuel Cost to Report to the Capital - Leetcode 2477 - Python
NeetCodeIO
Count Odd Numbers in an Interval Range - Leetcode 1523 - Python
NeetCodeIO
Contains Duplicate II - Leetcode 219 - Python
NeetCodeIO
Path with Maximum Probability - Leetcode 1514 - Python
NeetCodeIO
Add to Array-Form of Integer - Leetcode 989 - Python
NeetCodeIO
Unique Paths II - Leetcode 63 - Python
NeetCodeIO
Minimum Distance between BST Nodes - Leetcode 783 - Python
NeetCodeIO
Design Hashmap - Leetcode 706 - Python
NeetCodeIO
Range Sum Query Immutable - Leetcode 303 - Python
NeetCodeIO
Binary Tree Zigzag Level Order Traversal - Leetcode 103 - Python
NeetCodeIO
Middle of the Linked List - Leetcode 876 - Python
NeetCodeIO
Course Schedule IV - Leetcode 1462 - Python
NeetCodeIO
Single Element in a Sorted Array - Leetcode 540 - Python
NeetCodeIO
Capacity to Ship Packages - Leetcode 1011 - Python
NeetCodeIO
IPO - Leetcode 502 - Python
NeetCodeIO
Minimize Deviation in Array - Leetcode 1675 - Python
NeetCodeIO
Longest Turbulent Array - Leetcode 978 - Python
NeetCodeIO
Last Stone Weight II - Leetcode 1049 - Python
NeetCodeIO
Construct Quad Tree - Leetcode 427 - Python
NeetCodeIO
Find Duplicate Subtrees - Leetcode 652 - Python
NeetCodeIO
Sort an Array - Leetcode 912 - Python
NeetCodeIO
Ones and Zeroes - Leetcode 474 - Python
NeetCodeIO
Remove Duplicates from Sorted Array II - Leetcode 80 - Python
NeetCodeIO
Maximum Twin Sum of a Linked List - Leetcode 2130 - Python
NeetCodeIO
Concatenation of Array - Leetcode 1929 - Python
NeetCodeIO
Symmetric Tree - Leetcode 101 - Python
NeetCodeIO
Check Completeness of a Binary Tree - Leetcode 958 - Python
NeetCodeIO
Construct Binary Tree from Inorder and Postorder Traversal - Leetcode 106 - Python
NeetCodeIO
Find Peak Element - Leetcode 162 - Python
NeetCodeIO
Accounts Merge - Leetcode 721 - Python
NeetCodeIO
Binary Tree Preorder Traversal (Iterative) - Leetcode 144 - Python
NeetCodeIO
Binary Tree Postorder Traversal (Iterative) - Leetcode 145 - Python
NeetCodeIO
Number of Zero-Filled Subarrays - Leetcode 2348 - Python
NeetCodeIO
Minimum Score of a Path Between Two Cities - Leetcode 2492 - Python
NeetCodeIO
Sqrt(x) - Leetcode 69 - Python
NeetCodeIO
Successful Pairs of Spells and Potions - Leetcode 2300 - Python
NeetCodeIO
Optimal Partition of String - Leetcode 2405 - Python
NeetCodeIO
More on: Algorithm Basics
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
Bloom Filters, Explained Properly
Dev.to · Daksh Gargas
Prefix Sums: The Preprocessing Trick That Makes Range Queries Instant
Medium · Programming
I Thought I Was Ready for the Interview — Then One Simple Math Question Destroyed Me
Medium · Programming
Week 2(Day 10): LeetCode Two Pointers(slow & fast): Remove Duplicates from Sorted Array (Brute…
Medium · Python
Chapters (3)
Read the problem
0:50
Drawing Explanation
4:22
Coding Explanation
🎓
Tutor Explanation
DeepCamp AI