Matrix Diagonal Sum - Leetcode 1572 - Python

Поделиться
HTML-код
  • Опубликовано: 7 фев 2025

Комментарии • 23

  • @uptwist2260
    @uptwist2260 Год назад +3

    Thanks again for the daily. I used a 2 pointers approach:
    var diagonalSum = function(mat) {
    let left = 0;
    let right = mat[0].length - 1;
    let sum = 0;
    for (let row = 0; row < mat.length; row += 1) {
    sum += mat[row][left];
    if (left != right) {
    sum += mat[row][right];
    }
    left += 1;
    right -= 1;
    }

    return sum;
    };

    • @jonnyskybrockett8844
      @jonnyskybrockett8844 Год назад

      Doing the if l == r on every iteration would slow it down by a factor of n instead of doing modular arithmetic after or before.

  • @Muslim.mode.
    @Muslim.mode. 5 месяцев назад +1

    I actually picked up the trick as my first EVER leetcode question, I Made 2 variables though instead of using i as the increments and had the if/else in the loop, 😄😄

  • @jonnyskybrockett8844
    @jonnyskybrockett8844 Год назад +5

    Oh, I did two separate for loops that only go across the diag. I guess it's O(n) either way.
    edit: shorted it to one loop and it beats a lot more now:
    class Solution {
    public:
    int diagonalSum(vector& mat) {
    int n = mat.size();
    int sum = 0;
    if(n % 2 == 1) sum -= mat[int(n/2)][int(n/2)];
    int j = 0;
    for(int i = 0; i < n; i++){
    sum += mat[i][j];
    sum += mat[n-i-1][j++];
    }
    return sum;
    }
    };

    • @MeanSoybean
      @MeanSoybean Год назад

      this looks like C++. if that's the case then you don't need to typecast it to int because n/2 is already int

  • @RobinHistoryMystery
    @RobinHistoryMystery 10 месяцев назад +1

    "did you notice something? probably did if you have eyes"... sassy aren't we 4:50

  • @deekshitht786
    @deekshitht786 Год назад +1

    Please upload more videos on leet code problems. No one can explain like u❤

  • @juanmacias5922
    @juanmacias5922 Год назад +2

    I ended up using a set, and checking if the tuple([row,col]) was in it lol

  • @krishnachoudhary4924
    @krishnachoudhary4924 Год назад +4

    Amazing. Could you please update your 350 questions list with these new questions. Thanks

    • @NeetCodeIO
      @NeetCodeIO  Год назад +5

      Yeah, I'll prob find a way to just automate it. But i'm nervous about making a list that keeps growing indefinitely.. will need to add pagination at least...

    • @shubhamraj25
      @shubhamraj25 Год назад

      @@NeetCodeIO do you handle your website on your own?

    • @utkarshdewan8736
      @utkarshdewan8736 Год назад +1

      @@shubhamraj25 He has a team

    • @def__init
      @def__init Год назад

      @@NeetCodeIO hahahahaha

  • @MP-ny3ep
    @MP-ny3ep Год назад +2

    Thank you!

  • @Karan-java-99
    @Karan-java-99 4 месяца назад

    class Solution {
    public int diagonalSum(int[][] mat) {
    int row = mat.length;
    int col = mat[0].length;
    int sum = 0;
    for(int i = 0; i < row; i++){
    // Check if the current index is not the center (to avoid double counting in odd-sized matrices)
    if(i != col - i - 1){
    int primary = mat[i][i];
    int secondary = mat[i][col - i - 1];
    sum = sum + primary + secondary;
    continue;
    }
    // If the current index is the center, to avoid double counting
    sum += mat[i][i];
    }
    return sum;
    }
    }

  • @aahiljunaidnext
    @aahiljunaidnext Год назад +1

    just make whole primary digonal 0 after adding it into the sum

  • @krateskim4169
    @krateskim4169 Год назад

    Thank you so much

  • @sondidu
    @sondidu Год назад +8

    Did you notice something?? You probably did if you have eyes.

  • @suneosama939
    @suneosama939 Год назад

    Can anybody explain line if n% 2 else 0, i don't understand 🤓

  • @felixboachieyiadom4457
    @felixboachieyiadom4457 Год назад +1

    Awesome solution
    I’ve watched so many videos from you that my code for this problem looks just like yours 😂😂🫶🏾