8.05.2023
1572. Matrix Diagonal Sum easy
fun diagonalSum(mat: Array<IntArray>): Int =
(0..mat.lastIndex).sumBy {
mat[it][it] + mat[it][mat.lastIndex - it]
}!! - if (mat.size % 2 == 0) 0 else mat[mat.size / 2][mat.size / 2]
Join me on Telegram
https://t.me/leetcode_daily_unstoppable/206
Intuition
Just do what is asked.
Approach
avoid double counting of the center element
Complexity
Time complexity:
O(n)Space complexity:
O(1)