Matrix Diagonal Sum - Leetcode 1572 - Python

  Рет қаралды 7,788

NeetCodeIO

Жыл бұрын

🚀 neetcode.io/ - A better way to prepare for Coding Interviews
Solving Leetcode 1572, Matrix Diagonal Sum, today's daily leetcode problem on May 7.
🥷 Discord: discord.gg/ddjKRXPqtk
🐦 Twitter: neetcode1
🐮 Support the channel: www.patreon.com/NEETcode
⭐ BLIND-75 PLAYLIST: kzbin.info/www/bejne/gX3PiXZ8fJqHpKM
💡 DYNAMIC PROGRAMMING PLAYLIST: kzbin.info/www/bejne/bWTVZH6Nnqqpr80
Problem Link: leetcode.com/problems/matrix-diagonal-sum/
0:00 - Read the problem
0:30 - Drawing Explanation
5:40 - Coding Explanation
leetcode 1572
#neetcode #leetcode #python

Пікірлер: 23
@uptwist2260
@uptwist2260 Жыл бұрын
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.
@jonnyskybrockett8844
@jonnyskybrockett8844 Жыл бұрын
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
@Muslim.mode.
@Muslim.mode. Ай бұрын
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, 😄😄
@krishnachoudhary4924
@krishnachoudhary4924 Жыл бұрын
Amazing. Could you please update your 350 questions list with these new questions. Thanks
@NeetCodeIO
@NeetCodeIO Жыл бұрын
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 Жыл бұрын
@@shubhamraj25 He has a team
@def__init
@def__init Жыл бұрын
@@NeetCodeIO hahahahaha
@juanmacias5922
@juanmacias5922 Жыл бұрын
I ended up using a set, and checking if the tuple([row,col]) was in it lol
@deekshitht786
@deekshitht786 Жыл бұрын
Please upload more videos on leet code problems. No one can explain like u❤
@RobinHistoryMystery
@RobinHistoryMystery 6 ай бұрын
"did you notice something? probably did if you have eyes"... sassy aren't we 4:50
@sondidu
@sondidu Жыл бұрын
Did you notice something?? You probably did if you have eyes.
@NeetCodeIO
@NeetCodeIO Жыл бұрын
👀
@acceleratorlevel645
@acceleratorlevel645 Жыл бұрын
Bro just roasted and made us laugh at the same time lmao
@MP-ny3ep
@MP-ny3ep Жыл бұрын
Thank you!
@aahiljunaidnext
@aahiljunaidnext Жыл бұрын
just make whole primary digonal 0 after adding it into the sum
@Karan-java-99
@Karan-java-99 Ай бұрын
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; } }
@krateskim4169
@krateskim4169 Жыл бұрын
Thank you so much
@suneosama939
@suneosama939 10 ай бұрын
Can anybody explain line if n% 2 else 0, i don't understand 🤓
@shagggyyx
@shagggyyx Жыл бұрын
class Solution: def diagonalSum(self, mat: List[List[int]]) -> int: n = len(mat) res=0 for i in range(n): res+=mat[i][i]+mat[i][~i] if n%2==1: mid=n//2 res-=mat[mid][mid] return res This will also do sir.we can remove the mid by using negative indexing.
@felixboachieyiadom4457
@felixboachieyiadom4457 Жыл бұрын
Awesome solution I’ve watched so many videos from you that my code for this problem looks just like yours 😂😂🫶🏾
Help Me Celebrate! 😍🙏
00:35
Alan Chikin Chow
Рет қаралды 84 МЛН
Cool Parenting Gadget Against Mosquitos! 🦟👶
00:21
TheSoul Music Family
Рет қаралды 14 МЛН
А что бы ты сделал? @LimbLossBoss
00:17
История одного вокалиста
Рет қаралды 8 МЛН
Good teacher wows kids with practical examples #shorts
00:32
I migliori trucchetti di Fabiosa
Рет қаралды 12 МЛН
Help Me Celebrate! 😍🙏
00:35
Alan Chikin Chow
Рет қаралды 84 МЛН