Sort the Matrix Diagonally - (MICROSOFT) : Explanation ➕ Live Coding 🧑🏻💻👩🏻💻
Vložit
- čas přidán 27. 08. 2022
- In this video we will try to solve a very good Qn “Sort the Matrix Diagonally”.
It has been asked in Microsoft and we will see a very generalised and tricky approach to solve this Qn.
Note that this technique will help you to solve another leetcode Qn : Leetcode-498 (Diagonal Traverse)
We will do live coding after explanation and see if we are able to pass all the test cases.
Problem Name : Sort the Matrix Diagonally
Company Tags : MICROSOFT
Leetcode Link : leetcode.com/problems/sort-th...
Similar GfG Qn : practice.geeksforgeeks.org/pr...
My solutions on Github : github.com/MAZHARMIK/Intervie...
Another Leetcode Qn Like this : Leetcode-498 (Diagonal Traverse) : leetcode.com/problems/diagona...
My GitHub Repo for interview preparation : github.com/MAZHARMIK/Intervie...
Subscribe to my channel : / @codestorywithmik
╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
#coding #helpajobseeker #easyrecipes
#interviewpreparation #interview_ds_algo #hinglish
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 ❤️🙏😇❤️
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
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 ?
Thank you for such an excellent explanation.
Thanks a lot ❤️
great explanation!
Glad it was helpful! ❤️❤️❤️
nice
Nice vro keep going....
Thanks a lot ❤️
Great explaination bro really appreciate it ❣️
Thanks a lot ❤️
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
What is the time complexity ?
best
Thank you 🙏
link of array playlist?
Sure.
czcams.com/play/PLpIkg8OmuX-K6A0sEPFxOSJh4_AjCGAFf.html&si=dd2Ha31O5Ey-VdXI
Thank you for watching 😇❤️
awesome explanation , gonna complete all the videos 🔥🫡
Thanks a lot ❤️❤️❤️
kuch jada hi acha samjha dete ho bhai
bhaiya, doubts hai hh nahi toh comments kya kare😂😂
Means a lot 😇😇😇😍
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;
}
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 ❤️❤️
< -----------------------------------JAVA----------------------------------------->
class Solution {
public int[][] diagonalSort(int[][] mat) {
Mapmap = new HashMap();
for(int i=0;i