Find Valid Matrix Given Row and Column Sums | Simple Approach | Leetcode 1605 | codestorywithMIK

Sdílet
Vložit
  • čas přidán 7. 09. 2024

Komentáře • 46

  • @pranjalpandey3077
    @pranjalpandey3077 Před měsícem +13

    I got the idea from your video at very begining and paused the video and coded it in a one go . Credit goes to your efforts and dedication sir

  • @shikhajoshi8961
    @shikhajoshi8961 Před měsícem +1

    Understood the approach and coded it myself. I have watched few of your videos and I observed that the way you explain things, wow man!! Huge respect..
    Thanks!

  • @gui-codes
    @gui-codes Před měsícem +7

    Hi mik, i know it's weekend. If you get some free time, please continue segment tree playlist.
    Also if possible DP Concepts too
    Thank you again for your hard work

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

      The playlist of DP is on the channel

  • @Arya20012
    @Arya20012 Před měsícem +1

    smooth like butter

  • @AbhijeetMuneshwar
    @AbhijeetMuneshwar Před měsícem +3

    What a logical solution !!! 🙇

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

    initial thoughts were to backtrack and generate the matrix. yet this approach simplifies.

  • @rishabhprasad8638
    @rishabhprasad8638 Před měsícem +1

    coded just after listening 5 mins

  • @aizad786iqbal
    @aizad786iqbal Před měsícem +2

    superb...
    Hint 1 - Find the smallest rowSum or colSum, and let it be x. Place that number in the grid, and subtract x from rowSum and colSum. Continue until all the sums are satisfied.
    but here we aren't finding the minimum rowSum or colSum right, or am i getting confused by their wording...
    also,
    what if the contraints were like there can't be any 0 element ,
    OR, if row sum col sum isn't equal
    or there can be say 3 negative elements etc....

  • @NAVEENKUMAR-uj7xe
    @NAVEENKUMAR-uj7xe Před měsícem

    Bro you made this solution very easy to understand ---- thumbs Up !

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

    lovely explaintaion

  • @srikanthsatya3226
    @srikanthsatya3226 Před měsícem +1

    Excellent Explanation Brooo Thank you 🤩🤩

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

    Solved it in O(N*M) time complexity. Here is the python implementation:
    class Solution:
    def restoreMatrix(self, rowSum: List[int], colSum: List[int]) -> List[List[int]]:
    n, m = len(rowSum), len(colSum)
    res = [[0 for _ in range(m)] for _ in range(n)]
    for i in range(n):
    for j in range(m):
    res[i][j] = min(rowSum[i], colSum[j])
    rowSum[i] -= res[i][j]
    colSum[j] -= res[i][j]
    return res

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

    great video

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

    Would be tough to come up with a solution during an interview.

  • @techyou6163
    @techyou6163 Před měsícem +1

    class Solution {
    public:
    vector restoreMatrix(vector& rowSum, vector& colSum) {
    int n=rowSum.size();
    int m=colSum.size();
    vector mat(n, vector(m));
    for(int i=0;i

  • @_A__Mohit
    @_A__Mohit Před měsícem +1

    Nice video ❤
    Segment tree video when?

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

    Thank You Bhiya :)

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

    Hey Mik,
    Can you make a video on today's biweekly contest question 3 and question 4

  • @chad._life
    @chad._life Před měsícem

    can you please upload a vdo of upsolving today's biweekly leetcode contest

  • @gauravbanerjee2898
    @gauravbanerjee2898 Před měsícem +1

    Thanks a lot bhaiya ❤❤

  • @divyanshkhandelwal5191
    @divyanshkhandelwal5191 Před měsícem +1

    How do we come up with such a solution? I tried every possible way to code it like this but ended up solving nothing, then watched your solution and cursed myself.

  • @moneyonline5299
    @moneyonline5299 Před měsícem +1

    bhaiya please make a playlist on question of binary search tree .

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

    One doubt why TC is O(m+n) , if we are creating answer 2d vector and assigning it with 0 it will take m*n time so I think TC shouls be O(m*n)

  • @tanishchordia4813
    @tanishchordia4813 Před měsícem +1

    first❤❤
    Love you mik❤

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

    While understanding I ignore the that matrix only contain non negative integer and below code working fine, but it also negative negative integer in matrix. Now here to understand the another approach to solve this
    public static int[][] restoreMatrix(int[] rowSum, int[] colSum) {
    int n = rowSum.length;
    int m = colSum.length;
    int[][] matrix = new int[n][m];
    int s = 0;
    for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
    if (j == m - 1) {
    matrix[i][j] = rowSum[i] - j;
    if (i < n - 1)
    s += rowSum[i] - j;
    } else {
    matrix[i][j] = 1;
    }
    }
    if (i == n - 1) {
    for (int j = 0; j < m; j++) {
    matrix[i][j] = colSum[j] - i;
    }
    }
    }
    matrix[n - 1][m - 1] = colSum[m - 1] - s;
    return matrix;
    }

  • @kunalsharma-zc2ho
    @kunalsharma-zc2ho Před měsícem +1

    class Solution {
    public int[][] restoreMatrix(int[] rowSum, int[] colSum) {
    int m = rowSum.length;
    int n = colSum.length;
    int i = 0;
    int j = 0;
    int[][] result = new int[m][n];
    int minVal = Integer.MAX_VALUE;
    while (i < m && j < n) {
    minVal = Math.min(rowSum[i], colSum[j]);
    rowSum[i] -= minVal;
    colSum[j] -= minVal;
    result[i][j] = minVal;
    if (rowSum[i] == 0) {
    i++;
    // filling the i++ with default 0
    }
    if (colSum[j] == 0) {
    j++;
    // filling the j++ with default 0
    }
    }
    return result;
    }
    }

  • @Rahul-pr1zr
    @Rahul-pr1zr Před měsícem

    Good explanation but I thought the problem was very unintuitive. There are multiple solutions to the problem and looking at the examples it's hard to come up with the idea of taking the minimum and maintaining the order.

  • @ayaanrashid960
    @ayaanrashid960 Před měsícem +3

    if someone is from Machine Learning, they can relate this question as confusion Matrix 😁😁😁

  • @abhinay.k
    @abhinay.k Před měsícem +1

    thanks

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

    class Solution {
    public int[][] restoreMatrix(int[] rowSum, int[] colSum) {
    int m = rowSum.length;
    int n = colSum.length;
    int ans[][] = new int[m][n];
    for(int i=0;i

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

    I am having doubt as how it gurantees the solution.

  • @RohitKumar-dz8dh
    @RohitKumar-dz8dh Před měsícem +1

    I was trying to solve this problem from two hours but couldn't get this approach 😔.

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

    what if we want to find out the number of such matrix possible... how can we approach in that case ?

  • @RohitKumar-dz8dh
    @RohitKumar-dz8dh Před měsícem

    Thanks 😊

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

    We can also solve this using priority queue just take smallest element and subtract it from rowSum and colsum
    Bte segment tree videos when?

    • @gui-codes
      @gui-codes Před měsícem +1

      yes maine bhi wahi socha tha starting me.

  • @k-CE-OmkarPathak
    @k-CE-OmkarPathak Před měsícem +1

    Op

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

    bhaiya ye greedy approach kam kyu ker raha iska proof hai koi?

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

    bhaiya 3213 ka soln banaie na
    class Solution {
    public:
    vector restoreMatrix(vector& rowSum, vector& colSum) {
    int m=rowSum.size();
    int n=colSum.size();
    vector vec(m,vector(n,0));
    for(int i=0;i

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

    sir me khud se logic nhi bna pata

  • @Rajdweep
    @Rajdweep Před měsícem +2

    kya hi bekar qs hai yr ekdam bhi intuition nhi aya