Sort the Matrix Diagonally - (MICROSOFT) : Explanation ➕ Live Coding 🧑🏻‍💻👩🏻‍💻

Sdílet
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

Komentáře • 35

  • @password47403
    @password47403 Před rokem +4

    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!

    • @codestorywithMIK
      @codestorywithMIK  Před rokem

      Thank you so much 😀
      I am so glad to read lovely comments ❤️🙏😇❤️

  • @rdrahuldhiman19
    @rdrahuldhiman19 Před 5 měsíci +1

    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.

    • @codestorywithMIK
      @codestorywithMIK  Před 4 měsíci

      Yes both will work. Because [i-j] or [j-i] will be constant

  • @user-ip7ov5ne8l
    @user-ip7ov5ne8l Před 8 měsíci +1

    your explaning with neat representation bhaiya❤

  • @dhairyachauhan6622
    @dhairyachauhan6622 Před 10 měsíci +2

    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. :)

    • @codestorywithMIK
      @codestorywithMIK  Před 10 měsíci

      Dont feel sad.
      I am glad we learned something new. 🙏❤️😇

    • @ankitkr4119
      @ankitkr4119 Před 3 měsíci

      kaha job laga bhai abhi ?

  • @Aman-er2yr
    @Aman-er2yr Před rokem +1

    Thank you for such an excellent explanation.

  • @priyanshusrivastava2145
    @priyanshusrivastava2145 Před rokem +1

    great explanation!

  • @ankitkr4119
    @ankitkr4119 Před 3 měsíci

    nice

  • @OnlyAyushAgarwal
    @OnlyAyushAgarwal Před rokem +2

    Nice vro keep going....

  • @leoved1073
    @leoved1073 Před rokem +1

    Great explaination bro really appreciate it ❣️

  • @fashionvella730
    @fashionvella730 Před 8 měsíci +1

    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

  • @M.m554
    @M.m554 Před 8 měsíci

    What is the time complexity ?

  • @honey6567
    @honey6567 Před 11 měsíci +1

    best

  • @bhuppidhamii
    @bhuppidhamii Před 7 měsíci +1

    link of array playlist?

    • @codestorywithMIK
      @codestorywithMIK  Před 7 měsíci +1

      Sure.
      czcams.com/play/PLpIkg8OmuX-K6A0sEPFxOSJh4_AjCGAFf.html&si=dd2Ha31O5Ey-VdXI
      Thank you for watching 😇❤️

  • @sidharthdhiman4522
    @sidharthdhiman4522 Před rokem +1

    awesome explanation , gonna complete all the videos 🔥🫡

  • @Music-tp8gg
    @Music-tp8gg Před rokem

    kuch jada hi acha samjha dete ho bhai

  • @pradipsah9900
    @pradipsah9900 Před rokem +3

    bhaiya, doubts hai hh nahi toh comments kya kare😂😂

  • @PushpendraKushvaha
    @PushpendraKushvaha Před měsícem

    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;
    }

  • @password47403
    @password47403 Před rokem +1

    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;
    }
    }

  • @legendcreatz9783
    @legendcreatz9783 Před 6 měsíci +2

    < -----------------------------------JAVA----------------------------------------->
    class Solution {
    public int[][] diagonalSort(int[][] mat) {
    Mapmap = new HashMap();
    for(int i=0;i