Convert an Array Into a 2D Array With Conditions - Leetcode 2610 - Python

preview_player
Показать описание


0:00 - Read the problem
0:50 - Drawing Explanation
4:22 - Coding Explanation

leetcode 2610

#neetcode #leetcode #python
Рекомендации по теме
Комментарии
Автор

I'm glad to say that after watching a ton of NeetCode videos and solving a lot of Medium level problems, I'm not as afraid as I used to be looking at another MEDIUM problem anymore :). Thanks Navdeep

avinashsorab
Автор

An Easy hidden under disguise of a medium !

dingus
Автор

Thank you for the solution. Please continue uploading the solution of Leetcode POTD. It helps a lot.

omshinde
Автор

this solution is so smooth to understand

satyamraj
Автор

I solved it using Dfs, I was following your last all problem solution and kinda used same Dfs approach here to solve this.

public class Solution {
public IList<IList<int>> FindMatrix(int[] nums) {
var ans = new List<IList<int>>();
void Dfs(List<int> nums, HashSet<int> cache) {
if(nums == null || !nums.Any()) return;
var list = new List<int>();
var newlist = new List<int>();
foreach(int n in nums) {
if(!cache.Contains(n)) {
cache.Add(n);
list.Add(n);
} else {
newlist.Add(n);
}
}
ans.Add(list);
Dfs(newlist, new HashSet<int>());
}
Dfs(nums.ToList(), new HashSet<int>());
return ans;
}
}

joydeeprony
Автор

Well, Actually this can be solved without using this extra space, but with some extra time o(m*n) where m is the no of rows, n is the len(nums).

Idea :
If we check the constraints, atleast for this given problem its said that the values may not exceed the len(nums), using this as a hint, what we can do is that,
1.itereate over the values in nums and treat it as positions and simply go to that pos and sum the current value + len(nums)
2. Once if the above step is done for all the elements, its time to make the output.

Simply go over this modified array and check the position, if this is greater than len(nums) add pos+1 to the row and decrement len(nums) in the value .

3.Continue step2 untill max(nums) <=len(nums).

This gaurentees the solution can be done in 0(m*n) times.

visase
Автор

Please upload solutions of Leetcode contest too.

saipriyakarnati
Автор

Newbie here. How can I continue your roadmap? 

I have been solving leetcode with your roadmap, and I only solve easy questions and I just finished Heap/Priority Queue easy questions. I noticed that there are no more easy questions because backtracking has only contains Midium level. Do you think I should start medium level from Array? 

Any advise would be appriciated. Thank you!

Ds