commit 9bf90b72f4588a291b2d7860ae605bfd04fcee53 parent 3b43f9335fe6c854605c51d7146521ef65c60ea5 Author: AndrewLockVI <andrewlaack1@gmail.com> Date: Sun, 7 May 2023 19:26:48 -0500 Completed matrix diagonal sum problem using dart Diffstat:
| A | matrix-diagonal-sum/diagonal-sum.dart | | | 32 | ++++++++++++++++++++++++++++++++ |
1 file changed, 32 insertions(+), 0 deletions(-)
diff --git a/matrix-diagonal-sum/diagonal-sum.dart b/matrix-diagonal-sum/diagonal-sum.dart @@ -0,0 +1,32 @@ +//Given a matrix return the sum of all the values in the +//diagnols from top left to bottom right and top right to +//bottom left. +//Ex. +//1 2 3 +//3 4 5 +//2 4 5 +//The output would be 15 because you add up 1 + 4 + 5 +//and then add 3 + 2 onto that. Since 4 is the shared center +//that will not be added on twice hence me zeroing it out in the +//code below. +//The time complexity of this code is O(n) where n is the size of the matrix. +//Time: 388ms Beats: 100% +//Memory: 144.6MB Beats: 100% + + +class Solution { + int diagonalSum(List<List<int>> mat) { + int total = 0; + for(int i = 0 ; i < mat[0].length; ++i){ + total += mat[i][i]; + mat[i][i] = 0; + } + int count = 0; + for(int i = mat[0].length - 1 ; i >= 0 ; --i){ + print(mat[count][i]); + total += mat[count][i]; + count += 1; + } + return total; + } +}