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; }
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, 😄😄
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; } };
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...
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; } }
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;
};
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.
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, 😄😄
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;
}
};
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
"did you notice something? probably did if you have eyes"... sassy aren't we 4:50
Please upload more videos on leet code problems. No one can explain like u❤
I ended up using a set, and checking if the tuple([row,col]) was in it lol
Amazing. Could you please update your 350 questions list with these new questions. Thanks
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...
@@NeetCodeIO do you handle your website on your own?
@@shubhamraj25 He has a team
@@NeetCodeIO hahahahaha
Thank you!
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;
}
}
just make whole primary digonal 0 after adding it into the sum
Thank you so much
Did you notice something?? You probably did if you have eyes.
👀
Bro just roasted and made us laugh at the same time lmao
Can anybody explain line if n% 2 else 0, i don't understand 🤓
Awesome solution
I’ve watched so many videos from you that my code for this problem looks just like yours 😂😂🫶🏾