I can't stop commenting on your videos...itne mast hai! I'm halfway through the video and already loved the approach and the trick you gave for diagonals. Simply awesome!
WOW!! i have done almost 380 problems on lc and i had never done a problem like this before. I am a bit sad about the fact that i couldn't solve it on my own coz i was trying a way to traverse the matrix diagonally. Anyways thanks bhai as always i learned something new today. :)
Hey CoderStoryWithMik, I've a query. But first, thank you so much for your effort, it's helping a lot of people including me. Query: what's the difference between [i-j] and [j-i]. As per my understanding it's helping us to identify diagonals and save diagonal values. I'm able to get the same answer with both the approaches, both i-j and j-i are working fine.
Java implementation using LinkedList: class Solution { public int[][] diagonalSort(int[][] nums) { //top-left to bottom-right: [i - j] technique int n = nums.length, m = nums[0].length; Map map = new HashMap(); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { int key = i - j; if (map.containsKey(key)) { LinkedList list = map.get(key); list.addLast(nums[i][j]); } else { LinkedList list = new LinkedList(); list.addLast(nums[i][j]); map.put(key, list); } } } for (LinkedList list : map.values()) { Collections.sort(list); } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { int key = i - j; LinkedList list = map.get(key); nums[i][j] = list.removeFirst(); } } return nums; } }
This is my solution after understanding the logic from the video and it pass all the test case on LC. private static int[][] diagonalSort(int[][] mat) { int n = mat.length; int m = mat[0].length; Map map = new HashMap(); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { int d = i - j; if (!map.containsKey(d)) map.put(d, new ArrayList()); map.get(d).add(mat[i][j]); } } for (Map.Entry ma : map.entrySet()) { ma.getValue().sort(Comparator.naturalOrder()); } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { int d = i - j; mat[i][j] = map.get(d).removeFirst(); } } return mat; }
I can't stop commenting on your videos...itne mast hai! I'm halfway through the video and already loved the approach and the trick you gave for diagonals. Simply awesome!
WOW!! i have done almost 380 problems on lc and i had never done a problem like this before. I am a bit sad about the fact that i couldn't solve it on my own coz i was trying a way to traverse the matrix diagonally. Anyways thanks bhai as always i learned something new today. :)
I can't stop commenting on your videos...itne mast hai!
I'm halfway through the video and already loved the approach and the trick you gave for diagonals.
Simply awesome!
Thank you so much 😀
I am so glad to read lovely comments ❤️🙏😇❤️
Thank you so much for bringing this much content for free 😊😊😊
Happy to help! 🙏😇
Just noticed while studying that your subs increased to 80000 plus. Congratulations bhaiya!
More power to you!
your explaning with neat representation bhaiya❤
Thank you so much 😀
WOW!! i have done almost 380 problems on lc and i had never done a problem like this before. I am a bit sad about the fact that i couldn't solve it on my own coz i was trying a way to traverse the matrix diagonally. Anyways thanks bhai as always i learned something new today. :)
Dont feel sad.
I am glad we learned something new. 🙏❤️😇
kaha job laga bhai abhi ?
Great explaination bro really appreciate it ❣️
Thanks a lot ❤️
Thank you for such an excellent explanation.
Thanks a lot ❤️
Nice vro keep going....
Thanks a lot ❤️
great explanation!
Glad it was helpful! ❤️❤️❤️
Hey CoderStoryWithMik, I've a query. But first, thank you so much for your effort, it's helping a lot of people including me.
Query: what's the difference between [i-j] and [j-i]. As per my understanding it's helping us to identify diagonals and save diagonal values.
I'm able to get the same answer with both the approaches, both i-j and j-i are working fine.
Yes both will work. Because [i-j] or [j-i] will be constant
Thankyou so muchhhh❤
Did it using O(n) space
class Solution {
public:
vector diagonalSort(vector& mat) {
int n = mat[0].size();
int l=0;
for(int k=0;k
Java implementation using LinkedList:
class Solution {
public int[][] diagonalSort(int[][] nums) {
//top-left to bottom-right: [i - j] technique
int n = nums.length, m = nums[0].length;
Map map = new HashMap();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int key = i - j;
if (map.containsKey(key)) {
LinkedList list = map.get(key);
list.addLast(nums[i][j]);
} else {
LinkedList list = new LinkedList();
list.addLast(nums[i][j]);
map.put(key, list);
}
}
}
for (LinkedList list : map.values()) {
Collections.sort(list);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int key = i - j;
LinkedList list = map.get(key);
nums[i][j] = list.removeFirst();
}
}
return nums;
}
}
Thanks a lot for sharing ❤️❤️
What is the time complexity ?
thanks!
nice
awesome explanation , gonna complete all the videos 🔥🫡
Thanks a lot ❤️❤️❤️
link of array playlist?
Sure.
ua-cam.com/play/PLpIkg8OmuX-K6A0sEPFxOSJh4_AjCGAFf.html&si=dd2Ha31O5Ey-VdXI
Thank you for watching 😇❤️
best
Thank you 🙏
kuch jada hi acha samjha dete ho bhai
This is my solution after understanding the logic from the video and it pass all the test case on LC.
private static int[][] diagonalSort(int[][] mat) {
int n = mat.length;
int m = mat[0].length;
Map map = new HashMap();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int d = i - j;
if (!map.containsKey(d))
map.put(d, new ArrayList());
map.get(d).add(mat[i][j]);
}
}
for (Map.Entry ma : map.entrySet()) {
ma.getValue().sort(Comparator.naturalOrder());
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int d = i - j;
mat[i][j] = map.get(d).removeFirst();
}
}
return mat;
}
i+j, i-j✅
nothing to say bhaiya .
bhaiya, doubts hai hh nahi toh comments kya kare😂😂
Means a lot 😇😇😇😍
< -----------------------------------JAVA----------------------------------------->
class Solution {
public int[][] diagonalSort(int[][] mat) {
Mapmap = new HashMap();
for(int i=0;i
I can't stop commenting on your videos...itne mast hai!
I'm halfway through the video and already loved the approach and the trick you gave for diagonals.
Simply awesome!
WOW!! i have done almost 380 problems on lc and i had never done a problem like this before. I am a bit sad about the fact that i couldn't solve it on my own coz i was trying a way to traverse the matrix diagonally. Anyways thanks bhai as always i learned something new today. :)